Results 1 to 6 of 6
  1. #1
    2 Star Lounger
    Join Date
    Oct 2007
    Posts
    172
    Thanks
    0
    Thanked 2 Times in 1 Post

    Adding Footer shapes, runs differently in debugger (Word 2003 VB)

    Hi All,

    i have a loop where I am adding images to the section footers in the document. When I run the macro without breaking in the de######, I always get 3 images the same in the footer of the first page of the first section in the document. When I break in the de###### to see what's going on, the code works fine and I only get one image in the first section first page footer, as I would expect.

    Does the AddPicture function start a new thread? Is it possible that when the macro runs without breaking, I check if the logo already exists again faster than the image can be loaded? If that were the case, I would expect this problem to occur with every footer, not just the first footer of the first section - the only different thing about the first section is that the "First Page Header/Footer Different" flag is TRUE. For all other sections, the "First Page Header Footer Different" flag is set to FALSE.

    If anyone can give me a clue why this code might work fine when debugging, but not work at "normal speed", I would be very grateful! Maybe I am just being stupid!

    For Each sect In ActiveDocument.Sections

    'set up the array, ordering also by wdHeaderFooterIndex (important!)
    Set SR(1) = sect.Footers(wdHeaderFooterPrimary).range.ShapeRan ge
    Set SR(2) = sect.Footers(wdHeaderFooterFirstPage).range.ShapeR ange
    Set SR(3) = sect.Footers(wdHeaderFooterEvenPages).range.ShapeR ange

    'for each footer type in the section, add an image if necessary
    For i = 1 To 3

    If SR(i).Count = 1 Then ' should at least have logo

    'if the user has selected to add one and it doesn't already exist
    addNew = Not delete And path <> "" And Not SubsLogoExists(sect.Footers(i))

    If addNew Then
    Dim newLogo As Shape
    'add the image to the footer
    Set newLogo = sect.Footers(i).shapes.AddPicture(FileName:=path)
    'set the properties of the image
    sizeRestricted = SetSubsLogoProperties(newLogo, sect.PageSetup, i)
    End If

    End If

    Next i

    Next sect

  2. #2
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,852
    Thanks
    4
    Thanked 259 Times in 239 Posts

    Re: Adding Footer shapes, runs differently in debu

    I understand your frustration - many years ago I wrestled with this type of problem and needed to implement several kludges to resolve the issues I encountered. Perhaps the code in <post:=157,050>post 157,050</post:> will give you some hints on ways to work around the problem you are having. Its not pretty but it worked adequately. The big problem is that all the floating objects in any header-footer sit in the first section according to the VBA properties which is not really the case. The next problem is checking whether the section uses a same as previous setting or not since you can't actually find out if the shape is there already by the direct means.

    Without testing your code, I believe that if you placed the logos as inline objects rather than floating shapes then the behaviour would be more predictable. In the case of a watermark this wouldn't work but maybe there is no real reason why your logos need to be floating.
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  3. #3
    2 Star Lounger
    Join Date
    Oct 2007
    Posts
    172
    Thanks
    0
    Thanked 2 Times in 1 Post

    Re: Adding Footer shapes, runs differently in debu

    Hi,
    thanks for the reply. At the moment my biggest issue is to understand why the results are different when run through the de######. There's nothing wrong with my code logic, but obviously something is causing the behaviour to be different when I introduce a breakpoint somewhere in my main loop. Yes, this whole area is nasty stuff because of the way Word deals with headers/footers and their shapes. I tried a solution with inline shapes, but i need to position them dynamically and this proved even more tricky.

    In the worst case, I end up with three logos, all on top of each other, on the first page of the first section of the document. All other pages contain the single logo correctly. When I run with the de###### I get a single logo on every page, as expected.

    Any other ideas? i hvae read through the code on the linked post and i understand it - i just don't want to spend time completely re-writing it (again!) if there's a known reason why this could happen.

    Does anyone know if the ShapeRange.AddPicture function starts a new thread to load the image?

    cheers

  4. #4
    2 Star Lounger
    Join Date
    Oct 2007
    Posts
    172
    Thanks
    0
    Thanked 2 Times in 1 Post

    Re: Adding Footer shapes, runs differently in debu

    Actually I have no narrowed down the issue. If I run the code with no breakpoints, I get the problem described above.

    If I put a break on the line

    Set newLogo = sect.Footers(i).shapes.AddPicture(FileName:=path)

    and just click continue (3 times) until the macro has finished running, the code works. However, if I put a breakpoint ON THE NEXT LINE BELOW

    sizeRestricted = SetSubsLogoProperties(newLogo, sect.PageSetup, i)

    i have to click continue 5 times before the macro finishes and I have 2 extra images on the first page of the first section.

    I'll keep working on it, but this is very bizarre.

  5. #5
    2 Star Lounger
    Join Date
    Oct 2007
    Posts
    172
    Thanks
    0
    Thanked 2 Times in 1 Post

    Re: Adding Footer shapes, runs differently in debu

    Well I have resolved the problem based on a hunch, but I still can't explain it really.

    I have added a line to include an additional check to see if the logo should indeed be added, based on the status of the "Section.PageSetup.DifferentFirstPageHeaderFoo ter" flag. I had a feeling that images were being added to subsequent section first page footers which were then appearing on the first page of the first section, because the sections were linked to previous. This was happening even though the subsequent sections had the DifferentFirstPageHeaderFooter set to false and therefore didn't require another first page image to be added.

    I still can't explain why it worked in the de###### sometimes and other times it didn't. But there you go.

    The new line I added is:

    addNew = addNew And Not (i = wdHeaderFooterFirstPage And Not sect.PageSetup.DifferentFirstPageHeaderFooter)

  6. #6
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Adding Footer shapes, runs differently in debu

    Glad you were able to solve it, and thanks for sharing the solution.

Posting Permissions

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