Results 1 to 4 of 4
  1. #1
    3 Star Lounger
    Join Date
    Mar 2003
    Location
    Elkins Park, Pennsylvania, USA
    Posts
    325
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

    Dim HeaderType As String, hRange As Range

    Set hRange = Nothing
    Activedocument.Sections(1).PageSetup.DifferentFirs tPageHeaderFooter = False
    Activedocument.Sections(1).Headers(wdHeaderFooterP rimary).LinkToPrevious = False
    Set hRange = Activedocument.Sections(1).Headers(wdHeaderFooterP rimary).Range
    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.)
    <font face="Comic Sans MS">That's what you do in a herd; you look out for each other!</font face=comic> - Mike

  2. #2
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,853
    Thanks
    4
    Thanked 259 Times in 239 Posts
    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?
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  3. #3
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts
    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.DifferentFirs tPageHeaderFooter = 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

  4. #4
    3 Star Lounger
    Join Date
    Mar 2003
    Location
    Elkins Park, Pennsylvania, USA
    Posts
    325
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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!
    <font face="Comic Sans MS">That's what you do in a herd; you look out for each other!</font face=comic> - Mike

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •