更改打印方向而不更改 .docx 的页面方向

更改打印方向而不更改 .docx 的页面方向

我使用的是连续送纸打印机,打印机要求使用纵向打印文档,这样文档才能按我需要的方式打印出来。但是,我打印的文档宽度大于高度,而 Word 2010 不允许在这种情况下使用纵向页面布局,并自动将打印方向设置为与页面方向匹配。我想避免将文件转换为 .odt 并安装额外的组件。需要明确的是,我需要将打印方向设置为纵向,将页面方向设置为横向。——此代码将它们都更改了

Dim oWord As Word.Application
Dim oDoc As Word.Document

oWord = CreateObject("Word.Application")
oWord.Visible = False
oDoc = oWord.Documents.Add("C:\Users\lmartin\Desktop\Template.docx")

oDoc.PageSetup.Orientation = Word.WdOrientation.wdOrientPortrait
oWord.PrintOut()
oWord.Quit(SaveChanges:=Word.WdSaveOptions.wdDoNotSaveChanges)

答案1

您可以尝试导出为 PDF 然后打印。大多数 PDF 阅读器都会自动执行页面旋转,因此您无需担心页面方向。只要您愿意为每个文档手动执行此操作,此解决方案应该没问题,我想不出任何自动执行此操作的方法。

答案2

我设法通过在打印之前更改文件类型来实现这一点,就像 gronostaj 推荐的那样。这是我的代码的关键元素,它在循环中运行,两个字符串变量都在循环内设置。此代码采用预定义的 Word 模板,将数据填充到书签中,将其保存为 ODT,然后打印到默认打印机。

编辑:将代码分解为子代码和函数;验证 Open Office 是否将用于打印文件并暂时更改默认打印机。

Includes Word = Microsoft.Office.Interop.Word

Dim content As String
Dim finishedFile As String

Public Sub main
If checkForOpenOffice() Then
// start of loop
content = "value"
finishedFile = "value"
generateFile()
printFile()
// end of loop
Else
// error message
End If
End Sub


Private Sub generateFile(ByRef content As String, ByRef FinishedFile As String)
Dim oWord As Word.Application
Dim oDoc As Word.Document

oDoc = oWord.Documents.Add("C:\Users\lmartin\Template.dotx")
oDoc.Bookmarks.Item("Bookmark").Range.Text = content
oDoc.SaveAs2("C:\Users\lmartin\Desktop\" & finishedFile & ".odt", Word.WdSaveFormat.wdFormatOpenDocumentText)
oWord.Quit(SaveChanges:=Word.WdSaveOptions.wdDoNotSaveChanges)
End Sub

Private Sub printFile(finishedFile)
Dim printer As String
Dim pr As New PrintDocument
printer = pr.PrinterSettings.PrinterName
pr.Dispose()

Shell(String.Format("rundll32 printui.dll,PrintUIEntry /y /n ""{0}""", "\\NetworkName\PrinterName"))
Dim p As New Process()
p.StartInfo.Verb = "print"
p.StartInfo.CreateNoWindow = False
p.StartInfo.FileName = "C:\Users\lmartin\" & finishedFile & ".odt"
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden
p.Start()
p.WaitForExit()
p.Close()
Shell(String.Format("rundll32 printui.dll,PrintUIEntry /y /n ""{0}""", printer))
End Sub

Private Function checkForOpenOffice
Dim odt = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(".odt")
Dim linkedValue = odt.GetValue("")
Dim linkedKey = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(linkedValue)
Dim openWith = linkedKey.OpenSubKey("Shell\Open\Command").GetValue("")
Dim O As String = CStr(openWith)

If Not O.Contains("swriter.exe") Then
Return False
Else
Return True
End If

End Function

相关内容