Excel VBA code to add text to bookmarks in Word needs help

Muttdog117

New Member
Joined
Sep 10, 2009
Messages
33
I am developing some VBA code that will open a word document on my computer and add cell information to a bookmark in the document. After some searching on the internet I was able to come up with some code that does exactly that. An issue I am having is when there is no data in a cell, I want Excel to delete the bookmark in the word document. Here is the code I have so far. It is in a basic testing stage right now:

Private Sub CommandButton1_Click()

Dim objWord

Dim objDoc

Dim objRange

Set objWord = CreateObject("Word.Application")

Set objDoc = objWord.Documents.Open("C:\users\Stan Laptop\My Documents\bookmarktest.docx")

objWord.Visible = True

Set objRange = objDoc.Bookmarks("Image1").Range

objRange.InsertAfter Range("B6")

Set objRange = objDoc.Bookmarks("Image2").Range

objRange.InsertAfter Range("B7")
End Sub

So Excel will open my word doc and it looks for the first bookmark in the file (image1) and then it pastes the data from cell B6 into that bookmark. Perfect. Now it looks for the second bookmark (image2) and then pastes the data from Cell B7 as expected. My question is if Cell B7 is blank how do I tell Excel to delete the image2 bookmark in my word doc?
 

Some videos you may like

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"

Watch MrExcel Video

Forum statistics

Threads
1,109,461
Messages
5,528,916
Members
409,847
Latest member
Foster034
Top