Error when creating an instance of Word in VB.net

こ雲淡風輕ζ 提交于 2019-12-24 17:27:15

问题


I am getting an error when I run this app in VS 2010 (it works fine in VS 2008)

Private Sub GenerateInvoice()

Dim emptyObject As Object = System.Reflection.Missing.Value

Dim wordApp As New Word.Application
wordApp.Visible = True

Dim InvoiceDoc As New Word.Document
InvoiceDoc = wordApp.Documents.Add(InvoicePath, emptyObject, emptyObject, emptyObject)

Dim totalFields As Integer = 0

For Each mergeField As Word.Field In InvoiceDoc.Fields

The error occurs at the For Each line

"Object reference not set to an instance of an object."

Am I missing something here?


回答1:


Maybe the InvoicePath used in the instance run via VS2010 is invalid and so the call to Documents.Add fails?

Are you running both VS2010 and VS2008 on the same machine? And is the InvoicePath set to the exact same path in both instances?




回答2:


Try

 Dim InvoiceDoc As Word.Document
 wordApp.Documents.Add(InvoicePath, emptyObject, emptyObject, emptyObject)
 InvoiceDoc=wordApp.ActiveDocument


来源:https://stackoverflow.com/questions/4184593/error-when-creating-an-instance-of-word-in-vb-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!