• Page orientation check (W2000 SR-1)

    Author
    Topic
    #381418

    Hi,

    I’m trying to create a macro that checks each section of a document. If the section is portrait, I set the footer tab to 6 inches. If the section is landscape, I set the footer tab to 9 inches. I keep getting an object variable-related message, but my attempts to fix it have failed.

    The following post (100219) was helpful, but I’m still having trouble:

    http://www.wopr.com/cgi-bin/w3t/showthread…ew=&sb=&o=&vc=1

    Thanks,
    Bob

    **************************************************************************************************

    Sub FixSectionOrientation()

    Dim oSec As Section
    Dim oHead As HeaderFooter
    Dim oFoot As HeaderFooter
    Dim oRng As Range
    Dim counter1 As Long
    Dim orient As Long

    ‘Go to first footer

    Selection.GoTo What:=wdGoToSection, Which:=wdGoToFirst, count:=1, Name:=””
    Selection.Find.ClearFormatting
    With Selection.Find
    .Text = “”
    .Replacement.Text = “”
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    End With

    ‘Show footer

    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
    ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooter

    ‘Set tab to 6 inches in first footer

    Selection.EndKey Unit:=wdLine
    Selection.ParagraphFormat.TabStops.ClearAll
    ActiveDocument.DefaultTabStop = InchesToPoints(0.5)
    Selection.ParagraphFormat.TabStops.Add Position:=InchesToPoints(6), _
    Alignment:=wdAlignTabRight, Leader:=wdTabLeaderSpaces

    ‘Loop through sections, starting with Section 2
    ‘If section is portrait, set tab to 6 inches
    ‘If section is landscape, set tab to 9 inches

    For counter1 = 2 To ActiveDocument.Sections.count

    ‘Go to section

    Selection.GoTo What:=wdGoToSection, Which:=wdGoToFirst, count:=counter1, Name:=””

    Selection.Find.ClearFormatting
    With Selection.Find
    .Text = “”
    .Replacement.Text = “”
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    End With

    ‘Show footer

    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

    ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooter

    ‘If section is portrait, set tab to 6 inches

    If oSec.PageSetup.Orientation = 0 Then

    Selection.EndKey Unit:=wdLine
    Selection.ParagraphFormat.TabStops.ClearAll
    ActiveDocument.DefaultTabStop = InchesToPoints(0.5)
    Selection.ParagraphFormat.TabStops.Add Position:=InchesToPoints(6), _
    Alignment:=wdAlignTabRight, Leader:=wdTabLeaderSpaces

    End If

    ‘If section is landscape, set tab to 9 inches

    If oSec.PageSetup.Orientation = 1 Then

    Selection.EndKey Unit:=wdLine
    Selection.ParagraphFormat.TabStops.ClearAll
    ActiveDocument.DefaultTabStop = InchesToPoints(0.5)
    Selection.ParagraphFormat.TabStops.Add Position:=InchesToPoints(9), _
    Alignment:=wdAlignTabRight, Leader:=wdTabLeaderSpaces

    End If

    Next counter1

    End Sub

    Viewing 0 reply threads
    Author
    Replies
    • #642690

      Your macro contains a lot of superfluous code, and it refers to a variable oSec that is never set. I think it can be shortened to the following – please not that the underscores _ are continuation characters – there should be a space before them:

      Sub FixSectionOrientation()
      Dim oSec As Section
      Dim counter1 As Long
      Dim tabSetting As Single

      ‘ Set tab stop for first section footer
      With ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary). _
      Range.ParagraphFormat.TabStops
      .ClearAll
      .Add Position:=InchesToPoints(6), _
      Alignment:=wdAlignTabRight, Leader:=wdTabLeaderSpaces
      End With

      ‘Loop through sections, starting with Section 2
      For counter1 = 2 To ActiveDocument.Sections.Count
      Set oSec = ActiveDocument.Sections(counter1)
      ‘ Determine tab setting depending on orientation
      If oSec.PageSetup.Orientation = wdOrientPortrait Then
      tabSetting = 6
      Else
      tabSetting = 9
      End If
      ‘ Set tab stop for section footer
      With oSec.Footers(wdHeaderFooterPrimary). _
      Range.ParagraphFormat.TabStops
      .ClearAll
      .Add Position:=InchesToPoints(tabSetting), _
      Alignment:=wdAlignTabRight, Leader:=wdTabLeaderSpaces
      End With
      Next counter1

      Set oSec = Nothing
      End Sub

      • #643504

        This looks good. However, I’m still returning a “9999999” value for oSec.PageSetup.Orientation, rather than 0 or 1.

        Thanks,
        Bob

        • #643570

          A return value of 999999 usually means that both values are true, although how you could have both Landscape and Portrait pages in one section escapes me at the moment! Is there anything strange about your document’s page orientations?

          StuartR

          • #958891

            The section is set up to use different headers and footers for odd and even pages. I only use one kind of orientation in each section, but I wonder if Word is seeing multiple values because of the odd/even specification?

            I’ve played around with different combinations of the ActiveDocument, Sections, PageSetUp, Headers, Footers, and Orientation objects and properties, but I still keep getting the 999999 result.

            I’ve been testing the following code with the attached document:

            Sub ChangeHdrFtrTabs()

            Dim PgOr As Variant
            Dim SecNum As Integer
            Dim SecTotal As Integer

            SecTotal = ActiveDocument.Sections.Count

            For SecNum = 1 To SecTotal

            PgOr = ActiveDocument.Sections(SecNum).PageSetup.Orientation

            Next SecNum

            End Sub

            Sections 1 and 3 return good values (0 or 1), but sections 2 and 4 return 9999999

            Thanks,
            Bob

            • #958892

              Very strange, you get the same 99999999 for PageHeight and PageWidth, as well as Orientation – but I can’t see any obvious reason.

              Just changing Section 3 from Landscape to Portrait is sufficient to make it all behave properly.

              StuartR

            • #958942

              When you access the PageSetup property (create the PageSetup object) from a range smaller than the entire section, you can get better results from this document. Here’s some demo code:

              Function SectOrientList(doc As Word.Document) As String
              ' Returns "|" delimited string of orientation info for sections in doc
              Dim intCount As Integer, strArray() As String
              ReDim strArray(doc.Sections.Count)
              For intCount = 1 To doc.Sections.Count
                  strArray(intCount) = CStr(doc.Range( _
                                            doc.Sections(intCount).Range.Start + 1, _
                                            doc.Sections(intCount).Range.Start + 2). _
                                            PageSetup.Orientation)
              Next
              SectOrientList = Join(strArray(), "|")
              End Function
               
              Sub TEST_SectOrientList()
              Debug.Print SectOrientList(ActiveDocument)
              End Sub
              

              I have no idea why that should be the case!

              Added: Perhaps it is because your section 3 is being created inside an INCLUDETEXT field in Section 2 of your container document. That’s very confusing to me, so perhaps it’s confusing to VBA, too?

        • #643756

          If the page orientation can’t/won’t return the correct value, can you do a comparison of page height to width to give you the same answer.
          eg If .PageSetup.PageHeight > PageSetup.PageWidth then Portrait

          You may also need to consider cases where the footers are linked to previous (can get ugly) and where there are other types of footers present (even or first page)

          • #643767

            Thanks for the replies. We do use Link To Previous, to keep the content consistent in the footers. I’ll try turning that off before checking the page orientation.

            • #643992

              I’ve made a little bit of progress with this issue. The file I was working with included many inserted files. The change in page orientation occurs within those inserted files. When I broke the links to the inserted files, which hard-coded the content in the big file, the macro started identifying the page orientation OK.

              I’m still having some problems. We use even/odd footers, and the macro doesn’t seem to be working in even-numbered, landscape pages. I’ve attached a zip file that shows the kind of file I’ve been working with.

              Also, my current code is at the end of this e-mail.

              Thanks everyone for your help,
              Bob

              ****************************************

              Sub FixSectionOrientation()

              Dim oSec As Section
              Dim counter1 As Long
              Dim tabSetting As Single

              ‘Set tab stop for first section footer

              With ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary). _
              Range.ParagraphFormat.TabStops
              .ClearAll
              .Add Position:=InchesToPoints(6), Alignment:=wdAlignTabRight, Leader:=wdTabLeaderSpaces
              End With

              ‘Loop through sections, starting with Section 2

              For counter1 = 2 To ActiveDocument.Sections.count

              Set oSec = ActiveDocument.Sections(counter1)

              ‘ Link, then unlink content from previous footer
              ‘ to pull in content and then freeze it in a section

              oSec.Footers(wdHeaderFooterPrimary).LinkToPrevious = True
              oSec.Footers(wdHeaderFooterPrimary).LinkToPrevious = False

              ‘ Determine tab setting depending on page orientation

              If oSec.PageSetup.Orientation = wdOrientPortrait Then
              ‘If oSec.PageSetup.PageHeight > oSec.PageSetup.PageWidth Then

              tabSetting = 6

              Else

              tabSetting = 9

              End If

              ‘ Set tab stop for section footer

              With oSec.Footers(wdHeaderFooterPrimary). _
              Range.ParagraphFormat.TabStops
              .ClearAll
              .Add Position:=InchesToPoints(tabSetting), Alignment:=wdAlignTabRight, Leader:=wdTabLeaderSpaces
              End With

              Next counter1

              Set oSec = Nothing

              End Sub

    Viewing 0 reply threads
    Reply To: Page orientation check (W2000 SR-1)

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

    Your information: