Question Open a Word template inside a winform?

gate7cy

Well-known member
Joined
May 11, 2009
Messages
119
Programming Experience
3-5
hello there. I am trying to open a word template inside a form in my vb.net application but I cannot seem to find any guidance to doing this. I do not want to call microsoft word to open in order to see/read/write on my template. I want the word document to be within a form I am creating like a print preview sort off. I am using Word 2007 and VS 2008. I have been using so far automation and calling and interacting with the template but until so far I have to open Microsoft word to open it for me. Any clues?? Thanks for the replies guys.
 
I have done this so far. I used a word template and added some bookmarks to send data from my vb application and print the document. I manage to print the document without word openning but I cannot preview the document. The only option is to open Word. I have tried to use the printpreviewcontrol with no luck. This is the code so far :
VB.NET:
Dim conn As MySqlConnection = New MySqlConnection("server=******;Port=3306;database=*****;user id=root;password=******;")
        conn.Open()
        Dim query As String
        query = " SELECT Street, HouseNum, Town, CustomerID, PostCode FROM customer WHERE CustomerID = " & Me.custidtext.Text & ""
        Dim cmd As MySqlCommand = New MySqlCommand(query, conn)
        Dim reader As MySqlDataReader
        reader = cmd.ExecuteReader(CommandBehavior.CloseConnection)
        reader.Read()


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


        'Start Word and Open document template

        oWord = CType(CreateObject("Word.Application"), Word.Application)
        oWord.Visible = False


        oDoc = oWord.Documents.Add("C:\Visual Studio 2008\Projects\form.doc")

        oDoc.Bookmarks("customer").Range.Text = Me.customertext.Text

        oDoc.Bookmarks("description").Range.Text = Me.problem.Text

        oDoc.Bookmarks("address").Range.Text = reader("Street").ToString + " " + reader("HouseNum").ToString + vbCrLf + reader("Town").ToString
        oDoc.Bookmarks("model").Range.Text = Me.model.Text
        oDoc.Bookmarks("serial").Range.Text = Me.serial.Text
        oDoc.Bookmarks("reportedat").Range.Text = CStr(Me.datesched.Value)
        oDoc.Bookmarks("solution").Range.Text = Me.solution.Text
        oDoc.Bookmarks("finished").Range.Text = CStr(Me.datecompleted.Value)
        oDoc.Bookmarks("postcode").Range.Text = reader("PostCode").ToString
        oDoc.Bookmarks("town").Range.Text = reader("Town").ToString
        oDoc.Bookmarks("rmaid").Range.Text = Me.serviceidtext.Text
        oDoc.Bookmarks("datenow").Range.Text = CStr(Date.Now)
        oDoc.Bookmarks("technician").Range.Text = Me.technician.Text
        oDoc.Bookmarks("technician").Range.Text = Me.techniciantext.Text
        TEST.PrintPreviewControl1.Document = CType(oDoc, Printing.PrintDocument)

        TEST.Show()

With this I get this error :
Unable to cast COM object of type 'Microsoft.Office.Interop.Word.DocumentClass' to class type 'System.Drawing.Printing.PrintDocument'. Instances of types that represent COM components cannot be cast to types that do not represent COM components; however they can be cast to interfaces as long as the underlying COM component supports QueryInterface calls for the IID of the interface.

Any clues..... anyone? Thank you.
 
You can't render a Word document without the Word engine. There is no way to convert a Word.Document to a PrintDocument. The Word automation Document object has a PrintPreview method for instance. To show a Word document in a form you can use the WebBrowser control as a host, but it requires Word installed.
 
Back
Top