cutting content in word document

V

Vagabound_s

Hi,

I am trying to create a macro which will identify and list the cutting
contents in a word document by employing following tasks:
1) seach for all instances of negative paragraph indent and list the
references in a list box
2) search for tables with HeightRule = wdRowHeightExactly so as to check the
instances where text is wraping below the row height
3) search for all instances of text going beyound page margins due to
incorrect tab stop

I tried using "find" for above tasks, however unable to get it work, since i
could not write condition such as ActiveDocument.Paragraphs(i).LeftIndent <
0
I am sure if it works it will be most efficient

Finally I got the first two tasks work using loop, However it is very
inefficient and takes long time. The code are listed below:

Sub findNegativeIndent()
For i = 1 To ActiveDocument.Paragraphs().Count
If ActiveDocument.Paragraphs(i).LeftIndent < 0 Then
UserForm1.NegativeList.Visible = True
UserForm1.NegativeList.Enabled = True
UserForm1.NegativeList.AddItem (i)
End If
Next
End Sub

Sub FindRowHeight()
For i = 1 To ActiveDocument.Tables().Count
If ActiveDocument.Tables(i).Rows.HeightRule = wdRowHeightExactly And
ActiveDocument.Tables(i).Rows.Height = InchesToPoints(0) Then
UserForm1.IndentList.Visible = True
UserForm1.IndentList.Enabled = True
UserForm1.IndentList.AddItem (i)
End If
Next
End Sub

Does anyone have better idea?
 
G

George Lee

Use the for each loop:

Sub findAnotherNegativeIndent()
Dim i As Long
Dim myPara As Paragraph
For Each myPara In ActiveDocument.Paragraphs
If myPara.LeftIndent < 0 Then
End If

i = i + 1
StatusBar = i '& " of " & ActiveDocument.Paragraphs.Count
DoEvents
Next
End Sub

The I counter is just to be able to see the numbers fly by; the final
version of the macro can delete this.

Some observations about your code:
It’s a good idea especially during debugging to include a DoEvents
statement. This allows you to interrupt the loop without having to crash the
application.

It’s also unclear through the code what Userform1 is but if
UserForm1.NegativeList.Visible and UserForm1.NegativeList.Enabled just keeps
setting the same item over and over, get these out of the loop; use a flag or
an additional if statement.

Each dot in a qualifier (such as ActiveDocument.Paragraphs(i).LeftIndent)
adds some overhead to the macro. Consider shortening it like SET MYOBJECT =
ActiveDocument.Paragraphs(i) and then access it as .LeftIndent or .Count. If
you want to see a difference, remove the comment mark from the the Status bar
line.

Finally, consider not using ActiveDocument more than once in a routine, like
SET CURRENT_DOCUMENT = ActiveDocument. If the focus changes when the macro is
running you may get a new active document. This is common with bored users
during a long routine or even if users get mail and has Outlook using the
Word editor. In this case CURRENT_DOCUMENT, will always refer to the document
it was initially set to.
 
V

Vagabound_s

Thanks so much for your tips. I did see significant change in efficiency

I think tips such as these are worth being on the home page of MVP forums
since for a naive programmer such a me it is a very big step forward. I had
a feeling similar to the day when I first started learning macro programing
by recording a macro.
 

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