manipulating multiple bookmarks in a document from an html file

M

mahern

I have a Word 2007 document converted from an HTML file. Word has converted
certain tags to bookmarks. For arguments sake, say "mikebegin" repeats
multiple times throughout the document, represents a specific section in the
document, and needs to be manipulated (e.g., changing the font to bold,
italics). I have been trying to write a macro that selects each instance of
the bookmark and makes the appropriate change. However, if I select the
bookmark using the following code, it selects all non-related text between
the bookmarks (i.e., between the "mikeend" and next "mikebegin" bookmarks:

Sub Junk()
'
' Junk Macro
'
Set Book1 = ActiveDocument.Bookmarks("mikebegin")
Set Book2 = ActiveDocument.Bookmarks("mikeend")
'If Book2.End > Book1.Start Then Book1.Select

Selection.GoTo What:=wdGoToBookmark, Name:="mikebegin"

Selection.Bookmarks(1).Range.Bold = True


End Sub

I have noticed that if the document has only one "mikebegin" bookmark; the
Selection.Bookmarks(1).Range.Bold = True selects the text only down to a
</td> code that was generated as part of the original html file rather than
to the end of the document. But if more than one instance of the named
bookmark is present in the document, all text betwen the beginning of the
first instance of the bookmark and the </td> code for the second instance of
the bookmark is selected and manipulated by the macro code above.

So, in summary, I am trying to, in essence, multiple select text associated
with a recurring bookmark, and manipulate that text only; not the unrelated
text between the bookmarked text.

Thanks in advance for your help!
 
J

Jean-Guy Marcil

mahern said:
I have a Word 2007 document converted from an HTML file. Word has converted
certain tags to bookmarks. For arguments sake, say "mikebegin" repeats
multiple times throughout the document, represents a specific section in the
document, and needs to be manipulated (e.g., changing the font to bold,
italics). I have been trying to write a macro that selects each instance of
the bookmark and makes the appropriate change. However, if I select the
bookmark using the following code, it selects all non-related text between
the bookmarks (i.e., between the "mikeend" and next "mikebegin" bookmarks:

Sub Junk()
'
' Junk Macro
'
Set Book1 = ActiveDocument.Bookmarks("mikebegin")
Set Book2 = ActiveDocument.Bookmarks("mikeend")
'If Book2.End > Book1.Start Then Book1.Select

Selection.GoTo What:=wdGoToBookmark, Name:="mikebegin"

Selection.Bookmarks(1).Range.Bold = True


End Sub

I have noticed that if the document has only one "mikebegin" bookmark; the
Selection.Bookmarks(1).Range.Bold = True selects the text only down to a
</td> code that was generated as part of the original html file rather than
to the end of the document. But if more than one instance of the named
bookmark is present in the document, all text betwen the beginning of the
first instance of the bookmark and the </td> code for the second instance of
the bookmark is selected and manipulated by the macro code above.

So, in summary, I am trying to, in essence, multiple select text associated
with a recurring bookmark, and manipulate that text only; not the unrelated
text between the bookmarked text.

I am a bit lost here... What are these bookmarks you speak of?

Bookmark names must be unique. You cannot have two bookmarks with the same
name (Unless this was changed with Word 2007...).
So, I cannot understand what you mean by:
"But if more than one instance of the named bookmark is present "
???


By the way, you should not write "lazy" code... It might end up surprising
you with undesirable resuilts:

Sub Junk()
'
' Junk Macro
'
Set Book1 = ActiveDocument.Bookmarks("mikebegin")
Set Book2 = ActiveDocument.Bookmarks("mikeend")
'If Book2.End > Book1.Start Then Book1.Select

Selection.GoTo What:=wdGoToBookmark, Name:="mikebegin"

Selection.Bookmarks(1).Range.Bold = True


End Sub

Should be:

Sub Junk()

Dim Book1 As Bookmark
Dim Book2 As Bookmark

Set Book1 = ActiveDocument.Bookmarks("mikebegin")
Set Book2 = ActiveDocument.Bookmarks("mikeend")
'If Book2.Range.End > Book1.Range.Start Then Book1.Range.Select

Selection.GoTo What:=wdGoToBookmark, Name:="mikebegin"
Selection.Bookmarks(1).Range.Bold = True

End Sub
 
M

mahern

Thanks for your insight. I realized that the bookmark tags need to be unique
once I saw your reply. I subsequently took a very different tack by inserting
html tags representing Word quick styles tags into the HTML files at specific
locations. This seems to be recognized by Word once the html document is
opened in Word.

Just as an aside, the lazy code you noticed was generated by the Word macro
recorder.
 
J

Jean-Guy Marcil

mahern said:
Thanks for your insight. I realized that the bookmark tags need to be unique
once I saw your reply. I subsequently took a very different tack by inserting
html tags representing Word quick styles tags into the HTML files at specific
locations. This seems to be recognized by Word once the html document is
opened in Word.

Just as an aside, the lazy code you noticed was generated by the Word macro
recorder.

If you use the recorder to create "real" code, then make sure you have a
look at:
http://word.mvps.org/faqs/macrosvba/ModifyRecordedMacro.htm
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top