Hi Greg,
Thanks for your reply.
Yes I was referring to the skipped header problem.
I had tried that method (Loop with rngStory.NextStoryRange) previously and
wasn't able to make it reliable for all of the circumstances it needs to be
relied on for my purposes.
Encouraged by your reply, I hacked it today (word 2003) thinking maybe
things are different before (word2000), but couldn't make your code work
without it creating the unwanted header/footer when the header/footer did
not pre-exist.
I thought maybe by counting the fields (commented line below) I might work
around but merely counting the fields also creates the empty header.
Here's your code modified? ('I added these 2 lines "'If
rngStory.Fields.Count > 0 Then rngStory.Fields.Update" and
"rngStory.Fields.Update")
Sub ScratchMacro()
Dim rngStory As Word.Range
Dim lngJunk As Long
Dim i As Long
'Deal with the potential skipped blank Header/Footer problem
lngJunk = ActiveDocument.Sections(1).Headers(1).Range.StoryType
'Iterate through all story types in the current document
On Error Resume Next
For Each rngStory In ActiveDocument.StoryRanges
Do
Select Case rngStory.StoryType
Case 6, 7, 8, 9, 10, 11
'Case 8, 9, 11
'If rngStory.Fields.Count > 0 Then rngStory.Fields.Update
rngStory.Fields.Update
Case Else
'Do Nothing
End Select
On Error GoTo 0
'Get next linked story (if any)
Set rngStory = rngStory.NextStoryRange
Loop Until rngStory Is Nothing
Next
On Error GoTo 0
End Sub
Can you make that work without creating unwanted header/footer where did not
pre-exist?
And here's my modified version (also word 2003) which exposes the skipped
header/footer problem, but doesn't create the unwanted empty header/footer
because in my test doc, that story doesn't exist for the first section).
(as observed with Word2K, Word stops searching subsequent sections for story
ranges that did not exist in previous sections. AAARGH)
Sub ScratchMacro()
Dim rngStory As Word.Range
Dim i As Long
'AS1.InitWSEnvAV193
'AS1.SetDMSPArrayAV030
'Iterate through all story types in the current document
On Error Resume Next
For Each rngStory In ActiveDocument.StoryRanges
Do
Select Case rngStory.StoryType
Case 6, 7, 8, 9, 10, 11
UpdateOurFields rngStory
Case Else
'Do Nothing
End Select
On Error GoTo 0
'Get next linked story (if any)
Set rngStory = rngStory.NextStoryRange
Loop Until rngStory Is Nothing
Next
On Error GoTo 0
End Sub
Function UpdateOurFields(rng As Range)
Dim ofield As Field
For Each ofield In rng.Fields
If isFieldOurs(ofield.Code.Text) Then ofield.Update
Next ofield
Set ofield = Nothing
End Function
Function isFieldOurs(sCode As String) As Boolean
'for these tests just look for one property
'Dim i As Integer
'For i = 1 To UBound(AS1.sDMSpAV679)
If InStr(1, LCase(sCode), "dmsdocid") And _
InStr(1, LCase(sCode), "docproperty") Then
'If InStr(1, LCase(sCode), LCase(sDMSpAV679(i, 1))) And _
' InStr(1, LCase(sCode), "docproperty") Then
isFieldOurs = True
Exit Function
End If
'Next i
End Function
Is there any way to get the best of both?
Here's my scenario:
3 sections (all headers and footers are unlinked from previous sections).
All sections type "Next page". All sections 3 pages in length.
Section 1: Different first page header/footer. 1st page headers &
footer are empty. Primary ones contain fields.
Section 2: NOT Different fist page header: Primary headers/footers
both have fields.
Section 3: Same as section 1 except that the first page
headers/footers are NOT EMPTY. they contain fields.
Running my code, 1st page header/footer in section 3 get missed (as
expected).
The other problem I have using the story range collection to update the
footer fields is that I need to update headers/footers that might not exist
because of section length (e.g. section setup indicates different first page
footer applies but section text is currently only one page in length -
therefore using the story ranges collection skips the primary
header/footer). The footer needs updated in that circumstance so that if as
a result of editing, document expands to two pages, the footers is correct
without user intervention. Any way around that using the story ranges
collecton.
I sure would like to replace what I have with a method that is
fast,
reliable,
"quiet",
updates specific fields only, and
never creates unwanted empty headers/footers.
Seeking view is out of the question because my procedure runs automatically
as documents are opened and that is too slow as well as disruptive to the
user.
I we could access a story ranges collection PER section rather than Per
Document, that would probably do the trick
Back to Jeff's challenge: If he doesn't need to be selective as to which
fields to update, then forcing preview is probably the easiest and most
reliable way to get the job done.
I'm less hopeful of the existence of a resoluton to my challenge because of
the need to selectively update fields.
If we could get your code to work without creating unwanted empty
header/footer, I would adjust my expectations and accept that the code will
update ALL fields in footers, not just my chosen fields.
Any Hope?
Thanks!
regards,
Julie