I've got a PowerShell script (running on Windows Server 2008 R2 Enterprise) that opens a Word doc in Word 2010, performs a SaveAs, and saves the doc as a PDF. In brief my code looks similar to the below:
$word = new-object -ComObject "word.application"
$word.Visible = $true
$doc = $word.documents.open("path\file.doc")
$doc.SaveAs("path\file.pdf", [ref] 17)
$doc.Close()
ps winword | kill
The above works fine, no problems at all and is converting the documents as expected.
My question is:
If I physically open Word myself and navigate to 'File > Save As' I get various options in the dialog when saving as PDF (eg. page range, optimisation etc)
How can I, if at all, access these options from within the PowerShell script when performing the same action?
Any advice would be appreciated. Maybe it's just not possible.
Thanks in advance
After much investigation I've found that option I needed was ExportAsFixedFormat().
The documentation can be found here:
http://msdn.microsoft.com/en-us/library/bb256835%28v=office.12%29.aspx
And you can see it in action within a PowerShell script here:
http://blog.coolorange.com/2012/04/20/export-word-to-pdf-using-powershell/
Related
I'd like to make a Microsoft Word document be set with a password for opening the document. When I try to run the following code in Powershell, the script will hang once I enter the desired password.
This script will be run in Kaseya to help automate protecting Word documents. I've tried both modifying the Document.Password property and using the Document.Protect method and they both will hang the script.
$path = Read-Host("Specify path to word document")
Write-Host "Creating Word application object..."
$wordAppObj = New-Object -ComObject Word.Application
Write-Host "Creating Word document object..."
$wordDocObj = $wordAppObj.Documents.Open($path)
# Write-Host "Activating Word document object..."
# $wordDocObj.Activate
Write-Host "Setting password..."
$securePass = Read-Host("Set password as") -AsSecureString
$password = ConvertFrom-SecureString $securePass
# $wordDocObj.Protect(3, $true, $password)
$wordDocObj.Password = $password
Write-Host "Saving Word document object..."
$wordDocObj.Save
Write-Host "Closing the Word document..."
$wordDocObj.Close
Write-Host "Closing Word..."
$wordAppObj.Application.Quit()
I expect the script to run through and protect the file, but the script will hang and an instance of Microsoft Word will be running in the background taking up about 6-9% of the CPU. Nothing will happen to the file or in the script. There are no error messages that pop up.
UPDATE: As suggested, I added $wordAppObj.Visible = $true to the script to see if there were any pop-ups that happened during the execution of the script. Unfortunately, I didn't see any. I believe the script may be hanging when it prompts the user to re-enter the password. This happens when I use Word to encrypt a document with a password. Is there any way to fill this field in from Powershell?
I have developed a program in C# with the Microsoft Interop reference for Word. This program worked for me. At this point, I believe that this kind of thing cannot be done from Powershell and I would advise that anyone trying to do this seek another way to get this done. I think Powershell simply doesn't support modifying the password field of a document.
When I start Microsoft Word on the Start Menu and then query Application.NormalTemplate.Path for the location of the normal.dotm file, I get, as expected, C:\Users\USERNAME\AppData\Roaming\Microsoft\Templates.
However, when I start Word using COM automation in PowerShell:
$wrd = new-object -com word.application
$wrd.visible = $true
I get a different path when querying the same expression:C:\Users\USERNAME\AppData\Local\Packages\Microsoft.Office.Desktop_8wekyb3d8bbwe\LocalCache\Roaming\Microsoft\Templates.
Why is that and what can I do in order for the COM invocation to open the same normal.dotm file?
I have been experiencing troubles when running a PowerPoint Macro from Powershell (running Excel macros already works). As there is not much info on the web regarding this topic, I'm willing that you will be able to help me.
I'm trying to automate reporting in my work area and the flow is to query data from MS SQL Server, import data into Excel file, refresh charts and then build the final report with PowerPoint slides.
PowerPoint file's charts' are linked with the Excel (via File -> Edit Links to Files) and after selecting the specific chart, Chart Tools menu appears where on the Design tab I just click "Refresh Data" and the chart refreshes. I already have a macro which refreshes data automatically for me. Now I want to call that macro from Powershell script. Please see my code below:
$ppt = New-Object -ComObject Powerpoint.Application
$FilePath = '\\c03d03\public\Data\uvall\IBNB\OnePager.pptm'
$presentation = $ppt.presentations.Open($FilePath)
$ppt.Visible = $true
$app = $ppt.Application
$tst = #('test')
$presentation.Application.Run("RefreshData",[ref]$tst) / 1st way
$app.Run("RefreshData", [ref]#()) / 2nd way
$app.Run("RefreshData", #()) / 3rd way
$app.Run("RefreshData") / 4th way
$presentation.save()
$presentation.close()
$ppt.Quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($ppt)
And none of these 4 ways of calling a macro works. Sometimes it just returns an error that the "Sub or function not defined", even if it's truly defined, sometimes it returns "You cannot call a method on a null-valued expression" and other errors.
Related:
Run PowerPoint Macro from PowerShell
Automate Powerpoint Macro
Please also be aware that I'm using Office 2013
Maybe someone can carefully look into this and help me out!
I have several 20+ MB Excel files, and they need to be refreshed every week before business starts (Monday 8 AM).
These files contain one Data sheet, and data comes via external connection (ODC file), from an SQL Server view.
They also have one pivot sheet that also needs to be refreshed after the Data sheet is refreshed.
I am trying to find a solution (Windows PowerShell) to automatize the refreshing of Data and Pivot sheets without the need to touch the files.
"Refresh on opening" and other Excel options are not viable because it takes up to 20 minutes to refresh all the connections.
I also don't want to refresh ALL sheets because the file has custom coloring for charts and "Refresh" resets it to Excel default which cannot happen.
I tried this, but it doesn't seem to work with ODC connection? At least, it doesn't do anything.:
Windows PowerShell:
$ExcelApp = new-object -ComObject Excel.Application
$ExcelApp.Visible = $false
$ExcelApp.DisplayAlerts = $false
$Workbook = $ExcelApp.Workbooks.Open("c:\test\ref_test.xlsx", 3, $false, 5, $null, $null, $true)
Start-Sleep -s 30
$Workbook.RefreshAll()
$Workbook|Get-Member *Save*
$Workbook.Save()
$ExcelApp.Quit()
Any ideas?
Office version: 2010, on Windows 7
Possibly the answer on this question can help. The perl script is also available as a pre-compiled exe file.
I would approach this issue by using Excel VBA, and create your Excel file into a .xlsm.
Then update the file w/ Excel VBA commands and functions to refresh your odbc connection, and then save as a new file for distribution.
http://www.vbforums.com/showthread.php?675977-Auto-Open-Refresh-Pivots-Save-Close-Excel-files-using-VB
The following vba routine works well to print a word document
If sHlink <> "" Then
Set OfficeObject = CreateObject("Word.Application")
OfficeObject.Documents.Open sHlink
OfficeObject.PrintOut Background:=False
OfficeObject.Quit
Set OfficeObject = Nothing
End If
but what I need to print are .TIF documents. They open by default with the Microsoft Photo Viewer. Is there something similar that will call the MS Photo Viewer, or failing that, Acrobat? Perhaps with Acrobat could I use some kind of command line?
Thank You
I found this shell command that prints using the Photo Editor that was formerly installed with Office:
Shell """C:\Program Files\Common Files\Microsoft Shared\PhotoEd\photoed.exe"" -p h:\misc\MyPicture.tif"
I think it can be adapted to print using the current Picture Manager - I bet it uses the same -p switch. This seems to be named "OIS.EXE" for some reason.