Bolding seq field codes when looping thru doc

J

jbc

Hi,

I'm trying to automate the process of putting sequential numbers into a
document when the paragraph is normal. The following code is doing exactly
what I need, but I need the fields to be inserted with bold formatting. I've
tried all kinds of combinations, but word is not cooperating.

Dim oPar As Paragraph
Dim oRng As Range

ActiveWindow.View.ShowFieldCodes = True
For Each oPar In ActiveDocument.Paragraphs

If oPar.Style = "Normal" Then
'check if field code is there, if not add it
If Not Mid(oPar.Range.Text, 3, 3) = "SEQ" Then

oPar.Range.InsertBefore vbTab
Set oRng = oPar.Range.Duplicate
ActiveDocument.Fields.Add Range:=oRng, Type:=wdFieldSequence,
Text:= _
"Paragraph \# ""[0000]"" \n "
End If
End If

Next

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


Thanks for your help.

jbc
 
G

Greg

jbc,

Does this work?

Sub Test1()
Dim oPar As Paragraph
Dim oRng As Range

ActiveWindow.View.ShowFieldCodes = True
For Each oPar In ActiveDocument.Paragraphs
If oPar.Style = "Normal" Then
'check if field code is there, if not add it
If Not Mid(oPar.Range.Text, 3, 3) = "SEQ" Then
Set oRng = oPar.Range.Duplicate
oRng.Collapse Direction:=wdCollapseStart
oRng.InsertBefore vbTab
oRng.Font.Bold = True
ActiveDocument.Fields.Add Range:=oRng, _
Type:=wdFieldSequence, Text:="Paragraph \# ""[0000]"" \n "
End If
End If
Next
ActiveWindow.View.ShowFieldCodes = False
ActiveDocument.Fields.Update

End Sub
 
H

Helmut Weber

Hi jbc,

like
[snip]
Set oRng = oPar.Range.Duplicate
oRng.Font.Bold = True
[snip]

Greetings from Bavaria, Germany

Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
http://word.mvps.org/
Keep your VBA code safe,
sign the ClassicVB petition www.classicvb.org
 
J

jbc

Hi,

I thought the same thing also, but the rest of the paragraph turns bold
including the field number. I tried setting bold to false after the SEQ
number is inserted and the same thing happens.

Does the SEQ number have to considered a range? I'm not sure how to have
VBA select it.

Thanks.

jbc
 
H

Helmut Weber

Hi jbc,
if I step through the suggested code in single step mode,
first all of the paragraph is bolded, but immediately
afterwards with inserting the field, the bold from all
of the paragraph vanishes again and only the field stays
bolded. However, there are alternatives, like:

[snip]
oPar.Range.InsertBefore vbTab
Set oRng = oPar.Range.Duplicate
ActiveDocument.Fields.Add Range:=oRng, _
Type:=wdFieldSequence, _
Text:="Paragraph \# ""[0000]"" \n "
oRng.Paragraphs(1).Range.Fields(1).Result.Font.Bold = True '!
[snip]

Greetings from Bavaria, Germany

Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
http://word.mvps.org/
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
 

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

Similar Threads


Top