Preserving Formatting When Posting Documents Online

As you know, all formatting is lost when text is pasted into a plain-text Html editor from a normal formatted document (standards include .doc and .rtf files).

So, I designed the following Microsoft Word macro using MS Visual Basic to take care of the problem—it handles all bolding, italics, underlining, smart/curly quotes, normal quotes, em (—) and en ( – ) dashes, and paragraph breaks..

Remember: Don’t be scared by all the text in this document—the code is very easy to install and use.. (Note: This macro was only designed to work as-is inside of Microsoft Word, and may need to be altered if used in other applications. It was bug tested in MS Word 2000 but should work for most versions of Word or any program capable of running VB macros).

(Note: Some WYSIWYG editors will allow you to paste formatted text directly into the program/composition window, but the hidden code inside Microsoft Doc and Rich Text files can often result in unexpected effects (e.g., some browsers will not translate all the of text properly, or the resulting HTML code will be very convoluted and messy). Another route to take is to use Word to save your document as an HTML file, but, again, Microsoft will format your HTML source with lots of messy code that many browsers and HTML editing programs will stumble over.)

Once installed, this Macro will work extremely fast and efficiently with the simple click of couple of buttons (or one button, if you assign it a shortcut key). It will place the proper HTML code around italicized, bolded, and underlined words. This might look a little funny, but once placed inside the body of an HTML document (or in an HTML compatible editing screen online), you’ll see that all of your beautiful formatting looks perfect on the final webpage.

EXCEPTIONS: Some special characters (such as “@”), partially formatted words (e.g., “Formatted”), quote marks, and the spaces between words will be skipped (i.e., no formatting will be automatically applied). I’ve avoided formatting these particular things because they’ll look okay unformatted on a webpage; while, if this macro did format every little thing, it’d create a lot of messy-looking code (so, for instance, instead of “Formatted” you’ll end up with “Formatted”, which, I think, look quite good anyway).

Some of this may be updated in the future if requested. For instance, I could add a Macro that converts accented letters to the proper HTML code if there seems to be a lot of need for it (many print publications import accented characters improperly and end up with a lot strange symbols and question marks on their page because of it, but websites like Blogger.com usually handle accents fine).

***


Directions for installing the Macro:

1) Select all of the text in this document below the words “The Mighty Formatting Macro” (start with the words “Sub Format_Html”) and copy it to your clipboard (Ctrl-C).

2) Hit Alt-F8 (and the Macro tool will open).

3) Click "Edit" on the Macro tool. Microsoft Visual Basic will open.

4) Inside the edit screen of MS VB, in the main window on the right-hand side of your screen, scroll down to the very bottom of all the text (if any).

5) Now, hit Ctrl-V to paste the contents of your clipboard into the bottom of this Macro edit screen. (This will install the formatting macro into your copy of MS Word).

6) Click Ctrl-S to save. Close out of the Macro editor (the new Macros will be saved permanently and can be accessed from your Tools/Macro menu in Word at any time).

***


Directions for using the Macro:

1) Open up any document in Word (.doc, .rtf, etc.) that you’d like to format for HTML.

2) Hit Alt-F8 (the Macro tool will open back up).

3) Click "Edit" on the Macro tool. Microsoft Visual Basic will open. (If "Edit" is not available, it's probably because you have no Macros installed yet, so you have nothing to edit. So just hit "Create" instead and enter "temp" as the name of a new Macro. Then hit okay. Now you'll be able to follow the rest of the directions below.)

4) Select "Format_Html" and hit your Enter button (or click on "run" on the menu). All bold and italicized words (etc.) will now be marked with the appropriate HTML code.

5) You can now paste all of the content of your document into the body of an HTML webpage or into an HTML editor, and most formatting will be preserved when posted online.

6) Note: If you’d rather not have smart/curly quotes, you prefer normal Word paragraph marks to HTML paragraph marks, and you’d rather have actual quote marks preserved in the text instead of the correct quote-mark HTML tags, then hit Alt-F8 and run “Format_Html_Cleaner” after running the first Format_Html macro. (Make sure you have auto/smart quotes turned off in Word at this point, or else Word will automatically curl all of your quotes again.)

***


THE MIGHTY FORMATTING MACRO:



