Insert text at a bookmark via VBA

aellington287

New Member
Joined
Mar 7, 2016
Messages
7
I am somewhat new to VBA, I have created a userform that will open a word document and update several lines of text within the document before saving and closing it. I know that it is possible to insert text at a bookmark, but I do not currently have the understanding to do this. My code is as follows:

Code:
Private Sub Generate_Click()

Dim wdapp As Object
Set wdapp = CreateObject("Word.Application")
    wdapp.Visible = True
    wdapp.ChangeFileOpenDirectory "V:\PI\QI\MDB\"
    wdapp.documents.Open Filename:="DATA-  -PSS.docx"
wdapp.ActiveDocument.Close savechanges:=True
    wdapp.Quit
    Set wdapp = Nothing

Unload Me
End Sub

What line of code would I use and where would I put it if I were wanting to insert "I love excel and VBA" followed by the contents of cell A1, at the bookmark labeled "txtbx1"? Any help would be appreciated. Thanks!
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
It can be as simple as this.
Code:
wdapp.ActiveDocument.Bookmarks("txtbx1").Range.InsertAfter "I love excel and VBA."
 
Upvote 0
It can be as simple as this.
Code:
wdapp.ActiveDocument.Bookmarks("txtbx1").Range.InsertAfter "I love excel and VBA."

would it be possible to modify the code to insert a section of a word document (e.g. the text between two distinct bookmarks) into a new document, instead of
"I love excel and VBA."?

Thanky in advance,

Sarah
 
Upvote 0
Sarah

I suppose you could try something like this.
VBA Code:
Dim rngCopy As Object
Dim rngStart As Object
Dim rngEnd As Object

    With wdApp.ActiveDocument
        Set rngStart = .Bookmarks("Start")
        Set rngEnd = .Bookmarks("End")
        Set rngCopy = .Range(rngStart.Range.Start, rngEnd.Range.End)
        .Bookmarks("txtbx1").Range.InsertAfter "I love excel and VBA."
    End With
 
Upvote 0
Thank you very much!


Excuse me, I have been unclear in my question. I wanted to insert the text that is written between the two bookmarks ("Start" and "End") of a word document, into a new word document. The text should be inserted after a defined bookmark in the new document (e.g. "BookmarkNewDoc").

So i was wondering whether it would be possible to modify the code required by aellington287 as part of this question, so that not the string "I love Excel and VBA" is inserted into the new document, but the
text between the bookmarks "Start" and "End" from an old word document.

Thank you very much and have a great day,

Sarah
 
Upvote 0
Where would the code be placed?

In the old document or in an unrelated document?

If it's the latter you would need code to open both the old and 'new' document, something like this.
VBA Code:
Option Explicit

Sub CopyBookmark()
Dim wdApp As Object
Dim wdDocOld As Object
Dim wdDocNew As Object
Dim rngSrc As Object
Dim rngDst As Object
Dim rngStart As Object
Dim rngEnd As Object

    Set wdApp = CreateObject("Word.Application")
    
    With wdApp
        .Visible = True
        Set wdDocOld = .Documents.Open(Filename:="Old.docx")
        Set wdDocNew = .Documents.Open(Filename:="New.docx")
    End With
    
    With wdDocOld
        Set rngStart = .Bookmarks("Start").Range
        Set rngEnd = .Bookmarks("End").Range
        Set rngSrc = .Bookmarks(rngStart.Range.Start, rngEnd.Range.End)
    End With
    
    
    Set rngDst = wdDocNew.Bookmarks("BookmarkNewDoc").Range
    
    rngDst.InsertAfter rngSrc.Text
    
    Set wdApp = Nothing

End Sub
 
Upvote 0
Thank you so much!!

The code should be placed in the Old document [the document that contains the information that should be inserted into the new document]
But afterwards, the new document should save and close automatically.

Can i use/edit the code posted above, or do I have to change the code in this situation?

Best regards and many thanks,

Sarah
 
Upvote 0
If that's the case you can change the code to this.
VBA Code:
Sub CopyBookmark()
Dim wdDocOld As Document
Dim wdDocNew As Document
Dim rngSrc As Range
Dim rngDst As Range
Dim rngStart As Range
Dim rngEnd As Range

    Set wdDocOld = ThisDocument
    Set wdDocNew = Documents.Open(FileName:="New.docx")
    
    With wdDocOld
        Set rngStart = .Bookmarks("Start").Range
        Set rngEnd = .Bookmarks("End").Range
        Set rngSrc = .Range(rngStart.Start, rngEnd.End)
    End With
    
    Set rngDst = wdDocNew.Bookmarks("BookmarkNewDoc").Range
    
    rngDst.InsertAfter rngSrc.Text
    
End Sub
 
Upvote 0
Thank you so much!

I have defined the bookmarks "Start" and "End" in the old doc,and the bookmark "BookmarkNewDoc" in the new doc.

But after executing the code posted above, the error "Run-Time error 5941: The requested member of the collection does not exist" appears.

Do I have to take something special into acccount when defining the bookmarks?

Best regards,

Saah
 
Upvote 0

Forum statistics

Threads
1,223,229
Messages
6,170,881
Members
452,364
Latest member
springate

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top