I’m trying to define a range that refers to the Header of a section. No-brainer, right? But once I started trying to modify it, it’s going renegade on me! Specifically, in my code, when I’m trying to set a reference to the header of section 1, it empties the header!
Here is the pertinent part of the code. In this bit, the cursor is in the body of the document, not in the header itself.
[indent]Dim HeaderType As String, hRange As Range
Set hRange = Nothing
Activedocument.Sections(1).PageSetup.DifferentFirstPageHeaderFooter = False
Activedocument.Sections(1).Headers(wdHeaderFooterPrimary).LinkToPrevious = False
Set hRange = Activedocument.Sections(1).Headers(wdHeaderFooterPrimary).Range
[/indent]
Immediately after excecuting the line that starts with “Set”, the contents of the header disappear! (Also, when I halt the code execution, go to the document and “undo” that change, the whole document then becomes selected. I’m not sure what THAT’S about.)
Am I going about this wrong? I’m pretty sure I’ve used similar code many times before and I don’t remember this happening.
(P.S. I modified the egregious line of code to read Set hRange = ActiveDocument.StoryRanges(wdPrimaryHeaderStory), and that works OK. Still, I’d like to know if the previous version is actually wrong.)