Quantcast
Channel: Excel Help Forum - Excel Programming / VBA / Macros
Viewing all articles
Browse latest Browse all 50057

How to retrieve value of the bookmarks from the word document

$
0
0
I have written a code to delete the sentence range which has its bookmark value as empty.
Code:

Sub deletebookmarks()
Dim rng As Range
For Each bmkname In Wrd.ActiveDocument.Bookmarks()
        If Wrd.ActiveDocument.Bookmarks(bmkname).Range.Text = "" Then
          Set rng = bmkname.Range
            rng.Expand Unit:=wdSentence
            rng.Delete
        End If
    Next
End Sub

But the problem is that the sentence with the empty bookmark text is getting deleted. What i really want is to delete the bookmarks and its corresponding sentence if the bookmark value is empty. For example, the bookmark text is "XXX" but the data populated from excel cell to bookmark is empty cell. So i need to delete that bookmark and its sentence. the above code deletes the bookmark if there is no text. I tried working over this for several hours but of no improvement. Please help.

Viewing all articles
Browse latest Browse all 50057

Trending Articles