Solved

automating MS Word 2010 -> PDF

Posted on 2014-10-20
3
352 Views
Last Modified: 2014-10-21
I understand that it is possible to have a PowerShell script automate MS Word to read an MS Word document from a folder and save it out as a PDF document (same name, different extension) without bringing up a user GUI (in order to run on a server).

We have a need to run a script like this via the scheduler (once / minute or in a loop) to convert MS Word documents (*.doc, *.docx) to PDF (any relatively current version) on a Windows 2008 R2 (64-bit) server image.

Does anyone have the time to provide a script that can do this?  The goal is something that we can have going unattended and is reasonably immune to "hanging".
0
Comment
Question by:klamerus
  • 2
3 Comments
 
LVL 16

Accepted Solution

by:
Joshua Grantom earned 500 total points
ID: 40392813
Use this powershell script from Scripting Guy!
http://blogs.technet.com/b/heyscriptingguy/archive/2008/11/12/how-can-i-convert-word-files-to-pdf-files.aspx

$wdFormatPDF = 17
$word = New-Object -ComObject word.application
$word.visible = $false
$folderpath = "c:\folderpath\*"
$fileTypes = "*.docx","*doc"
Get-ChildItem -path $folderpath -include $fileTypes |
foreach-object `
{
 $path =  ($_.fullname).substring(0,($_.FullName).lastindexOf("."))
 "Converting $path to pdf ..."
 $doc = $word.documents.open($_.fullname)
 $doc.saveas([ref] $path, [ref]$wdFormatPDF)
 $doc.close()
}
$word.Quit()

Open in new window


Then set it up as a scheduled task on your server that will run every 5 minutes. You will need to have word installed on the server as well.

You will also need to take a look here (this is from the comments on Scripting Guy! blog)
Summary of the 'Ogawa Hack': create a desktop folder for the system profile, as either

C:\Windows\SysWOW64\config\systemprofile\Desktop, or

C:\Windows\System32\config\systemprofile\Desktop

...depending on whether you have 64-bit Windows.

Also, the folder needs write permission for whatever user is "driving" Office.
https://social.msdn.microsoft.com/Forums/en-US/b81a3c4e-62db-488b-af06-44421818ef91/excel-2007-automation-on-top-of-a-windows-server-2008-x64?forum=innovateonoffice
0
 
LVL 1

Author Closing Comment

by:klamerus
ID: 40395248
This works great on my desktop (which is fine at this point).

Now I have to move on and get PPT to work.  Then onto getting these to run on a server.
0
 
LVL 16

Expert Comment

by:Joshua Grantom
ID: 40395269
Function to convert PPT files to PDF
http://pshscripts.blogspot.co.uk/2012/11/convert-pptxtopdfps1.html


Script to perform function from Thomas Lee's blog
http://tfl09.blogspot.com/2012/11/saving-powerpoint-slides-to-pdf-with.html
    $ipath = "E:\SkyDrive\PowerShell V3 Geek Week\"

    Foreach ($ifile in $(ls $ipath -Filter "*.pptx")) {
      # Build name of output file
      $pathname = split-path $ifile
      $filename = split-path $ifile -leaf
      $file     = $filename.split(".")[0]
      $ofile    = $pathname + $file + ".pdf"

      # Convert _this_ file to PDF
       Convert-PptxToPDF -ifile $ifile -OFile $ofile
    }

Open in new window

0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

"Migrate" an SMTP relay receive connector to a new server using info from an old server.
Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
This video shows and describes the main difference between both orientations in Microsoft Word. Viewers will understand when to use each orientation and how to get the most out of them.
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now