1

I came up with this site: http://forums.codeguru.com/showthread.php?15568-WRITING-DATA-TO-WORD-DOCUMENT-FROM-VB

Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrint.Click
    Dim wd As Word.Application
    Dim wdDoc As Word.Document
    wd = New Word.Application
    wd.Visible = True
    wdDoc = wd.Documents.Add("D:\Employees.dotx") 'Add document to Word
    With wdDoc
        .FormFields("E_Name").Result = txtLastName.Text [error]
        .FormFields("E_NName").Result = txtFirstName.Text
        .FormFields("E_Address").Result = txtMiddleName.Text
    End With
    wdDoc.PrintPreview() 'Opens print Preview Window
    wdDoc.SaveAs("D:\doc1.DOC") 'Saves the Document
    wd.Application.Quit() 'Closing Word Application
    wd = Nothing 'Releasing References to Variable
End Sub

Error: The requested member of the collection does not exist.

Can anyone help me with connecting microsoft word with VB.net

pnuts
  • 58,317
  • 11
  • 87
  • 139
Ako Ci Divine
  • 69
  • 3
  • 13

2 Answers2

1

The video you are referring is using vb6. In VS 2010, you can do it like this:

 Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrint.Click
    Static wd1 As Word.Application
    Static wd1Doc As Word.Document
    wd1 = New Word.Application
    wd1.Visible = True
    wd1Doc = wd1.Documents.Add("yourFilePath\profile.dot") 'example: "D:\profile.dot"

    With wd1Doc
        .FormFields("W_Lname").Result = txtLastName.Text  'In VS2010, property `Range` is Readonly. 
        .FormFields("W_Fname").Result = txtFirstName.Text 'You need to use `Result`
        .FormFields("W_Mname").Result = txtMiddleName.Text
    End With

    wd1 = Nothing
    wd1Doc = Nothing
End Sub 

But first, you need to add Microsoft Word <ver> Object Library to your reference.

And if you still see the errors, you might need to import:

Imports Microsoft.Office.Interop

Note: If you are using MS Office 2013, your file extension should be .dotx (profile.dotx)

So you might need to change

wd1Doc = wd1.Documents.Add("yourFilePath\profile.dotx")
                                                    ^
Baby
  • 5,062
  • 3
  • 30
  • 52
  • @Jonjongot **Unable to cast COM object of type 'Microsoft.Office.Interop.Word.DocumentClass' to interface type 'Microsoft.Office.Interop.Word.Application'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{00020970-0000-0000-C000-000000000046}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).** I get this error – Ako Ci Divine Dec 01 '14 at 04:53
  • @AkoCiDivine Have you previously installed other version of Office (other than 2013) before? If yes, then you may refer [this](http://stackoverflow.com/a/12962795/2817802) answer. Your solution should be similar to that case – Baby Dec 01 '14 at 05:50
  • @Jonjongot sorry but i just reformatted my laptop so the first word application i installed is 2k13 – Ako Ci Divine Dec 01 '14 at 05:57
  • @AkoCiDivine Ok this one is beyond my knowledge, and I'm not able to reproduce the error you got too. For now, perhaps you can try google-ing the error. See if someone else have shared similar problem before. – Baby Dec 01 '14 at 06:13
  • sorry but ill not mark your answer as correct, maybe others might try it and doesn't get the right output, thanks by the way – Ako Ci Divine Dec 01 '14 at 06:23
  • @AkoCiDivine Yes of course I definitely have no problem with that :) – Baby Dec 01 '14 at 06:31
  • @AkoCiDivine You may post a comment in [his thread](http://stackoverflow.com/a/18465901/2817802) and give a link to this question. As mentioning him here will not notify him. – Baby Dec 01 '14 at 06:42
  • haha, @Jonjongot i cant comment an his thread, it needs 50 reputation – Ako Ci Divine Dec 01 '14 at 06:57
  • @AkoCiDivine Ah I see. I give you tips, go edit some questions or answers, if your edit approved you will get 2 reputation. You only need 10 edits approved and you can post comment. But make sure your edit is necessary and helpful. Cheers – Baby Dec 01 '14 at 07:02
  • @Jonjongot can you help me with this one http://stackoverflow.com/questions/27222855/inserting-values-ok-in-mysql-database-i-check-box-state-is-checked?noredirect=1#comment42924494_27222855 – Ako Ci Divine Dec 01 '14 at 07:07
0

Try to browse this link and read carefully all the topics http://wiki.smartsimple.com/wiki/Adding_Form_Fields_to_a_MS_Word_Document

Dim wd As Word.Application
    Dim wdDoc As Word.Document
    wd = New Word.Application
    wd.Visible = True
    wdDoc = wd.Documents.Add("D:\Employees\Employees.dotx") 'Add document to Word
    With wdDoc
        .FormFields("E_Name").Result = txtLastName.Text
        .FormFields("E_Nickname").Result = txtNickname.Text
        .FormFields("E_Address").Result = txtAddress.Text
        .FormFields("E_Position").Result = cboPosition.Text

    End With
    wdDoc.SaveAs("D:\Employees\" & txtLastName.Text & "" & txtFirstName.Text & ".DOC") 'Saves the Document
    MsgBox("Please check the folder of employees and open the name of the employee to print his/her document", MsgBoxStyle.OkOnly)
    wd.Application.Quit() 'Closing Word Application
    wd = Nothing 'Releasing References to Variable
    wdDoc = Nothing

That will do the trick, no errors.. Note:Please create a destination folder for your documents like this one "D:\Employees\Employees.dotx", this line of codes instantly save the word document in the destination folder for later printing purposes. Cheers

Ako Ci Divine
  • 69
  • 3
  • 13