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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27180737/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 18:31:53  来源:igfitidea点击:

How to print output to MS Word 2013 using Word Template in VB?

vb.netprintingms-word

提问by Ako Ci Divine

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

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

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

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

任何人都可以帮助我将 microsoft word 与 VB.net 连接

回答by Baby

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

您所指的视频正在使用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 

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

但首先,您需要添加Microsoft Word <ver> Object Library到您的参考中。

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)

注意:如果您使用的是 MS Office 2013,您的文件扩展名应该是.dotx( profile.dotx)

So you might need to change

所以你可能需要改变

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

回答by Ako Ci Divine

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

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

这样就可以了,没有错误.. 注意:请为您的文档创建一个目标文件夹,例如“D:\Employees\Employees.dotx”,这行代码立即将 word 文档保存在目标文件夹中以供以后打印目的。干杯