Click here to Skip to main content
15,889,335 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
Posted

A bookmark Range cannot be extended. The documentation for Range.InsertAfter says that. Therefore, you have to create a new range that starts immediately after the bookmark Range. The new range is created so that it is the length of the first text that is to be added to the range. In this test case, a one is added in the Set rngNewRange statement.

VB
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
VB
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


Test using Word 2010
 
Share this answer
 
v4
Thanks Mike!

Your solution works fine. I still have some questions about why VBA code has to be so ugly and unlogical but it'll do for now.

It seems (I haven't got a clue why) that the "+1" is unnecessary, the range automagically adapts to the right length.

This is the code I went with:

SQL
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


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.
 
Share this answer
 
Comments
Mike Meinz 18-Sep-13 13:17pm    
It is considered bad form to post solutions to your own questions. You should use "Have a question or comment?" on a Solution or "Improve Question" to add comments or provide additional information.

The Range for a Bookmark is a special case because it is zero length. When you created r in your example above, you created a new zero length Range object and then with InsertAfter put content at the end of zero length Range. InsertAfter extended the Range by the size of the text that was inserted.

What no stars for solving your problem?
netogg 19-Sep-13 11:58am    
Sorry, didn't want to be rude, I'm just ignorant. I might not have phrased my problem accurate enough. I'll sweat some more about it, and might ask a better question if necessary. Thank again though.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900