Fill in a word whenever macro hits a blank header

A

andreas

Dear Experts:
Below macro is supposed to insert "hello" in all blank headers. It is
somehow not working. Why?
If I insert one (1) space in a header and alter the code line
(Len(....)) to ( If Len(hdr.range.Text) > 0 Then ..) the macro works.
But it does not work if the header is really blank. I find this very
strange.

Help is much appreciated. Thank you very much in advance. Regards,
Andreas

Sub Insert_Hello_In_Blank_Headers()
Dim rng As range
Dim sec As Section
Dim hdr As HeaderFooter
Dim fld As Field

For Each sec In ActiveDocument.Sections
For Each hdr In sec.Headers
If Len(hdr.range.Text) = 0 Then
hdr.range.Text = "hello"
End If
Next hdr
Next sec
End Sub
 
J

Jay Freedman

Dear Experts:
Below macro is supposed to insert "hello" in all blank headers. It is
somehow not working. Why?
If I insert one (1) space in a header and alter the code line
(Len(....)) to ( If Len(hdr.range.Text) > 0 Then ..) the macro works.
But it does not work if the header is really blank. I find this very
strange.

Help is much appreciated. Thank you very much in advance. Regards,
Andreas

Sub Insert_Hello_In_Blank_Headers()
Dim rng As range
Dim sec As Section
Dim hdr As HeaderFooter
Dim fld As Field

For Each sec In ActiveDocument.Sections
For Each hdr In sec.Headers
If Len(hdr.range.Text) = 0 Then
hdr.range.Text = "hello"
End If
Next hdr
Next sec
End Sub

A header that is "really blank" still contains a paragraph mark, so the value of
Len(hdr.range.Text) is 1, not 0.
 
A

andreas

A header that is "really blank" still contains a paragraph mark, so the value of
Len(hdr.range.Text) is 1, not 0.

--
Regards,
Jay Freedman
Microsoft Word MVP        FAQ:http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroupso all may benefit.- Hide quoted text -

- Show quoted text -

Hey Jay,

ok, did not know that. Thank you very much for your help. Regards,
Andreas
 

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