1

I modified what someone else had on this forum, based on a successful use of powershell Excel to pdf with no success. Any ideas on my failing in the following code? My goal is to be able to convert an entire folder of doc and docx documents to pdf without opening the native applications. Thanks in advance:

# Acquire a list of DOC files in a folder
$Word = New-Object -ComObject word.application 
$path = "c:\Users\Desktop\Test" 
$formats = "Microsoft.Office.Interop.Word.WdFixedFormatType" -as [type]
$Word.visible = $false
$fileTypes = "*.docx","*doc"
$Files = GET-CHILDITEM $path -include $fileTypes

Foreach ($File in $Files) {
    $filepath = Join-Path -path $path -childpath ($File.basename +".pdf")
    $Doc = $Word.open($File.fullname, 3)
    $Doc.Saved= $true
    "Converting $File to pdf ... $destPath"
    # Save this File as a PDF in Word 2010/2013
    $Doc.ExportAsFixedFormat($formats::wdTypePDF, $path)
    $Doc.FullName -replace '\.doc?$', '.pdf'
    $Doc.Close()
}
$Word.Quit()
YowE3K
  • 23,852
  • 7
  • 26
  • 40
user1255441
  • 141
  • 2
  • 11
  • 1
    Just an FYI, when you use the `Word.Application` comobject, you *are* opening the native application. – Maximilian Burszley Sep 18 '17 at 18:58
  • True, but the $Word.visible=$false means the application only runs in the background, I believe... – user1255441 Sep 18 '17 at 19:05
  • 2
    are you getting a error? – ArcSet Sep 18 '17 at 19:08
  • `$Word.open` > `$Word.Documents.Open`, for a start. And your `Get-ChildItem` does not return anything, so you never enter the `foreach` loop. – sodawillow Sep 18 '17 at 19:09
  • @sodawillow thats becuase its getting the files from a folder with a given type...I would assume its not empty on the users end. – ArcSet Sep 18 '17 at 19:17
  • @sodawillow what if I removed line 5 and changed line 6-9 to read as follows: $Files = GET-CHILDITEM -Path $path -include *.doc, *.docx -recurse Foreach ($File in $Files) { $filepath = Join-Path -path $path -childpath ($File.basename +".pdf") $Doc = $Word.Documents.Open($File.fullname, 3) – user1255441 Sep 18 '17 at 19:23
  • @ArcSet - no error, just nothing happening... – user1255441 Sep 18 '17 at 19:24
  • @ArcSet & OP: [read this](https://stackoverflow.com/questions/18616581/how-to-properly-filter-multiple-strings-in-a-powershell-copy-script). OP: please test the code yourself, and check the errors you get, to correct your mistakes. Also, [here is a useful tutorial for you](https://learn-powershell.net/2014/12/31/beginning-with-powershell-and-word/). – sodawillow Sep 18 '17 at 19:29
  • 1
    Also....your $Formats is equal to nothing – ArcSet Sep 18 '17 at 19:32
  • @sodawillow okey dokey good point. Still no dice... no errors generated and also no pdfs generated! I'll figure it out eventually... I'm pretty new to powershell and thought you might see something obvious other than what was discussed. Thanks anyway. – user1255441 Sep 18 '17 at 19:34
  • @ArcSet ouch! That's a Monday for ya. – user1255441 Sep 18 '17 at 19:36

1 Answers1

2
$path = 'C:\tests'

$wd = New-Object -ComObject Word.Application
Get-ChildItem -Path $path -Include *.doc, *.docx -Recurse |
    ForEach-Object {
        $doc = $wd.Documents.Open($_.Fullname)
        $pdf = $_.FullName -replace $_.Extension, '.pdf'
        $doc.ExportAsFixedFormat($pdf,17,$false,0,3,1,1,0,$false, $false,0,$false, $true)
        $doc.Close()
    }
$wd.Quit()

This will Go trough the folder and then look for .doc and .docx files

It then opens each file creates a new save name and exports to PDF

You can learn more about the ExportAsFixedFormat from https://msdn.microsoft.com/en-us/library/bb256835(v=office.12).aspx

ArcSet
  • 6,518
  • 1
  • 20
  • 34
  • `ExportAsFixedFormat`: 13 parameters. Not less. Now I understand why my ISE was going crazy over this specific line... **Always** read the docs! %) – sodawillow Sep 18 '17 at 22:21
  • 1
    Parameter number 5 is the `WdExportRange`, which determines how many pages are exported; I found with it set to `3` as in the answer I only got the first page out, setting to `0` exports the entire document. https://learn.microsoft.com/en-us/office/vba/api/word.wdexportrange – David258 Dec 03 '20 at 14:20