converting a pdf to a docx

Welcome Forums General PowerShell Q&A converting a pdf to a docx

Viewing 6 reply threads
  • Author
    Posts
    • #202457
      Participant
      Topics: 5
      Replies: 4
      Points: 42
      Rank: Member

      I’m trying to convert a pdf to a docx

      My code:

      $filePath = "C:\Users\Larso\Downloads\Install.pdf"
      $wd = New-Object -ComObject Word.Application
      $wd.Visible = $true
      $txt = $wd.Documents.Open(
      $filePath,
      $false,
      $false,
      $false)
      
      $wd.Documents[1].SaveAs("C:\Users\Larso\Downloads\Install.docx")
      $wd.Documents[1].Close()

      i get error:

      New-Object : Retrieving the COM class factory for component with CLSID {00000000-0000-0000-0000-000000000000}
      failed due to the following error: 80040154 Class not registered (Exception from HRESULT: 0x80040154
      (REGDB_E_CLASSNOTREG)).
      At line:2 char:11
      + $wd = New-Object -ComObject Word.Application
      + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo : ResourceUnavailable: (:) [New-Object], COMException
      + FullyQualifiedErrorId : NoCOMClassIdentified,Microsoft.PowerShell.Commands.NewObjectCommand
      
      The property 'Visible' cannot be found on this object. Verify that the property exists and can be set.
      At line:3 char:1
      + $wd.Visible = $true
      + ~~~~~~~~~~~~~~~~~~~
      + CategoryInfo : InvalidOperation: (:) [], RuntimeException
      + FullyQualifiedErrorId : PropertyNotFound
      
      You cannot call a method on a null-valued expression.
      At line:4 char:1
      + $txt = $wd.Documents.Open(
      + ~~~~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo : InvalidOperation: (:) [], RuntimeException
      + FullyQualifiedErrorId : InvokeMethodOnNull
      
      Cannot index into a null array.
      At line:10 char:1
      + $wd.Documents[1].SaveAs("C:\Users\Larso\Downloads\Install.docx")
      + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo : InvalidOperation: (:) [], RuntimeException
      + FullyQualifiedErrorId : NullArray
      
      Cannot index into a null array.
      At line:11 char:1
      + $wd.Documents[1].Close()
      + ~~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo : InvalidOperation: (:) [], RuntimeException
      + FullyQualifiedErrorId : NullArray

      what did i do wrong or am missing?

      • This topic was modified 1 month, 3 weeks ago by grokkit. Reason: formatting
    • #202487
      Participant
      Topics: 12
      Replies: 1489
      Points: 1,987
      Helping Hand
      Rank: Community Hero

      Is Microsoft Word installed on the device that is running this code?

    • #202499
      Participant
      Topics: 5
      Replies: 4
      Points: 42
      Rank: Member

      Yes it is, is it possible like excel, where i needed to download the module for it to work in powershell?

    • #202502
      Participant
      Topics: 5
      Replies: 4
      Points: 42
      Rank: Member

      so i jsut tried

      Get-Command | Where-Object -Property Name -Match ‘Word’

      and i got

      function   Set-PcsvDeviceUserPassword

      and

      cmdlet Reset-ComputerMachinePassword

       

    • #202505
      Participant
      Topics: 5
      Replies: 4
      Points: 42
      Rank: Member

      Now, one thing i also noticed was when i search WORD, it does list it under Apps.

    • #202517
      Participant
      Topics: 12
      Replies: 1489
      Points: 1,987
      Helping Hand
      Rank: Community Hero

      No, Excel and Word use COM Objects, here are some more examples. These were mostly used as interfaces for scripting languages like VBScript because they could not access .NET framework.

      While there are wrappers for Microsoft Word, like PSWriteWord, they typically still leverage the COM Object. The error you posted looks like Word is not installed, the COM Object is not registered or something is generally broken as your code to open Word does work on my system. If you run this, it should be True:

      $comObjects = Get-ChildItem HKLM:\Software\Classes -ErrorAction SilentlyContinue | Where-Object {
         $_.PSChildName -match '^\w+\.\w+$' -and (Test-Path -Path "$($_.PSPath)\CLSID")
      } | Select-Object -ExpandProperty PSChildName
      
      $comObjects -contains 'Word.Application'
      

      If it is not True, you may want to try another workstation or re-install\repair Office.

    • #202520
      Participant
      Topics: 5
      Replies: 4
      Points: 42
      Rank: Member

      Ok, that test really helped. Unfortunately, it did come up as false, i was running it in Powershell ISE as an administrator. I tried the repair, uninstalled and reinstalled, and still kept getting False. What other work stations do you recommend?

Viewing 6 reply threads
  • You must be logged in to reply to this topic.