我正在使用具有连续进纸功能的打印机,并且该打印机要求以纵向打印文档,以便像我需要的那样出来。但是,我要打印的文档比它们的高大,并且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)
我设法通过在打印之前更改文件类型来使此工作正常进行,就像建议的gronostaj一样。这是我的代码的关键元素,代码在循环中运行,两个字符串变量都在循环中设置。此代码采用预定义的Word模板,将数据填充到书签中,将其另存为ODT,然后打印到默认打印机。
编辑:将代码分解为subs和一个函数;验证将使用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
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句