Solved

automating MS Word 2010 -> PDF

Posted on 2014-10-20
3
369 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

A brief introduction to what I consider to be the best editor for PowerShell.
The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
This video walks the viewer through the process of creating a watermark for their document, customizing it, and saving it for viewing/printing needs.
This video walks the viewer through the process of creating an MLA formatted document, as well as a bibliography with citations.

820 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