Strange oHeader Behavior

J

JeffP->

Updating to Office 12 has introduced some new odd behavior. Some of my doc's
have header content others dont', so for example... In a doc that has NO
header content and two merge fields at the top of the doc Name & Policy
number which are updated via a legacy addin using DDE. As I debug and step
through with show paragraph marks on in the doc, I can see nothing above my
two merge fields, however at the following line which will subsequently
iterate through any fields in the header and update them....
For Each oHeader in oSection.Headers --- Instantly there is now Times New
Roman 12pt carriage return where there was nothing, which wacks pagnation and
advancing page breaks etc.

I get the default font, but not why it's inserted into the header. I added a
1pt period in the header, prints like a spec of dust, but I have well over
500 templates! Rather than insert "fix" each doc, I'd prefer to fix my addin
code.

Any clues?
 
J

JeffP->

Apparently no one has seen this behavior, well if you want to see it, here's
how....

Create a module Mod_01 (anyname will do), paste in the code below. Save
into normal and create a doc that has "Hello" on the first line, adjust the
top margin to .5", you don't need any fields.

Then open your doc and run the following you'll have a nice Times New Roman
12pt line inserted into your header. A work around is to toggle the view,
cheeseie but it works.

Sub Open_Doc()

Dim oField As Field
Dim oSection As Section
Dim oHeader As HeaderFooter

For Each oSection In ActiveDocument.Sections

For Each oHeader In oSection.Headers
If oHeader.Range.Fields.Count > 1 Then
For Each oField In oHeader.Range.Fields
oField.Update
Next oField
End If
Next oHeader

Next oSection

'cure is to toggle the view. Don't you just love it....
'ActiveWindow.View.Type = wdPrintPreview
'ActiveWindow.View.Type = wdPrintView

End Sub

JeffP....
 
J

Julie

Hi Jeff,

THAT sounds like a problem that isn't new to Office 12. I can make it
happen with Word 2K, XP & 03.

The problem, in a nutshell, is that programmatically evaluating the range of
a header or footer will activate that header or footer if it wasn't active
before. The result is that after the operation, the header will be created
with an empty paragraph of header/footer style. I call it a 'bug".

You will have to work around, and the choice of workaround depends on your
situation.

Regards,
Julie
 
J

JeffP->

Julie,

Yes it does occur in earlier versions of Word. The actual change to Word
2007 is not having links auto-update on opening due to chronic prompting to
update links and a decision by IT not to lower security. I may try a trusted
selfcert for all our templates (over 500). I wonder if these were all
converted to Word 2007 docs (currently 97-03) that there a way I could
programmatically (.Net) update the doc's with the selfcert.... ?

The document fields should always be refreshed. My attempted solution is to
run the sub routine conditionally which at the end, changes the view to
PrintPreview & back to PrintView trying to clear the mark worked a few
times... wish me luck.

TIA

JeffP...
 
J

Julie

Hi Jeff,

Based on your information, I think you are on the best track for your
task -- force print preview.

If that doesn't work out for you, and IF your documents have one section
only then you might be able to work around by testing header content for
fields by using the header story ranges collection. That will invoke error
when a header doesn't exist, so you can trap that error to determine whether
a header exists.

On Error GoTo errh
If ActiveDocument.StoryRanges(wdFirstPageHeaderStory).Fields.Count > 0 Then
'set ranges and update fields
End If
Exit Sub
errh:
'if we get here, header didn't exist

That method doesn't create the unwanted header, but it isn't reliable if
there are multiple sections in the document.

This (relying on an error trap) is contrary to what some VBA experts assert
is "proper" coding but occasionally I find that relying on an error trap is
the only practical way to handle a task. You be the judge in the context of
your application!

It's interesting what you mention about 2007. I'm still developing pretty
much exclusiving in '03 but keeping a close eye on Word 2007 "happenings",
so its very valuable to hear your experience. Thanks for sharing that.

Wishing you luck. Please post back to let us know what worked for you.

Regards,
Julie
 
G

Greg Maxey

Julie,

