PDA

View Full Version : Replacing bold text, break line and paragraphs with html tags.



Wisniatt
04-16-2015, 11:17 AM
Hi,
I was trying to figure out how to write this macro in Microsoft word. I want to word to replace bold text, break line and paragraphs with html tags and then save it in txt.
I'll give you a example.

Word text:
Some example text
Some example text

Some example text


And after conversion:

<p>Some example text<br />
<b>Some example text</b></p>


<p>Some example text</p>



Thanks for any help :)

gmaxey
04-17-2015, 05:32 AM
Something like this:

Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim oRng As Word.Range
Set oRng = ActiveDocument.Range
With oRng.Find
.Font.Bold = True
While .Execute
oRng = "<b>" & oRng & "</b>"
oRng.Collapse wdCollapseEnd
Wend
End With
ActiveDocument.Range.InsertBefore vbCr
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = "[^11^13]*[^11^13]"
.MatchWildcards = True
While .Execute
oRng.MoveStart wdCharacter, 1
oRng.MoveEnd wdCharacter, -1
If oRng.Characters.Last.Next = Chr(13) Then
oRng = "<p>" & oRng & "</p>"
Else
oRng = "<br>" & oRng & "</br>"
End If
oRng.Collapse wdCollapseEnd
Wend
End With
ActiveDocument.Range.Characters(1).Delete
End Sub

Wisniatt
04-17-2015, 06:45 AM
Thank you very much!!
Everything work almost perfect, but i get this error:
1319713198

gmaxey
04-17-2015, 04:26 PM
I can't reproduce that error and it doesn't make much sense. The code line above that has just moved the range back one so .Last.Next is the character that was just part of the range. You will have to post your text.

gmaxey
04-18-2015, 06:02 AM
Try this:


Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim oRng As Word.Range
Set oRng = ActiveDocument.Range
With oRng.Find
.Font.Bold = True
While .Execute
oRng = "<b>" & oRng & "</b>"
oRng.Collapse wdCollapseEnd
Wend
End With
ActiveDocument.Range.InsertBefore vbCr
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = "[^11^13]*[^11^13]"
.MatchWildcards = True
While .Execute
oRng.MoveStart wdCharacter, 1
oRng.MoveEnd wdCharacter, -1
On Error GoTo lbl_Exit
If oRng.Characters.Last.Next = Chr(13) Then
oRng = "<p>" & oRng & "</p>"
Else
oRng = "<br>" & oRng & "</br>"
End If
oRng.Collapse wdCollapseEnd
Wend
End With
ActiveDocument.Range.Characters(1).Delete
lbl_Exit:
Exit Sub
End Sub

Wisniatt
04-18-2015, 06:19 AM
Works perfect! Thank you very much :)