Results 1 to 8 of 8
  1. #1
    Star Lounger
    Join Date
    Feb 2003
    Location
    Annapolis, Maryland, USA
    Posts
    84
    Thanks
    0
    Thanked 0 Times in 0 Posts

    macro for converting formatting to HTML (2003 SP3)

    I've tried and failed to create a macro to add HTML tags to basic formatting in Word--italics and boldface. Does anybody have something like this?
    Michael Coleman
    BOMI International

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 18 Times in 18 Posts

    Re: macro for converting formatting to HTML (2003 SP3)

    You could save a document as filtered html - Word will add all the tags you want (and more).

  3. #3
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 18 Times in 18 Posts

    Re: macro for converting formatting to HTML (2003 SP3)

    Here is a simple macro. It doesn't produce tags the most efficient way if bold and italic overlap, but it works (at least, in my test):
    <code>
    Sub AddTags()
    With ActiveDocument.Content.Find
    .ClearFormatting
    .Text = ""
    .Replacement.ClearFormatting
    .Format = True
    .Font.Bold = True
    .Font.Italic = True
    .Execute ReplaceWith:="^&", Replace:=wdReplaceAll
    .Font.Bold = False
    .Execute ReplaceWith:="^&", Replace:=wdReplaceAll
    .Font.Bold = True
    .Font.Italic = False
    .Execute ReplaceWith:="^&", Replace:=wdReplaceAll
    End With
    End Sub
    </code>
    I hope that the tags in this post will be displayed correctly.

  4. #4
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: macro for converting formatting to HTML (2003

    Here's something I wrote years ago. I can't fully understand it now, but if you want something to play with...

    <code>Sub BoldToHTML()
    If MsgBox("Convert Word bold to HTML tags?", vbQuestion + vbYesNo) = vbNo Then Exit Sub
    Selection.HomeKey wdStory, wdMove
    With Selection.Find
    .ClearFormatting
    .Font.Bold = True
    .Text = ""
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindStop
    .Format = True
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    Do
    If .Execute() = False Then Exit Do
    With Selection
    If Len(.Text) > 2 Then
    .InsertBefore ""
    .InsertAfter "
    "
    .Font.Bold = False
    .Collapse wdCollapseEnd
    ElseIf Len(.Text) = 1 Then
    If .Text = Chr(13) Then
    .Font.Bold = False
    .Collapse wdCollapseEnd
    Else
    .InsertBefore ""
    .InsertAfter "
    "
    .Font.Bold = False
    .Collapse wdCollapseEnd
    End If
    End If
    End With
    Loop
    .ClearFormatting
    End With
    End Sub</code>

    For italics, I had a separate procedure that worked the same way (changing the style searched and the tags inserted, of course).

  5. #5
    Star Lounger
    Join Date
    Feb 2003
    Location
    Annapolis, Maryland, USA
    Posts
    84
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: macro for converting formatting to HTML (2003 SP3)

    This worked. The tags were fine in the post, but I copied it from my e-mail inbox, where they were a bit mangled. I worked it out, though. Thanks.
    Michael Coleman
    BOMI International

  6. #6
    3 Star Lounger
    Join Date
    Jan 2004
    Location
    Los Angeles, California, USA
    Posts
    322
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: macro for converting formatting to HTML (2003 SP3)

    I've been trying to find out the meaning of ^& is. I can't seem to find in in the help files. Help files are very help but the trick is trying to find what you're looking for. I've tried using the Find dialog box with the Special buttons but no luck.
    You know it&#39;s time to diet when you push away from the table and the table moves.

  7. #7
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 18 Times in 18 Posts

    Re: macro for converting formatting to HTML (2003 SP3)

    ^& is the code you can use in the "Replace with" box for "whatever the text you found is". You can't use it in the "Find what" box, so if you click Special while in the "Find what" box you won't see it.

    See <!post=Find/Replace Special Characters (Word),421558>Find/Replace Special Characters (Word)<!/post> for a complete overview of the codes you can use in the "Find what" and "Replace with" boxes.

  8. #8
    3 Star Lounger
    Join Date
    Jan 2004
    Location
    Los Angeles, California, USA
    Posts
    322
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: macro for converting formatting to HTML (2003 SP3)

    Thank you Hans. So the ^& represents the contents of what the Find method found and you insert html tags before and after the contents that it found. I thought that might be the case. I understand how it works now. Thanks.

    That was a nice post you sent me to and a start post at that. Phil did a pretty good job.
    You know it&#39;s time to diet when you push away from the table and the table moves.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •