Loop thru doc problem

J

jbc

Hi,

The following code is working perfectly for me except when it hits a table.
I would like it to SKIP tables and not put the sequential number field there.
I only want it if the paragraph style is Normal and not in a table.

Right now it is placing sequential numbers in each cell of the first table.
I'm getting the following error message when it gets to the end of the first
row:

Run-time error 5251. This is not a valid action for the end of a row.

I've tried several different options to get this to work. I've been looking
at it all day and am still not getting anywhere.

Any help would be greatly appreciated.

Thanks.

jbc
*********************************************
Dim oPar As Paragraph
Dim oRng As Range

ActiveWindow.View.ShowFieldCodes = True

For Each oPar In ActiveDocument.Paragraphs

If oPar.Style = "Normal" Then
If Not Mid(oPar.Range.Text, 3, 3) = "SEQ" Then

oPar.Range.InsertBefore vbTab
Set oRng = oPar.Range.Duplicate

oRng.Collapse direction:=wdCollapseStart
ActiveDocument.Fields.Add Range:=oRng, Type:=wdFieldSequence,
Text:= _
"Paragraph \# ""[0000]"" \n "

oRng.Paragraphs(1).Range.Fields(1).Result.Font.Bold = True

End If
End If

Next oPar

ActiveWindow.View.ShowFieldCodes = False
ActiveDocument.Fields.Update
*************************************************
 
J

Jay Freedman

Replace the line

If oPar.Style = "Normal" Then

with

If (oPar.Style = "Normal") And _
(Not oPar.Range.Information(wdWithInTable)) Then
 
J

jbc

Jay,

Thank you so much. I tried something very similar, but did not use the ( ).
Why do you need them?

Thanks.

jbc

Jay Freedman said:
Replace the line

If oPar.Style = "Normal" Then

with

If (oPar.Style = "Normal") And _
(Not oPar.Range.Information(wdWithInTable)) Then

--
Regards,
Jay Freedman
Microsoft Word MVP

Hi,

The following code is working perfectly for me except when it hits a table.
I would like it to SKIP tables and not put the sequential number field there.
I only want it if the paragraph style is Normal and not in a table.

Right now it is placing sequential numbers in each cell of the first table.
I'm getting the following error message when it gets to the end of the first
row:

Run-time error 5251. This is not a valid action for the end of a row.

I've tried several different options to get this to work. I've been looking
at it all day and am still not getting anywhere.

Any help would be greatly appreciated.

Thanks.

jbc
*********************************************
Dim oPar As Paragraph
Dim oRng As Range

ActiveWindow.View.ShowFieldCodes = True

For Each oPar In ActiveDocument.Paragraphs

If oPar.Style = "Normal" Then
If Not Mid(oPar.Range.Text, 3, 3) = "SEQ" Then

oPar.Range.InsertBefore vbTab
Set oRng = oPar.Range.Duplicate

oRng.Collapse direction:=wdCollapseStart
ActiveDocument.Fields.Add Range:=oRng, Type:=wdFieldSequence,
Text:= _
"Paragraph \# ""[0000]"" \n "

oRng.Paragraphs(1).Range.Fields(1).Result.Font.Bold = True

End If
End If

Next oPar

ActiveWindow.View.ShowFieldCodes = False
ActiveDocument.Fields.Update
*************************************************
 
J

Jay Freedman

The VBA interpreter doesn't "need" the parentheses that separate the
arguments of the And operator unless the standard order of operator
evaluation would be different from the intended logic -- which it isn't, in
this case. But I need them to remind me of how to read the statement (slow
meatware!).

The parentheses around wdWithInTable are necessary because I want to use the
result of the Information function -- see
http://word.mvps.org/FAQs/MacrosVBA/BracketsWithArgmnts.htm.

--
Regards,
Jay Freedman
Microsoft Word MVP
Jay,

Thank you so much. I tried something very similar, but did not use
the ( ). Why do you need them?

Thanks.

jbc

Jay Freedman said:
Replace the line

If oPar.Style = "Normal" Then

with

If (oPar.Style = "Normal") And _
(Not oPar.Range.Information(wdWithInTable)) Then

--
Regards,
Jay Freedman
Microsoft Word MVP

Hi,

The following code is working perfectly for me except when it hits
a table. I would like it to SKIP tables and not put the sequential
number field there. I only want it if the paragraph style is Normal
and not in a table.

Right now it is placing sequential numbers in each cell of the
first table. I'm getting the following error message when it gets
to the end of the first row:

Run-time error 5251. This is not a valid action for the end of a
row.

I've tried several different options to get this to work. I've
been looking at it all day and am still not getting anywhere.

Any help would be greatly appreciated.

Thanks.

jbc
*********************************************
Dim oPar As Paragraph
Dim oRng As Range

ActiveWindow.View.ShowFieldCodes = True

For Each oPar In ActiveDocument.Paragraphs

If oPar.Style = "Normal" Then
If Not Mid(oPar.Range.Text, 3, 3) = "SEQ" Then

oPar.Range.InsertBefore vbTab
Set oRng = oPar.Range.Duplicate

oRng.Collapse direction:=wdCollapseStart
ActiveDocument.Fields.Add Range:=oRng,
Type:=wdFieldSequence, Text:= _
"Paragraph \# ""[0000]"" \n "

oRng.Paragraphs(1).Range.Fields(1).Result.Font.Bold = True

End If
End If

Next oPar

ActiveWindow.View.ShowFieldCodes = False
ActiveDocument.Fields.Update
*************************************************
 

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