Word Automation causes blank headers to appear

D

Daniel Kim

I've been writing software to automate Word for more than a year now using
Visual Studio, C++, and I've just recently run into a seeming bug... I
don't think this "bug" (if it's a bug) is C++ specific, so if you are using
other languages, you can still help... I am using msword9.olb type library
to automate it.

The problem is this: Even though I create a brand new document using word
automation, calling Section.GetHeaders() always seems to return headers!
Even though there are none.

In order to find out if this header is empty, I have to get the Range
Property on the HeaderFooter object by calling GetRange()... and this seems
to actually create the header (which then pushes down the text that happens
to be in the header margins). In effect, it seems that Word automation
creates blank headers simply by probing for a header.

Most of the time, this is unnoticed by the user, because the top margins are
usually 0.5" or more. But my customers first noticed the problem when they
set the top margin to 0.3" or less, and then when I automate Word, it
creates a blank header, thus pushing down the text.

My question is:
Is this a bug in MS Word automation library? Why would GetHeaders() always
return something rather than nothing? Is there any way to detect an empty
header and delete it?

By the way, I tried to get the Text property from the Range (gotten from the
HeaderFooter object) to see if it's empty, but because there's a return
character, my effort to see if the Text property returns a null also fails.

Any help would be greatly appreciated. Anyone who has run into this problem
before?

--Daniel
 
P

Peter Huang [MSFT]

Hi

From the VBA helper,
Headers Property
Returns a HeadersFooters collection that represents the headers for the
specified section. Read-only.

So you can detect the numbers in the collections to see if there is a
headers.

Based on my test for a new document, there will be three items in the
collection.

So it made sense that it always returned something, because returned a
collection of 0 items is OK.

Here is a VBA macro for your reference.
You may try to use to VBA for developping test, and then convert it into
C++ would speed the develop time.
Sub Test()
Dim s As Section
Dim h As HeadersFooters
Set h = ThisDocument.Sections(1).Headers
Debug.Print h(wdHeaderFooterPrimary).Exists
Debug.Print h(wdHeaderFooterFirstPage).Exists
Debug.Print h(wdHeaderFooterEvenPages).Exists
Debug.Print h(wdHeaderFooterPrimary).Range.Text
End Sub


BTW: The Office 2000 Mainstream Support Retired.
http://support.microsoft.com/lifecycle/?p1=2484

http://support.microsoft.com/gp/lifepolicy

Best regards,

Peter Huang

Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
D

Daniel Kim

Thank you for the reply.

So if I'm understanding you properly, Headers Property will ALWAYS return 3
items, even though I didn't create any headers?
If that is the case, then my problem persists... because I need to sort of
"probe" those headers to see if there are any fields in there. But the
moment I try to probe those headers by getting the Range Property, it
actually CREATES the empty header, which pushes down the text in the main
body. Like I mentioned, this problem is not noticed most of the time,
because the main body starts around 1" from the top. But when my users put
the main body text starting at 0.3" from the top, then my usage of Range
Property creates an empty header, pushing down the main body text... For
pages that fit exactly 1 page, you can see why this would be a problem.

Put in the context of what I'm saying, in the VBA example that you gave, the
my problem occurs in the last line: Debug.Print
h(wdHeaderFooterPrimary).Range.Text

What I'm observing is that this line (at least in C++) actually creates an
empty header, which takes up space, and pushes the main body down if the top
margin is less than 0.5".

Is this something that you're seeing on your side? (try making the top
margin 0.3", put in text at the very top of the page and see if running the
VBA macro pushes down the text)


One possible solution is for me to probe for the Range of the header (which
will create the header), and see if the Text returns a blank or a \r\n
character. Then if that's all there is, I can assume that it's an empty
header created by Word (which I consider to be a bug - it shouldn't create
empty headers just because I probe for the Range).. and erase it.

Question: Is there a way to erase a header?

Thank you very much for your help in this.

--Daniel
 
P

Peter Huang [MSFT]

Hi Daniel,

Headers means there are three kinds of Header or Footers.

So in my code I use the exist method to detect if certain kind of
header/footer exist.

Here is the information from the VBA document.
Exists Property
See AlsoApplies ToExampleSpecificsTrue if the specified HeaderFooter object
exists. Read/write Boolean.

Note The primary header and footer exist in all new documents by default.
Use this method to determine whether a first-page or odd-page header or
footer exists. You can also use the DifferentFirstPageHeaderFooter or
OddAndEvenPagesHeaderFooter property to return or set the number of headers
and footers in the specified document or section.

If we run the code I post in the my previous post, we will get two False
and a True result.
That means the primary header and footer exist, which matched the
information in the document above.

Based on my test, I can not reproduce the problem.
Sub Test()
'I also try to set the TopMargin manually, the result persists.
ThisDocument.PageSetup.TopMargin = InchesToPoints(0.3)

Dim s As Section
Dim h As HeadersFooters
Set h = ThisDocument.Sections(1).Headers
Dim hh As HeaderFooter
Set hh = h.Item(wdHeaderFooterPrimary)

