I've have posted and been polishing code for a comprehensive find and replace Word add-in for several years. The current published verion is located here: http://gregmaxey.mvps.org/word_tip_p...d_replace.html
Yesterday I learned that when the process is executed it introduces an empty paragraph into headers and footers that were previously blank.
Steps to produce problem.
1. Download and install the add-in
2. Open an new Word document and type in a word or two.
3. Call the add-in and use it to find one of the typed words.
4. When finished the previously "empty" header and footer now contains an empty paragrph.
I'm not sure what, if any, issues this may cause in printing documents with complex layouts, and I would like to understand the cause and find a solution.
The cause appears to be the execution of a line of code that I have long understood is required to process linked header and footer storyranges and prevent an "empty" header or footer from breaking the process. This is explained here with the help of people far smarter than me:
http://word.mvps.org/faqs/customizat...ceAnywhere.htm
The line of code in my addin is located in the "Private Sub ProcessStoryTypes(ByVal oDocToProcess As Word.Document)" procedure of the UserInterface form and reads:
lngJunk = oDocToProcess.Sections(1).Headers(1).Range.StoryType
Can anyone explain why setting a long variable equal to a storytype value results in the physical change in the header (i.e., going from blank/empty to containing an empty paragraph)?
While I don't understand the physical change, I think I will have to a) live with the fact that it occurs or b) accept that header and footer storyranges may not be fully processed if empty header/footers are involved.
Option b being unacceptable, I did some further tinkerings and found that by simply toggling PrintPreview I can eliminate the empty paragraph and restore the header back to "empty." Once again being a tinkerer, I don't understand why this does what it does. Can anyone explain?
To work around this problem I added a new variable bGhost to my process and then modified the code to check for multiple sections and only invoke the lngjunk line if they are present:
[VBA]bGhost = False
If oDocToProcess.Sections.Count > 1 Then
lngJunk = oDocToProcess.Sections(1).Headers(1).Range.StoryType
bGhost = True
End If[/VBA]
Then if invoked, I toggle print preview:
[VBA]lbl_Exit:
If bGhost = True Then
CleanupGhostHeader oDocToProcess
End If
[/VBA]
[VBA]Sub CleanupGhostHeader(ByRef oDoc As Word.Document)
oDoc.PrintPreview
oDoc.ClosePrintPreview
End Sub
[/VBA]
The revised code is contained in the attachment (sorry can't attach a .dotm file).
Would appreciate any insight/technical explaination anyone has on 1) Why is the lngjunk line really required, 2) Why does toggling print preview make the empty paragraph disappear, 3) A better way to resolve the issue.
Thanks.