none
Powershell - Setting page orientation in Microsoft word RRS feed

Answers

All replies

  • The error message usually means that the object on which you're calling the method / setting the property is not in a state that allows the execution to be performed.

    Since we have no idea what $objselection represents, it's not possible to offer any further opinion.


    Cindy Meister, VSTO/Word MVP, my blog

    Monday, July 21, 2014 1:37 PM
    Moderator
  • Here is where the $objselection is initialised

            $document=$application.documents.open($_.fullname) #opening the documents
            write-host "Processing $($document.name)" -ForegroundColor green #letting the user know what document the script is working on currently
            $objSelection = $application.Selection #select the document

    And here is where I get the error. I have a series of if statements to handle page size and orientation depending on the orientation and paper size of the hyperlinked document

     if ($insertdoc.pagesetup.papersize -eq 41)
                    {
                        write-host "Custom to A3"
                        # set section to landscape
                        #$objselection.pagesetup.pagesize=6
                        $objselection.PageSetup.PaperSize = [Microsoft.Office.Interop.Word.WdPaperSize]::wdPaperA3
                        $objselection.PageSetup.Orientation = 1

                    }
                    # A3 Landscape page
                    elseif (($insertdoc.pagesetup.Orientation -eq 1) -and ($insertdoc.pagesetup.papersize -eq 6))
                    {
                        write-host "A3 landscape"
                        # set section to landscape
                        #$objselection.pagesetup.pagesize=6
                        $objselection.PageSetup.PaperSize = [Microsoft.Office.Interop.Word.WdPaperSize]::wdPaperA3
                        $objselection.PageSetup.Orientation = 1
                    }

    Thank you

    Monday, July 21, 2014 1:51 PM
  • I've managed to get this working. Added a insertBreak(2) command in the if statements.
    Tuesday, July 22, 2014 2:44 PM
  • I'm glad you found something that worked for you. FWIW, the PageSetup object is actually SECTION specific and doesn't correctly "belong" to the Selection object. I imagine the problem may happen when the object model isn't "sure" what you're referring to - can't make the internal jump from Selection to SectionObject.PageSetup. Inserting a section break may have made this clear.

    But you will need to check the result you're getting very carefully to make sure it's correct. A number of things are related directly to the section and inserting unintended section breaks, just because "it works", can result in "oddly behaving" documents.

    It might be better for you to test something more like this (in VB-speak):

    Dim sec as Word.Section
    Set sec = Selection.Sections(1)
    sec.PageSetup 'etc.


    Cindy Meister, VSTO/Word MVP, my blog

    Tuesday, July 22, 2014 2:54 PM
    Moderator