VBA将文本附加到书签

VBA将文本附加到书签

本文介绍了VBA将文本附加到书签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



我在VBA中编码创建以从Userform中的用户输入撰写法律文档。现在我需要将其格式化为一个很好的Word 2010文档。我想使用书签。



然而这让我疯狂:



使用ActiveDocument

.Bookmarks.Add(test)

。书签(test)范围=A

。书签(测试 ).Range.InsertAfterB

。书签(测试)。Range.InsertAfterC

结束如果



生成的文本始终显示为CBA我需要它在书签Test中为ABC。恢复线不是一种选择。由于其复杂性,文档需要按逻辑顺序组成。出于同样的原因,我希望将书签的数量保持在最低限度,并且在我将它们放入文档之前,喜欢从文本/输入文件中预先组合书签。



任何帮助都将受到高度赞赏。在此先感谢。

Hello,

I'm coding in VBA to create to compose a legal document from user input in a Userform. Now I need to format this into a nice Word 2010 document. I'd like to use bookmarks.

However this is driving me nuts:

With ActiveDocument
.Bookmarks.Add ("test")
.Bookmarks("test") Range = "A"
.Bookmarks("test").Range.InsertAfter "B"
.Bookmarks("test").Range.InsertAfter "C"
End If

The generated text is always displayed as "CBA" I need it to be "ABC" in bookmark "Test". Reverting the lines is not an option. The document needs to be composed in a logical order, due to its complexity. For the same reason I like to keep the number of bookmarks in use to a minimum, and like to "pre-assemble" bookmarks from pieces of text / input, before I put them into the document.

Any help will be highly appreciated. Thanks in advance.

推荐答案

Dim rngNewRange As Range ' Range that starts at point immediately after the bookmark
With ActiveDocument
    .Bookmarks.Add ("test") ' Create Bookmark
    '
    ' Set a new range that starts at the bookmark and is one character long
    Set rngNewRange = .Range(Start:=.Bookmarks("test").Range.End, End:=.Bookmarks("test").Range.End + 1)
End With
rngNewRange = "A" ' Add one character
rngNewRange.InsertAfter "B" ' Add another character
rngNewRange.InsertAfter "C" ' Add another character





秒示例



A second example

Sub x()
Dim rngNewRange As Range ' Range that starts at point immediately after the bookmark
With ActiveDocument
    .Bookmarks.Add ("test") ' Create Bookmark
    '
    ' Set a new range that starts at the bookmark and is six characters long
    Set rngNewRange = .Range(Start:=.Bookmarks("test").Range.End, End:=.Bookmarks("test").Range.End + 6)
End With
rngNewRange = "Hello " ' Add six characters
rngNewRange.InsertAfter "my name is " ' Add 11 characters
rngNewRange.InsertAfter "Mike!" ' Add 5 characters
End Sub





使用Word 2010测试


Dim r As Range
With ActiveDocument
    .Bookmarks.Add ("test")
    Set r = .Range(Start:=.Bookmarks("test").Range.End, End:=.Bookmarks("test").Range.End)
    r.InsertAfter "Aaa"
    r.InsertAfter "Beee"
    r.InsertAfter = "Cee"
End With





如果有人对Word文档的结构有一个很好的指针(其中)导航概念,即什么和为什么是范围),然后我很高兴收到一些指示。



If anybody has a good pointer about the structure of Word documents (its navigation concepts, i.e. what and why is a "Range"), then I'm happy to receive some pointers.


这篇关于VBA将文本附加到书签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 13:03