0

I am using a printer with a continuous feed and the printer requires documents to be printed using the portrait orientation for them to come out like I need them. The documents I am printing however are wider than they are tall and Word 2010 does not allow a portrait page layout in this condition and auto sets the print orientation to match the page orientation. I would like to avoid converting files to .odt and installing extra components. To be clear I need the print orientation to be Portrait and the page orientation to be Landscape. -- this code changed them both

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)
2
  • So basically you want to print it rotated 90 degrees? Do you need it to be done automatically (ie. you'll have to print many documents like that) or is doing something manually an option?
    – gronostaj
    Dec 13, 2013 at 21:53
  • I am creating the documents off of a template that has text boxes in it. I have a separate program that populates the fields and saves the files on a network. They are 4 x 2 inch labels. Because Word 2010 links the page and print orientation, manually adjusting the files did not work. I was hoping I could programmatically change the print orientation and 'trick' Word into printing how I need. I have spent days on msdn looking for something usable.
    – Wayne
    Dec 13, 2013 at 22:13

2 Answers 2

0

You can try exporting to PDF and then printing. Most PDF readers perform page rotation automatically, so you don't need to worry about page orientation. This solution should be fine as long as you're fine with doing it manually for every document, I can't think of any way to do it automatically.

1
  • I got the files to print correctly after exporting to PDF or ODT. For PDF I could not find any solution to print automatically without leaving an Adobe window hanging so I went with ODT in the end. I will post my working code as an answer tomorrow since I have the 8 hour restriction still.
    – Wayne
    Dec 13, 2013 at 23:58
0

I managed to get this working by changing the file type before printing, like gronostaj recommended. This is the key element to my code which is ran in a loop with both string variables being set inside the loop. This code takes a predefined Word template, populates data into the bookmarks, saves it as an ODT, and prints to default printer.

Edit: broke code into subs and a function; verifies that Open Office will be used to print the file and temporarily changes the default printer.

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .