Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #1  
Old 10-20-2022, 02:42 PM
WordUser789 WordUser789 is offline Macro is manipulating item in wrong section even though variable watch says it should be right Windows 10 Macro is manipulating item in wrong section even though variable watch says it should be right Office 2016
Novice
Macro is manipulating item in wrong section even though variable watch says it should be right
 
Join Date: Jun 2017
Posts: 25
WordUser789 is on a distinguished road
Default 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)
...and the "with myShape" part, the transformation is applied to one of the two header graphics in the header of section 3! After it does this, the text I want to insert is applied in the correct section, which is section 8.

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

Reply With Quote
 



Similar Threads
Thread Thread Starter Forum Replies Last Post
Footnote numbering wrong after section break peterbav Word 1 03-17-2020 11:18 PM
Macro is manipulating item in wrong section even though variable watch says it should be right 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
Macro is manipulating item in wrong section even though variable watch says it should be right 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

Other Forums: Access Forums

All times are GMT -7. The time now is 10:09 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft