Solved

VBA for each paragraph add a some text before and after

Posted on 2010-08-26
4
4,401 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

Suggested Solutions

Title # Comments Views Activity
Word automation in Visual FoxPro 3 29
Outlook 2016 VB code syntax 4 38
Auto save MS Word - shortest intervals 10 24
VBA Lookup Problem 2 13
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
This article describes a serious pitfall that can happen when deleting shapes using VBA.
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
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…

738 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