lmshaft
03-03-2006, 09:35 AM
I have a manual process in Word where I create articles using Word 2003 and then save the articles as .rtf and then on to html (with a separate rtf-to-html conversion process). I'm attempting to streamline the process by making it all happen in Word by either converting directly to HTML (or possibly XML and then on to HTML).
The articles contain default and Custom Word Styles. I am currently looping through all paragraphs, finding certain styles, then adding html code before and after the selected paragraph. What I'd also like to do is find formatted text within a paragraph (of only certain Styles) and change the formatted text to their corresponding html tags. Here's the code I have so far...
Public Sub Main()
Dim p As String
p = "<p class=""class1"">"
pClose = "</p>"
h2 = "<h2 class=""class2"">"
h2Close = "</h2>"
i = "<i>"
iClose = "</i>"
' loop through all paragraphs for opening paragraph tags
For Each paragraph In ActiveDocument.Paragraphs
paragraph.Range.Select
If paragraph.Style = "Normal" Then
Selection.Range.InsertBefore (p)
' MsgBox "paragraph selected"
End If
If paragraph.Style = "Heading 2" Then
Selection.Range.InsertBefore (h2)
End If
' within each paragraph, I need to find all Italic, Bold, etc. text and replace with tags
' how do I just search text that is currently selected?
Next
' for close tags
For Each paragraph In ActiveDocument.Paragraphs
paragraph.Range.Select
If paragraph.Style = "Normal" Then
Selection.Range.MoveEnd Unit:=wdCharacter, Count:=-1
Selection.Range.InsertAfter (pClose)
End If
If paragraph.Style = "Heading 2" Then
Selection.Range.InsertAfter (h2Close)
End If
Next
' find eM dash and other special characters and replace with html characters
Set MyRange = ActiveDocument.Content
MyRange.Find.Execute FindText:="^+", _
ReplaceWith:="?", Replace:=wdReplaceAll
End Sub
I'm new to VBA, so if there's a better way to do any of this, I'm all ears. Or, if this has already been answered, please point me to the correct link. Thanks!
The articles contain default and Custom Word Styles. I am currently looping through all paragraphs, finding certain styles, then adding html code before and after the selected paragraph. What I'd also like to do is find formatted text within a paragraph (of only certain Styles) and change the formatted text to their corresponding html tags. Here's the code I have so far...
Public Sub Main()
Dim p As String
p = "<p class=""class1"">"
pClose = "</p>"
h2 = "<h2 class=""class2"">"
h2Close = "</h2>"
i = "<i>"
iClose = "</i>"
' loop through all paragraphs for opening paragraph tags
For Each paragraph In ActiveDocument.Paragraphs
paragraph.Range.Select
If paragraph.Style = "Normal" Then
Selection.Range.InsertBefore (p)
' MsgBox "paragraph selected"
End If
If paragraph.Style = "Heading 2" Then
Selection.Range.InsertBefore (h2)
End If
' within each paragraph, I need to find all Italic, Bold, etc. text and replace with tags
' how do I just search text that is currently selected?
Next
' for close tags
For Each paragraph In ActiveDocument.Paragraphs
paragraph.Range.Select
If paragraph.Style = "Normal" Then
Selection.Range.MoveEnd Unit:=wdCharacter, Count:=-1
Selection.Range.InsertAfter (pClose)
End If
If paragraph.Style = "Heading 2" Then
Selection.Range.InsertAfter (h2Close)
End If
Next
' find eM dash and other special characters and replace with html characters
Set MyRange = ActiveDocument.Content
MyRange.Find.Execute FindText:="^+", _
ReplaceWith:="?", Replace:=wdReplaceAll
End Sub
I'm new to VBA, so if there's a better way to do any of this, I'm all ears. Or, if this has already been answered, please point me to the correct link. Thanks!