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!
 
Excuse me, I have overseen a typo!
Your code works perfectly fine!

But your code currently also copies the names of the bookmarks (Start and End) into the new document.

Is there a way to prevent this?

i have tried using Range.End + 1 / Range.End - 1, but this does not work correctly.

Thank you very much Norie, I really appreciate your help!

Have a great day and thanks in advance,

Sarah
 
Upvote 0

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Do you mean 2 new bookmarks named 'Start' and 'End' are being created?
 
Upvote 0
After the macro has copied the text from the old document into the new document, the text inserted into the new document looks like this:

Start - Text that should be inserted - End


So basically the names of the bookmarks that define the range in the old document are also pasted into the new document.

Thank you in advance,

Sarah
 
Upvote 0
Thank you very much!!

Because the bookmarks are the headings of different chapters in the new document, I am also trying to insert the text below the bookmark in the new document.

E.g.:
"BookmarkNewDoc"
Inserted text


This would be very helpful, because there would be no need for fixing the layout/structure of the new document after inserting the new text.

Is this possible with vba?

Thank you and best regards,

Sarah
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,906
Members
452,366
Latest member
TePunaBloke

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