Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Detecting Formats in Word Documents 1

Status
Not open for further replies.

bgreenhouse

Technical User
Feb 20, 2000
231
CA
Hi Folks

I'm writing a VBA macro to create an XML document from a Word table. The client wants to be able to preserve formatting, so I want to be able to detect words that are in italics or bold and enclose them in the appropriate HTML tags. I know how to find, enclose, and all that, I'm just not sure if you can detect words based on their format (ie italics or bold).

Any suggestions?

Thanks

Ben
 
Depends on what you're trying to capture and reproduce. For example, if you're simply looking for bold and italic, you can query
selection.font.bold
and
selection.font.italic
to find out the settings. If you also need to find font changes, etc, it's more work...
Rob
[flowerface]
 
Hi Rob

What I want to do is to replace any text in italics with the same text surrounded by
Code:
<i></i>
, and any text in bold the text surrounded by
Code:
<b></b>
. I see what you're saying about determining whether a selection is bold or not (I assume it's something like selection.font.bold = true), but how do I find words that are formatted like that?

Thanks

Ben

 
The following code will find the next selection that is bolded (the selection will be the entire bolded phrase):

With Selection.Find
.font.bold=true
.Text = &quot;&quot;
.Forward = True
.Format = True
.execute
End With

Having said that, I still think in most cases it's easier to just copy the text from Word character by character, and check for bold and italic at every step. Yes, it sounds laborious, but going BACK to find text is often even more problematic.
Rob
[flowerface]
 
I did a macro recording for you. I have to go now so I did not bother cleaning it up.

Code:
Sub Macro1()
'
' Macro1 Macro
' Macro recorded 02/11/03 by Justin G. Ezequiel
'
  Selection.Find.ClearFormatting
  Selection.Find.Font.Italic = True
  Selection.Find.Replacement.ClearFormatting
  Selection.Find.Replacement.Font.Italic = False
  With Selection.Find
    .Text = &quot;&quot;
    .Replacement.Text = &quot;<i>^&</i>&quot;
    .Forward = True
    .Wrap = wdFindContinue
    .Format = True
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
  End With
  Selection.Find.Execute Replace:=wdReplaceAll
  Selection.Find.ClearFormatting
  Selection.Find.Font.Bold = True
  Selection.Find.Replacement.ClearFormatting
  Selection.Find.Replacement.Font.Bold = False
  With Selection.Find
    .Text = &quot;&quot;
    .Replacement.Text = &quot;<b>^&</b>&quot;
    .Forward = True
    .Wrap = wdFindContinue
    .Format = True
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
  End With
  Selection.Find.Execute Replace:=wdReplaceAll
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top