Solved

VBA for each paragraph add a some text before and after

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Preface: When I started this series, I used the term CommandBars because that is the Office Object class that it discusses. Unfortunately, when Microsoft introduced Office 2007, they replaced the standard Commandbar menus with "The Ribbon" and rem…
This article describes how to use the Send to Mail Recipient command. The instructions apply generally to Office 2007 and later versions, but Microsoft® Word 2013 was used for the specific steps and figures.  What is Send to Mail Recipient? Send…
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now