Thursday, April 12, 2012

VB.NET - Margin Outside of Printable Area

Problem: You are trying to print something in Microsoft Word through VB.NET but get stopped with a message telling you the margins are outside of the printable area and you have to click to continue printing.  You do not want this message to come up but instead go straight to printing.

Solution: You must turn off the the error messaging and backgroud printing, then print the document, then you can turn everything back on the way it was.  Here is the code snippet to show you how.



Dim objWordApp As Word.Application
Dim blnWordAlerts As Boolean


'............
'code
'............

' Store current Background Printing setting.
blnWordAlerts = objWordApp.Options.PrintBackground

' Turn off error messaging and Background Printing.
objWordApp.Application.DisplayAlerts = Word.WdAlertLevel.wdAlertsNone
'objWordApp.wdAlertsNone()
objWordApp.Options.PrintBackground = False

' Print the active document.
objWordApp.ActiveDocument.PrintOut(False) 'false allows the print to finish before it moves to any other code

' Turn on error messaging and restore Background Printing
' to original setting.
objWordApp.Application.DisplayAlerts = Word.WdAlertLevel.wdAlertsAll
objWordApp.Options.PrintBackground = blnWordAlerts

No comments:

Post a Comment