This isn't tested at all, but am am thinking that it should deal witht the
issue: Is the potential skipped header/footer the source of the
unreliability you mention?


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
If rngStory.Fields.Count > 0 Then
'set ranges and update fields
End If
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
 
J

Julie

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
 
G

Greg Maxey

Julie,

AFAIK, there is no way to "decisively" loop through all headers and footers
without creating that indestructible paragraph mark. I am not sure why you
don't want it there, but it is possible to hide it from view:

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
If Len(rngStory) = 1 Then
rngStory.Font.Hidden = True
End If
'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
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
Application.ScreenRefresh
End Sub
 
G

Greg Maxey

Julie,

Is there some reason that you can use the PrintPreview to suppress the
paragraph mark in the empty headers or footers?


Sub ScratchMacro()
Dim myView As Long
Dim rngStory As Word.Range
Dim lngJunk As Long
myView = Application.ActiveWindow.View
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
If rngStory.Fields.Count > 0 Then
'set ranges and update fields
End If
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
Application.ActiveWindow.View = wdPrintPreview
Application.ActiveWindow.View = myView
End Sub
 
J

Julie

Thanks Greg. My reason for not wanting the "indestructible" paragraph mark
is that sometimes its insertion will affect pagination and it wouldn't be
kind of me to run that at document opening time. Users might revolt!!
Thanks, Julie
 
J

Julie

Hi Greg,

I'm definitely considering that (Print Preview method)!

Reason for reluctance: My code runs automatically as documents are opened
from DocMgr, so would like to avoid updating any field that Word doesn't
itself normally update upon document opening (other than, of course, my own
{DocProperty} fields!). My code also runs upon save of new DM document or
new DM version -- a time when user wouldn't expect a general field update
to happen -- so I'm little relucant about using the PrintPreview method at
that time as well. I'm thinking its worth a try to see how things flush
out so I will do it and if things look good will post back here to report.

Thanks for your help
Regards,
Julie
 
J

Julie

Hi Greg,

Just to complete the thread here, I took time to investigate the print
preview idea using a newer system not yet in production for DM users.

And I found a note to myself dated 2006 that I had rewritten the DM field
update function to try a new method to get the update the DM {DocProperty}
fields. That is, forcing Print Layout view, which for my purposes is no
different than forcing Print Preview.

'1. Walk the fields in the main document story and update DM DocProperty
fields (switch to print layout or print preview doesn't cut it)
'2. Force option to updatefields at print
'3. turn off screen updating
'4. force a switch to print layout view (same effect as print preview
as to my requirements)
'5. switch to whatever view was active to start
'7. restore screen updating and the restore original setting for update
fields at print
8. There's a screen refresh at the very end of the document opening
procedure as a whole, so don't require it here

That system has been sitting around for a couple of years and is about to go
to pilot, so "decision time" is now.

The problem though, as FEARED, this method updates ASK and FILLIN fields, so
I will have to re-think. All of my clients are "WordPerfect converts" so
there is a real possibility that merge documents improperly converted from
WordPerfect may contain those fields. However, in Year 2008, the odds of
encountering that are relatively small compared to the odds of seeing it in
year 1999, so possibly the benefit (no skipped footers) may outweigh the
risk.

The system I'm about to pilot is for a small firm (100 users). I'm not
likely to be so intrepid for a larger firm where it could be dangerous to
play those odds. The story ranges method remains in use for my existing
production systems, and that falls back on automation to force the setting
update-fields-at-print at every possible opportunity. To some degree,
that's an odds game as well, and so far in seven years with a few thousand
users, no-one has ever reported it to be a problem. Still though, I'd like
to close the loophole eventually, if ever possible.

I'm using Word 2003 now, and would be interested to know if Word 2007
updates the ASK and FILLIN fields upon switch to Print Layout View or Print
Preview. My Word 2007 PC is off line now so cannot test that. Do you know?

So unless you know if there is any way to keep those pesky ASK and FILLIN
fields from updating automatically (Word 2003) with a switch to Print Layout
view or Print Preview, I will stop flogging this dead horse.

Thanks very much for your input and interest in this thread.

Regards,
Julie
 

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