Sub Format_Html()
'
' Format_Html Macro
' Macro designed 2004/2005 (c) by Lucas Brachish
' If used, please link to CelebrityCola.blogspot.com
'
Selection.Find.ClearFormatting
Selection.Find.Font.Bold = True
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.Font.Bold = False
With Selection.Find
.Text = "<*>"
.Replacement.Text = "<b>^&</b>"
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.ClearFormatting
Selection.Find.Font.Italic = True
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.Font.Italic = False
With Selection.Find
.Text = "<*>"
.Replacement.Text = "<i>^&</i>"
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.ClearFormatting
Selection.Find.Font.Underline = wdUnderlineSingle
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.Font.Underline = wdUnderlineNone
With Selection.Find
.Text = "<*>"
.Replacement.Text = "<u>^&</u>"
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "<<u>b</u>><u>"
.Replacement.Text = "<u><b>"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
With Selection.Find
.Text = "<<i>b</i>><i>"
.Replacement.Text = "<i><b>"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
With Selection.Find
.Text = "<<u>i</u>><u>"
.Replacement.Text = "<u><i>"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "</u></<u>b</u>>"
.Replacement.Text = "</b></u>"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
With Selection.Find
.Text = "</i></<i>b</i>>"
.Replacement.Text = "</b></i>"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
With Selection.Find
.Text = "</u></<u>i</u>>"
.Replacement.Text = "</i></u>"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
With Selection.Find
.Text = "<<u><i>b</i></u>><u><i>"
.Replacement.Text = "<u><i><b>"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
With Selection.Find
.Text = "</i></u></<u><i>b</i></u>>"
.Replacement.Text = "</b></i></u>"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
With Selection.Find
.Text = "<b<"
.Replacement.Text = "<b><"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
With Selection.Find
.Text = "<<"
.Replacement.Text = "<"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
With Selection.Find
.Text = ">>"
.Replacement.Text = ">"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
With Selection.Find
.Text = "^p"
.Replacement.Text = "</p><p>"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
With Selection.Find
.Text = "</p><p></p><p>"
.Replacement.Text = "</p><p>"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
Dim MyText As String
Dim MyRange As Object
Set MyRange = ActiveDocument.Range
MyText = "<p>"
' Selection Example: Inserts text before
' the cursor in the document.
' Selection.InsertBefore (MyText)
' Range Example: Inserts text at the beginning
' of the active document.
MyRange.InsertBefore (MyText)
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = ChrW(8220)
.Replacement.Text = "&ldquo;"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
With Selection.Find
.Text = ChrW(8221)
.Replacement.Text = "&rdquo;"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "^+"
.Replacement.Text = "—"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "^="
.Replacement.Text = "–"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = ChrW(8217)
.Replacement.Text = "’"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = ChrW(8216)
.Replacement.Text = "‘"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = """"
.Replacement.Text = "&quot;"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
With Selection.Find
.Text = "'"
.Replacement.Text = "&#39;"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub


Sub Format_Html_Cleaner()
'
' Format_Html_Cleaner Macro
' Macro recorded 1/28/2005 by Lucas Brachish
'
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "&#39;"
.Replacement.Text = "'"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = """
.Replacement.Text = """
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "&rsquo;"
.Replacement.Text = "'"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "‘"
.Replacement.Text = "'"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "&ldquo;"
.Replacement.Text = """
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "&rdquo;"
.Replacement.Text = """
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
With Selection.Find
.Text = "</p><p>"
.Replacement.Text = "^p^p"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
With Selection.Find
.Text = "<p>"
.Replacement.Text = "^p"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub

4 comments:

Lucas Brachish said...

If you're a user of Blogger.com, MoveableType, TeamSite, or anything similiar, you'll probably want to run BOTH of the macros above every time. Why? Because the HTML editing screens on sites like Blogger.com will automatically insert breaks between your paragraphs, and will even accept smart (curly) quotes without HTML tags. So it's really just the bolding, italics, etc., that you'll need to worry about....

Just running the first Macro above will give you good HTML code -- the webpage will look fine. But if you ever want to go back and edit the code, it's gonna be harder to look at. So with a Blog, you might want text that only has minimal coding, so it's easy for you to change the source code at any time, even if you're not using the "compose" screen/WYSIWYG editor.

So, to streamline the process, you can simply remove the following code from the beginning of the second Macro and the end of the first Macro.... that way, they'll both run at the same time, and you'll be done super-fast:

End Sub

Sub Format_Html_Cleaner()
'
' Format_Html_Cleaner Macro
' Macro recorded 1/28/2005 by Lucas Brachish
'


That's it!

Lucas Brachish said...

Also see:


Gawd Dash It All: MS Word Shortcuts Made Easy

Naked Celebrities said...

Just running the first Macro above will give you good HTML code -- the webpage will look fine. But if you ever want to go back and edit the code, it's gonna be harder to look at. So with a Blog, you might want text that only has minimal coding, so it's easy for you to change the source code at any time, even if you're not using the "compose" screen/WYSIWYG editor.

youtube downloader said...

great.
i had the problem in the past.
even though i don't need it anymore it's good to know the solution. just in case :)
thank you