Solved

VBA for each paragraph add a some text before and after

Posted on 2010-08-26
4
4,289 Views
Last Modified: 2012-05-10
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


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

Open in new window

0
Comment
Question by:wobbled
  • 2
4 Comments
 
LVL 2

Expert Comment

by:eoindevery
ID: 33538839
At a guess I would say that the vbCrLf is creating a new paragraph perhaps try the code without them.

 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
0
 
LVL 17

Author Comment

by:wobbled
ID: 33539776
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.

0
 
LVL 8

Accepted Solution

by:
PandaPants earned 500 total points
ID: 33542843
My read on this is the same as yours, that each time you add the tags you're adding a new paragraph to the collection of all paragraphs, so when you start at para(1) and add tags to the beginning of para(1), what had previously been para(1) is now para(2), and the paragraph you just added -- with the tags -- is para(1). So, the code moves to the end of para(1) and adds the "after" tags, then tries to move to the second paragraph in the collection, which is one of the tag paragraphs you just added...and so on.

Try working back-to-front instead. Start by counting all the paragraphs in the document, then moving to the end and placing the tags at the end of the last paragraph, then in front of the last paragraph, then at the end of the original second-to-last paragraph, and so on. In other words, move from para to para by number. Since you'll be adding paragraphs to the END of the collection, the total number of paragraphs will rise but the numbers -- indexes -- of the unmarked paragraphs won't change.

Something similar to this, using your code and modifying it slightly:

Public Sub WrapSomeParasWithText()
Dim p As Paragraph
Dim str As String
Dim iTotalParas As Integer, iParaCounter As Integer

    iTotalParas = ActiveDocument.Paragraphs.Count
    For iParaCounter = iTotalParas To 1 Step -1
'    For Each p In ActiveDocument.Paragraphs
        Set p = ActiveDocument.Paragraphs(iParaCounter)
        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
    Next iParaCounter

End Sub

Open in new window

0
 
LVL 17

Author Closing Comment

by:wobbled
ID: 33542888
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.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft Office Picture Manager was included in Office 2003, 2007, and 2010, but not in Office 2013. Users had hopes that it would be in Office 2016/Office 365, but it is not. Fortunately, the same zero-cost technique that works to install it with …
This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
Learn how to create and modify your own paragraph styles in Microsoft Word. This can be helpful when wanting to make consistently referenced styles throughout a document or template.

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question