powershell -- easier way to GoTo a specific Page in a Word document?

Christian Bahnsen -- .mil account 201 Reputation points
2020-10-22T18:01:06.277+00:00

This code works but I get a ton of unneeded data. Is there an easier way to go to a specified page in a Word document or a way to suppress all the unwanted info. Here I'm going to page 2:

# create a word object
$w = New-Object -ComObject Word.Application

# make it visible
$w.visible = $true

# create a document object by opening a file
$doc = $w.Documents.Open('\\servername\Users\Christian.Bahnsen\Desktop\ToDo Folder\word\splitTest\batch721extract.docx')

# create a selection object
$objSelection = $w.Selection

# parameters equate to (wdGoToPage,wdGoToAbsolute,pageNumber)
$objSelection.GoTo(1,1,2)  

This takes me to page 2 but I'm getting a ton of unwanted info in the PowerShell ISE, e.g.,

PS C:\Users\Christian.Bahnsen> $mySelection.GoTo(1,1,2)


Text                      : 
FormattedText             : System.__ComObject
Start                     : 2099
End                       : 2099
Font                      : Microsoft.Office.Interop.Word.FontClass
Duplicate                 : System.__ComObject
StoryType                 : 1
Tables                    : System.__ComObject
Words                     : System.__ComObject
Sentences                 : System.__ComObject
Characters                : System.__ComObject
Footnotes                 : System.__ComObject
Endnotes                  : System.__ComObject
Comments                  : System.__ComObject

...
and so forth for several pages

So is there an easier way to go to a specific page or is there a way to quash all the info posh is spitting back at me?

Thanks in advance for any assistance.

Christian Bahnsen

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,504 questions
0 comments No comments
{count} votes

Accepted answer
  1. Ian Xue (Shanghai Wicresoft Co., Ltd.) 35,996 Reputation points Microsoft Vendor
    2020-10-23T02:38:51.163+00:00

    Hi,

    If you want no output you can just redirect it to null

    $mySelection.GoTo(1,1,2) | Out-Null  
    

    Best Regards,
    Ian

    ============================================

    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.