I have created a vb.net application (website) that allows the user to fill out a form and save the data to a SQL database, then retrieve the data for later display. The last requirement is to be able to print out a completed form. I am using a Word .dotx template and performing a Word replace to fill out the template. then sending it to the printer. This all works as expected when debugging but when published to IIS on a remote server the print job fails without error. Both my development box and the server have the same version of MS Office installed.
I am doing the replace in the following way:
Dim wordApp As Word.Application = Nothing
Dim document As Word.Document = Nothing
Try
wordApp = New Word.Application() With {.Visible = False}
Dim documentName As String = "SensoryForm.dotx"
Dim projectRoot As String = AppDomain.CurrentDomain.BaseDirectory
Dim wordPath As String = Path.Combine(projectRoot, documentName)
document = wordApp.Documents.Add(Template:=wordPath)
document.Select()
Then using the data to do all the replacing in the Word template. And then trying to print using:
document.Application.ActiveDocument.PrintOut()
'document.Application.ActiveDocument.PrintPreview()
Catch ex As Exception
Finally
If document IsNot Nothing Then
document.Close(Word.WdSaveOptions.wdDoNotSaveChanges)
End If
If wordApp IsNot Nothing Then
wordApp.Quit()
End If
End Try
This is my first time trying to print from IIS on a remote server and I don't know what I'm missing. Why does this work when debugging locally but fail without throwing an exception on the server?
It would also be acceptable for the client to automatically download (save) the completed Word document.
