Solved

automating MS Word 2010 -> PDF

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
How to sign a powershell script so you can prevent tampering, and only allow users to run authorised Powershell scripts
This video shows where to find the word count, how to display it, and what it breaks down to in Microsoft Word.
Learn how to create and modify your own paragraph styles in Microsoft Word. This can be helpful when wanting to make consistently referenced styles throughout a document or template.

757 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

23 Experts available now in Live!

Get 1:1 Help Now