Solved

VBA for each paragraph add a some text before and after

Posted on 2010-08-26
4
4,155 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
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.

791 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