Solved

VBA for each paragraph add a some text before and after

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
PowerPoint Links 7 42
Excel Hyperlink Question 4 48
How to get many filtered in Excel 2010? 7 31
Error with a code discussed on this page 5 12
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Recently Microsoft released a brand new function called CONCAT. It's supposed to replace its predecessor CONCATENATE. But how does it work? And what's new? In this article, we take a closer look at all of this - we even included an exercise file for…
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …
This Micro Tutorial well show you how to find and replace special characters in Microsoft Word. This is similar to carriage returns to convert columns of values from Microsoft Excel into comma separated lists.

786 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