Debug.Print PointsToInches(ThisDocument.PageSetup.TopMargin) '0.3

Debug.Print h(wdHeaderFooterPrimary).Exists
Debug.Print h(wdHeaderFooterFirstPage).Exists
Debug.Print h(wdHeaderFooterEvenPages).Exists
Debug.Print h(wdHeaderFooterPrimary).Range.Text
Debug.Print PointsToInches(ThisDocument.PageSetup.TopMargin) '0.3
End Sub

Also you may take a look at the code below, we can delete a header's range.
http://wordtips.vitalnews.com/Pages/T1453_Deleting_All_Headers_and_Footers.h
tml

I test the code at Word 2003, as I said before the Office 2000 product have
retired its mainstream support.
If the issue is urgent, you may want to work with Microsoft Customer
Service and Support (CSS) for a faster resolution. Once you open a Support
incident with Microsoft CSS, a dedicated Support Professional can work with
you in a more efficient manner.

If you still have any concern, please feel free to post here.

Best regards,

Peter Huang

Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
D

Daniel Kim

Hello Peter

Thank you so much for this detailed reply. People like you are the reason
why I enjoy working with MSDN.
Here is the information from the VBA document.
Exists Property
See AlsoApplies ToExampleSpecificsTrue if the specified HeaderFooter
object
exists. Read/write Boolean.
Note The primary header and footer exist in all new documents by default.

Well, if that's the case, then I don't know how that could help me. I need
a way to tell if the primary header exists or not.
Based on my test, I can not reproduce the problem.

That's expected, the way you tested for it.. If I test for the resultant
top margin programmatically, it gives you the right results for me, too. I
actually save the word file, and then I go in and open the word file. When
I go to Page Setup, the top margin still says 0.3"! But visually, it's
obviously not 0.3" anymore. The first line has been shifted down visually.
Then I double-click on the "header space", which pops up the thin floating
toolbar for editing headers, and then when I close that toolbar (without
doing anything), THEN the header really disappears, and the first line is
where it should be. Strange, huh?

I'm sorry, I'm too heavily invested into C++ that I don't really have the
means to test out the VB code.. Is there an easy way? (through Word,
perhaps?)... so for now, I'm probably frustrating you with the language
barrier btw C++ and VB.

However, in order to actually reproduce what I'm talking about, I would add
3 more items onto your test code:

1) Put in a line of text at the top of the document: "Hello World" (which
shoudl be at 0.3" from the top)

2) After "Set hh = h.Item(wdHeaderFooterPrimary)" line, you need to actually
get the Range for the header... (from my debugging, I have isolated this
action as the culprit).
Maybe with a line that looks like: "Set rh = hh.Range( ...)"
Sorry, I am not a VB person, so I'm just guessing about the syntax above.

3) Instead of trying to programmatically print out the top margin, have the
VB code save the Word file. Then open up the file using MS Word, then take
a look at where "Hello World" is...


If you are too busy to follow through on this plan, please let me know.. at
least I can try to figure out how to run VB code myself so that I can run
these tests myself...

Thank you so much, Peter.

--Daniel
 
P

Peter Huang [MSFT]

Hi Daniel,

I am sorry if I have any confusion.

1. From the VBA Help file,
Note The primary header and footer exist in all new documents by default.

That is to say, the primary header will exist in all new document and I
navigate through all the Object Modal there is no such a method to delete a
header. So far the only workaround is to use the method below to clear the
content in the header range.
http://wordtips.vitalnews.com/Pages/T1453_Deleting_All_Headers_and_Footers.h
tml

2. The code I show is the VBA code which is the buildin code for Office
product. We did not need to install additional tool. Just Press Alt+F11, we
will get a VBA Editor to write and run VBA code. The reason why I stick to
use VBA code is that, for any other language, we are using Automation code
to manipulate the Word Object Modal, but there are still something possible
there existed in the Code itself or the layer that other language call Word
Library. But for the VBA code, it is the buildin code for Word. Almostly if
our VBA code is correct, then we can make sure the problem is for the Word
itself but not the language.

Based on my test per your suggestion, I can reproduce your scenario, that a
watermark line will occur if you access Header,Range and we can not delete
it by code.

So far I have reported the problem to our product group, if you still have
any concern, please feel free to post here.

Thanks!


Best regards,

Peter Huang

Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
D

Daniel Kim

Based on my test per your suggestion, I can reproduce your scenario, that
a
watermark line will occur if you access Header,Range and we can not delete
it by code.

So far I have reported the problem to our product group, if you still have
any concern, please feel free to post here.

Oh, that's great (in a sense). So that means at least I know that this
problem is on the part of MS word. Is there anything that I can do, then?
Can I follow up with the problem report? What are the options open to me?
What is the likelihood that this bug will be fixed in the next Word patch,
for example?

Thank you again,

--Daniel
 
P

Peter Huang [MSFT]

Hi Daniel,

I do understand your concern in this scenario, I highly suggest you can
submit this feedback to our product feedback center:
http://lab.msdn.microsoft.com/productfeedback/default.aspx

Best regards,

Peter Huang

Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 

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