My current PowerShell script:
$document = "C:\\test.doc"
$word = new-object -comobject word.application
$word.Visible = $false
$word.DisplayAlerts = "wdAlertsNone"
$word.AutomationSecurity = "msoAutomationSecurityForceDisable"
$doc = $word.Documents.Open($document)
$word.ActivePrinter = "\\http://ptr-server:631\pdf-printer"
$background = $false
$doc.PrintOut([ref]$background)
$doc.close([ref]$false)
$word.quit()
But it results in an alert box The macros in this project are disabled. Please refer to the online help or documentation of the host application to determine how to enable macros.
How can I open the document without it running the AutoOpen
macro or displaying any sort of dialog prompt?
Environment Details:
Turns out this is MUCH easier to do in VB.NET than in C# (which I never could figure out). But all you would need to do is create, say, a console application with a single routine. Here are the instructions:
Imports word = Microsoft.Office.Interop.Word
Module Module1
Sub Main()
Dim args() As String = Environment.GetCommandLineArgs
Dim path = args(1)
Dim printer = args(2)
Dim wordApp As word.Application = New word.Application
wordApp.WordBasic.DisableAutoMacros(1)
wordApp.Visible = False
Dim doc As word.Document = wordApp.Documents.Open(path)
wordApp.ActivePrinter = printer
Dim background As Object = False
doc.PrintOut(background)
doc.Close(False)
wordApp.WordBasic.DisableAutoMacros(0)
wordApp.Quit()
End Sub
End Module
Module1
and insert the code above.$wordprinterpath
.$doc
and $printer
, respectively.$wordprinterpath = "C:\\path\\wordprinter.exe" $doc ="""C:\\Users\\me\\Documents\\Your doc.doc""" $printer = "\\http://ptr-server:631\pdf-printer" Invoke-Expression "$wordprinterpath $doc $printer" | out-Null
You should be good to go after this. I haven't tested the printing part of this, so that may need some work, but disabling of the auto-macros and opening the doc works.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With