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