Modify a doc with VBA

P

Patrick

Hello,

I would like to modify a document by read it line by line. According to the
contents of the line, I modify it or I remove it. The whole in VBA.

Do you know how to do that ?
Does it exist references on the Web ?

Thank you for your help.
 
C

Cindy M -WordMVP-

Hi =?Utf-8?B?UGF0cmljaw==?=,
I would like to modify a document by read it line by line. According to the
contents of the line, I modify it or I remove it. The whole in VBA.

Do you know how to do that ?
Yes... Although it would be important to know whether "line-by-line" means
literally that, or if each line would be a separate paragraph.
Paragraph-by-paragraph would be much easier to realize.

You should also tell us which version of Word you have.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 
P

Patrick

Hi,

I use Word 2002 (XP).
You're right : I mean "Paragraph-by-paragraph" because all line is a
paragraph.

Thank you.
 
C

Cindy M -WordMVP-

Hi =?Utf-8?B?UGF0cmljaw==?=,
I use Word 2002 (XP).
You're right : I mean "Paragraph-by-paragraph" because all line is a
paragraph.
OK, normally, one would do something like this:

Sub ModifyParas()
Dim doc as Word.Document
Dim para as Word.Paragraph

Set doc = ActiveDocument
For each para in doc.Paragraphs
'Put the code here to manipulate the paragraph
Next
End Sub

BUT, since you may delete the paragraphs, the internal indexing system that keeps
track of the Paragraphs collection might get mixed up, so you could end up missing
paragraphs when you loop. In situations like that, it's better to start at the end
and work your way to the front:

Sub ModifyAndDeleteParas()
Dim lNrParas as Long
Dim counter as Long
Dim doc as Word.Document
Dim para as Word.Paragraph

Set doc = ActiveDocument
lNrParas = doc.Paragraphs.Count
For counter = lNrParas to 1 Step -1
'Do the work here
Set para = doc.Paragraphs(counter)
If para... 'Test condition here
para.Range.Delete
Else

End If
Next
End Sub

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or reply in
the newsgroup and not by e-mail :)
 
P

Patricia Shannon

As Cindy said, when you're deleting things, you have to use .count and
indexing and work from the bottom.
The reason her first example is superior, when it can be used, is that (at
least in some cases), when you use indexing, VBA evidently starts at index 1
and counts up to the desired element, so as the indexes get bigger, it takes
longer to get to the desired element. I was doing something (can't remember
what) that way, and it never did finish. I left it running when I went home,
and it was still running the next morning! When I sent a message to the
status line at the beginning of each loop iteration, I saw what was
happening. It started out zipping along, and the further it got in the
document, the slower it got.
When I changed it to "For each ? in ?" , it finished quickly.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top