如何使用VB中的Word模板将输出打印到MS Word 2013?

时间:2014-11-28 01:24:34

标签: vb.net printing ms-word

我想出了这个网站: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

错误:The requested member of the collection does not exist.

任何人都可以帮我将微软的单词与VB.net联系起来

2 个答案:

答案 0 :(得分:1)

您所引用的视频正在使用vb6。在VS 2010中,您可以这样做:

 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 

但首先,您需要在引用中添加Microsoft Word <ver> Object Library

如果您仍然看到错误,则可能需要导入:

Imports Microsoft.Office.Interop

注意:如果您使用的是MS Office 2013,则文件扩展名应为.dotxprofile.dotx

所以你可能需要改变

wd1Doc = wd1.Documents.Add("yourFilePath\profile.dotx")
                                                    ^

答案 1 :(得分:0)

尝试浏览此链接并仔细阅读所有主题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

那就行了,没有错误.. 注意:请为此文档创建目标文件夹 &#34; D:\ Employees \ Employees.dotx&#34;,这行代码会立即将word文档保存在目标文件夹中,以便以后进行打印。干杯