• Setting a range empties the range?

    Author
    Topic
    #466365

    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.)

    Viewing 2 reply threads
    Author
    Replies
    • #1207865

      I don’t know why your Set hRange line wipes the content but I am surprised the line before it doesn’t error. The header of Sections(1) can’t link to Previous since there is no previous section. The interface normally disables the link to previous option when you are in the first section and I thought that your code would error if you forced this on the first section. Is it possible that line is the cause of what you see happening at the next line?

    • #1207874

      We don’t have the benefit of seeing the document this code is acting upon, but there is a scenario where the result you describe, could be caused by this code:

      If you’re starting with a document which has already been set up in Page Layout as having ‘Different first page’, and some content has been put into the first page header but not into the continuation page header, then the line –

      Activedocument.Sections(1).PageSetup.DifferentFirstPageHeaderFooter = False

      would make the contents of the first page header disappear. Any chance something like that is going on? (which would also beg the question, why are you changing that setting via code, at that moment, anyway?)

      Gary

    • #1207999

      I think that’s it exactly. I have been testing this out on a document that did have “DifferentFIrstPage” turned on, and the header contents were thus on that page.

      The reason that these changes are being ensured, if not made, is so that future manipulation (combing files, copying and pasting) is more predictable, especially when it’s being done by the auithors themselves. I’m aware it’s no panacea, but the fewer deskside visits I have to make, the better for my other tasks.

      I don’t understand the logic, though, that would explain why the setting a range would have the effect of emptying it, since it wasn’t set to anything before. And I can’t explain why setting the “LinkToPrevious” property on the first section doesn’t throw an error; I’m not even trapping errors per se in this utility.

      Still, at least I remembered the other method, which seems to work just fine. But I WILL also be testing it on a document that isn’t like the one I’m using now. So thanks for the help!

    Viewing 2 reply threads
    Reply To: Setting a range empties the range?

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: