|
|
Thread Tools | Display Modes |
#1
|
|||
|
|||
Macro is manipulating item in wrong section even though variable watch says it should be right
This is driving me nuts. I have a macro that is run from a drop down list in a custom toolbar. That part works fine, the argument(?) from the drop down is passed to the macro and the macro runs. It's supposed to insert a new appendix after the current section. All goes fine until it comes to manipulating the inserted cover graphic in the header. A variable keeps track of what the current section is, and when stepping through the code the variable remains correct (i.e. it always refers to the section I want to manipulate). My test document has 7 sections to start, the macro inserts another one and the "aSec" variable changes to 8 and is shown as 8 in the Watch area. However, when it comes to the line:
Code:
Set myShape = aDoc.Sections(aSec).Headers(wdHeaderFooterFirstPage).Shapes(1) Any idea why it's not applying the transformation to the header graphic of section 8? Code:
Sub BuildAppendixA4(ByRef strArg As String) Dim aDoc As Document, aSec As Long, aRng As Range, bRng As Range, bSec As Long Dim myShape As Shape Dim oTemplate As Template 'On Error Resume Next aSec = Selection.Information(wdActiveEndSectionNumber) Set aDoc = ActiveDocument Set aRng = aDoc.Sections(aSec).Range Set oTemplate = ActiveDocument.AttachedTemplate 'Put cursor at end of current section aRng.Collapse Direction:=wdCollapseEnd Selection.InsertBreak Type:=wdSectionBreakNextPage 'Make sure the view is correct If ActiveWindow.View.SplitSpecial <> wdPaneNone Then ActiveWindow.Panes(2).Close End If If ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow. _ ActivePane.View.Type = wdOutlineView Then ActiveWindow.ActivePane.View.Type = wdPrintView End If 'Turn off link to previous in case previous appendix isn't A4 Portrait ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader With Selection.HeaderFooter .LinkToPrevious = Not Selection.HeaderFooter.LinkToPrevious End With 'Advance selected section by one, which puts us in the newly-created section aSec = aSec + 1 'Set a new range which is the header of the new section, and set it to be a different first page Set bRng = aDoc.Sections(aSec).Headers(wdHeaderFooterFirstPage).Range aDoc.Sections(aSec).PageSetup.DifferentFirstPageHeaderFooter = True 'Set it to be an A4 portrait page, in case the previous appendix wasn't A4 portrait With aDoc.Sections(aSec).PageSetup .Orientation = wdOrientPortrait .PageWidth = CentimetersToPoints(21) .PageHeight = CentimetersToPoints(29.7) End With 'Delete anything in the header already, and insert the correct cover page jpeg from building blocks aDoc.Sections(aSec).Headers(wdHeaderFooterFirstPage).Range.Delete oTemplate.BuildingBlockEntries("Appendix Cover").Insert Where:=bRng, RichText:=True 'aDoc.Sections(aSec).Headers(wdHeaderFooterFirstPage).Range.Select 'Set the selection to be the appendix cover jpeg, which will be the only shape in the selected header Set myShape = aDoc.Sections(aSec).Headers(wdHeaderFooterFirstPage).Shapes(1) 'Make sure the A4 jpeg is the correct size and positioned correctly With myShape .WrapFormat.Type = wdWrapFront .LockAspectRatio = msoTrue .Width = CentimetersToPoints(21) .RelativeHorizontalPosition = wdRelativeHorizontalPositionPage .Left = wdShapeLeft .RelativeVerticalPosition = wdRelativeVerticalPositionPage .Top = wdShapeTop End With 'Exit the header and insert the appendix heading text ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument Selection.Style = ActiveDocument.Styles("Heading 9") Selection.TypeText Text:=Chr(11) & "Appendix Name" Selection.TypeParagraph Select Case strArg Case "AppendixNextA4Portrait" Selection.InsertBreak Type:=wdPageBreak Selection.TypeParagraph Case "AppendixNextA3Portrait" Selection.InsertBreak Type:=wdSectionBreakNextPage ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader With Selection.HeaderFooter .LinkToPrevious = Not Selection.HeaderFooter.LinkToPrevious End With aSec = aSec + 1 Set bRng = aDoc.Sections(aSec).Headers(wdHeaderFooterFirstPage).Range aDoc.Sections(aSec).PageSetup.DifferentFirstPageHeaderFooter = False With aDoc.Sections(aSec).PageSetup .Orientation = wdOrientPortrait .PageWidth = CentimetersToPoints(29.7) .PageHeight = CentimetersToPoints(42) End With Case "AppendixNextA4Landscape" Selection.InsertBreak Type:=wdSectionBreakNextPage ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader With Selection.HeaderFooter .LinkToPrevious = Not Selection.HeaderFooter.LinkToPrevious End With aSec = aSec + 1 Set bRng = aDoc.Sections(aSec).Headers(wdHeaderFooterFirstPage).Range aDoc.Sections(aSec).PageSetup.DifferentFirstPageHeaderFooter = False With aDoc.Sections(aSec).PageSetup .Orientation = wdOrientLandscape .PageWidth = CentimetersToPoints(29.7) .PageHeight = CentimetersToPoints(21) End With Case "AppendixNextA3Landscape" Selection.InsertBreak Type:=wdSectionBreakNextPage ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader With Selection.HeaderFooter .LinkToPrevious = Not Selection.HeaderFooter.LinkToPrevious End With aSec = aSec + 1 Set bRng = aDoc.Sections(aSec).Headers(wdHeaderFooterFirstPage).Range aDoc.Sections(aSec).PageSetup.DifferentFirstPageHeaderFooter = False With aDoc.Sections(aSec).PageSetup .Orientation = wdOrientLandscape .PageWidth = CentimetersToPoints(42) .PageHeight = CentimetersToPoints(29.7) End With End Select ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument End Sub |
#2
|
||||
|
||||
In my experience with floating shapes in the headers, VBA seems to think all the shapes are in every section even though the user interface shows a graphic only in one sections header. This can be further complicated when some headers may have 'Link to Previous' turned on.
So I have no faith at all in being able to use VBA to determine which shape you are referring to via the section number. If you have some shapes in your document headers, try this macro to see the way VBA keeps track of these shapes. Code:
Sub TestHeaderFloaters() Dim aH As HeaderFooter, aShp As shape, aSect As Section Debug.Print ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Shapes.Count For Each aSect In ActiveDocument.Sections For Each aH In aSect.Headers For Each aShp In aH.Shapes Debug.Print aSect.Index, aH.Index, aShp.Anchor.Information(wdActiveEndSectionNumber), aShp.AlternativeText Next aShp Next aH Next aSect End Sub
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#3
|
|||
|
|||
Wow, that is super interesting. Thanks for taking the time to reply.
I am guessing that the expected output of that macro would be that it logs each image from the start of the document to the end? For extra clarity I gave all my images alt text describing which one it was, and also added in a line to delete each one as it went for more visual effect. I then stepped through it bit by bit and watched it and wow, no logic at all to it. 1 1 5 Section 5 RH 1 1 6 Section 6 First Page BG 1 1 2 Section 2 BG 1 1 5 Section 5 LH 1 1 6 Section 6 LH 1 2 3 Section 3 RH 1 2 1 Section 1 BG 1 3 6 Section 6 RH 2 1 3 Section 3 LH I ran it again and the order was the same so it's obviously not random, but thought perhaps it simply runs through them in the order in which they were inserted? With that in mind I made a new document and pasted each graphic in order. Ran it and got this: 1 1 4 Section 3 RH 1 1 6 Section 6 FP BG 1 1 1 Section 1 BG 1 1 4 Section 3 LH 1 1 6 Section 6 LH 1 2 5 Section 5 RH 1 2 2 Section 2 BG 1 3 6 Section 6 RH 2 1 5 Section 5 LH So, still not in any particular order. It's almost but not quite the reverse order to before, but as it's not exact I won't read much into it. Anyway, I went back and took out the delete line, and noticed that it ran 6 times through the document. I was puzzled by this but then saw, aha, I have 6 sections. Added another section and it ran through the loop 7 times. So that would indicate that it does indeed see every image as being part of every section, and it loops through them each time. When I had the line in there to delete the image after it printed the info it obviously only found the info once. Also, the first number is the section number, and it's seeing all bar one in section 1 as opposed to 1-2 per section as is the reality. Thanks also for sharing your workaround: sounds like that might solve the problem by approaching it from a different angle. |
#4
|
||||
|
||||
Quote:
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#5
|
||||
|
||||
I haven't noted that before so would need to do testing to verify that is now the case. When I was testing years ago the 'link to previous' was NOT responsible for the dodgy results.
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#6
|
||||
|
||||
It should only be necessary to do that if you're trying to do something that requires access to the parent Section; usually, textboxes can be manipulated in whatever Section's header they're found in, regardless of the .LinkToPrevious attribute. Naturally, that affects all other Sections containing the same textbox. A real 'gotcha' would be textboxes that show up in STYLEREF or REF fields - in which case you really do need to manipulate them at the source.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Footnote numbering wrong after section break | peterbav | Word | 1 | 03-17-2020 11:18 PM |
How do I make a checkbox or dropdown item reference data in that specific section of the data array? | dhare | Excel Programming | 2 | 02-24-2016 12:36 PM |
Copying data related to one item to worksheet with many instances of the same item | nmp13 | Excel | 3 | 02-06-2016 02:13 AM |
Multilevel Numbering wrong after Section Break | Kay Wood | Word | 2 | 01-16-2013 08:44 AM |
Help with VBA macro - Variable input | sc30317 | Excel Programming | 0 | 08-31-2011 01:00 PM |