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?
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).

Forum statistics

Threads
1,215,036
Messages
6,122,794
Members
449,095
Latest member
m_smith_solihull

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