title: PowerShell $objSelection = $objOffice.Selection not working as a service
body: <p>I have a PowerShell script that reads a docx document, then does some find and replace operations and prints or saves it as a pdf. I call that script through a batch file.</p>
  
  <p>When I call the script in a normal way, by double clicking, it works fine, but when I create a service (Using NSSM) that calls the batch file it fails.</p>
  
  <p>The problem lies in the following line:</p>
  
  <pre><code>$objSelection = $objOffice.Selection
  Write-Host "************ " $objSelection
  </code></pre>
  
  <p>When I call the script over the GUI I get the following:</p>
  
  <pre><code>************  System.__ComObject
  </code></pre>
  
  <p>But when I call it through a service I get the following</p>
  
  <pre><code>************  
  </code></pre>
  
  <p>Meaning that when I attempt to use $objSelection.Find.Execute(...) the whole thing fails, as it attempts to do a null.Find.Execute(...)</p>
  
  <pre><code>Es ist nicht m”glich, eine Methode fr einen Ausdruck aufzurufen, der den NULL hat.
  In C:\Users\Avejsic\Desktop\DruckenAlsService\PSConvertToPDF\OfficeFilePrint.ps1:193 Zeichen:13
  +             $objSelection.Find.Execute($FindText,$MatchCase,$MatchWho ...
  +             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
  + FullyQualifiedErrorId : InvokeMethodOnNull
  </code></pre>
  
  <p>Translation:
  You cannot call a method on a null-valued expression.</p>
  
  <p>Does anyone have a idea why this happens and how to avoid the problem?</p>
  
  <h2>EDIT:</h2>
  
  <p>I am using NSSM - the Non-Sucking Service Manager to create a Windows service. How to use it:</p>
  
  <ol>
  <li><p>It needs to be started over the console: "nssm64 install AAwsome-service"</p></li>
  <li><p>Application->Path: select the "..." and select the batch which calls the PS1 script</p></li>
  <li><p>ExitActions->Restart: No action (we wan the script to run only once)</p></li>
  <li><p>I/O: Output(sdtout): select any *.txt document</p></li>
  <li><p>Hit the "Install service" button</p></li>
  </ol>
  
  <p>The batch file which executes the powershell:</p>
  
  <pre><code>powershell -Command "./PowershellScript.ps1"
  </code></pre>
  
  <p>The powershell script:</p>
  
  <pre><code>$DocumentName = "C:\Users\&lt;YOUR_USER&gt;\Desktop\PS1_Extract\Document.docx" 
  $SaveAsName = "C:\Users\&lt;YOUR_USER&gt;\Desktop\PS1_Extract\DocumentOut.pdf"
  $Printername = "Bullzip PDF Printer"
  #$Printername = "Foxit Reader PDF Printer"
  
  $objOffice = New-Object -comobject Word.Application
  $objOffice.DisplayAlerts = [Microsoft.Office.InterOp.Word.WdAlertLevel]::wdAlertsNone
  $objOffice.Visible = $false
  
  $objDoc = $objOffice.Documents.OpenNoRepairDialog($DocumentName)
  
  $objSelection = $objOffice.Selection
  
  Write-Host "--- objOffice ---" $objOffice
  Write-Host "--- objSelection ---" $objSelection
  
          $FindText = "Test123"
          $ReplaceAll = 1
          $FindContinue = 1
          $MatchCase = $False
          $MatchWholeWord = $True
          $MatchWildcards = $False
          $MatchSoundsLike = $False
          $MatchAllWordForms = $False
          $Forward = $True
          $Wrap = $FindContinue
          $Format = $False
          $ReplaceText = "Success"
  
  
  $objSelection.Find.Execute($FindText,$MatchCase,$MatchWholeWord,$MatchWildcards,$MatchSoundsLike,$MatchAllWordForms,$Forward,$Wrap,$Format,$ReplaceText,$ReplaceAll)
  
  $objDoc.Close()
  $objOffice.Quit()
  </code></pre>