wobbled
asked on
VBA for each paragraph add a some text before and after
Hi,
I am looping through all the paragraphs in a document. If there is a "¬" in the paragraph then I wish to add some xml mark up tags before and after that paragraph.
This should work simply enough, but I am just outputting thousands of my xml markers all in the first paragraph. I think that each time I am adding these items to the sheet that I am resetting the paragraph count or something so it is all going very strange.
My document has text in it like this:
text text ¬
other text other text other text
text text ¬
other text other text other text
other text other text other text
etc etc
I am looping through all the paragraphs in a document. If there is a "¬" in the paragraph then I wish to add some xml mark up tags before and after that paragraph.
This should work simply enough, but I am just outputting thousands of my xml markers all in the first paragraph. I think that each time I am adding these items to the sheet that I am resetting the paragraph count or something so it is all going very strange.
My document has text in it like this:
text text ¬
other text other text other text
text text ¬
other text other text other text
other text other text other text
etc etc
Public Const STR_XML_START As String = "<glossentry>"
Public Const STR_XML_END As String = "</glossentry>"
Public Const STR_XML_TERM As String = "<glossterm>"
Public Const STR_XML_TERM_END As String = "</glossterm>"
Public Const STR_XML_DEF As String = "<glossdef>"
Public Const STR_XML_DEF_END As String = "</glossdef>"
Sub SortItOut()
Dim p As Paragraph
Dim str As String
For Each p In ActiveDocument.Paragraphs
str = Trim(p.Range.Text)
If InStr(1, str, "¬", vbTextCompare) > 0 Then
p.Range.Text = STR_XML_START & vbCrLf & _
STR_XML_TERM & str & STR_XML_TERM_END & STR_VBCRLF & _
STR_XML_END
str = ""
End If
Next p
End Sub
ASKER
I thought that as well and had tried it without the carriage return in there, but again it just populates all the XML tags at the top of the document, rather than before and after each paragraph.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks for the help - such a simple thing, but these are the ones that often drive you mad.
I also played around with putting each paragraph over one character length 1 into an array and doing it that way. It works fine, but I thought I was over complicating matters.
I also played around with putting each paragraph over one character length 1 into an array and doing it that way. It works fine, but I thought I was over complicating matters.
p.Range.Text = STR_XML_START & vbCrLf & _
STR_XML_TERM & str & STR_XML_TERM_END & STR_VBCRLF & _
STR_XML_END
Also if you do not need the ¬ character you could remove it to avoid triggering the Instr repeatedly