Error when creating an instance of Word in VB.net - vb.net

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?

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?

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

Related

Office application hangs during document opening via interop

I am trying to open a file but the Office application hangs during document opening in VB.NET.
I have this code:
Dim oProp As Object
Dim strPropValue As String
Dim lngRetVal As Integer
Dim strmsg As String
Dim lngretcode As Integer
Dim strPropertyName As String
Dim oWordDoc As Word.Document
Dim ObjOfficeAPP As Object
ObjOfficeAPP = New Word.Application()
GetWORDKEYS = cstFAILURE
ObjOfficeAPP.DisplayAlerts = WdAlertLevel.wdAlertsAll
ObjOfficeAPP.Application.Visible = True
oWordDoc = ObjOfficeAPP.Documents.Open(FileName:=strpFileName, Visible:=False)
I have problems on the line:
oWordDoc = ObjOfficeAPP.Documents.Open(FileName:=strpFileName, Visible:=False)
The debugger hangs on the Documents.Open() call, and just stays there waiting - without firing any type of exception or error. We have looked in the event log but only found the following.
My problem is, how can I set to open this document and not to block on the line with the Documents.Open() call?
Here are few points that could help:
Don't use the Application property for setting the Visible property:
ObjOfficeAPP.Visible = True
Don't set the DisplayAlerts property before opening a document.
Use the System.Reflection.Missing.Value for missing arguments.
You may find the How to automate Word from Visual Basic .NET to create a new document article helpful which explains the required steps for automating Word and provides a sample code:
'Start Word and open the document template.
oWord = CreateObject("Word.Application")
oWord.Visible = True
oDoc = oWord.Documents.Add

Create Object for handling word on the fly (VBA)

I'm using VBA for creating an instance of word currently by adding a reference to the library, but this cause an issue, because there existing some machines without word.
This cause instantly a runtime error at startup on these machines. It's not possible to catch this error.
However, I try to create an object in VBA on the fly by something like this
Dim oWshShell As WshShell
Set oWshShell = New WshShell
' *** TEST REGESTRY
Dim tmp As String
tmp = oWshShell.RegRead("HKEY_CURRENT_USER\SOFTWARE\Microsoft\Office\15.0\Word\Options\PROGRAMDIR")
Debug.Print tmp
tmp = tmp + "winword.exe"
Dim oWord As Object
Set oWord = Shell(tmp)
But my problem is that oWord is not an Object of Word.Application. So how to handle this?
Would be nice to get available all functionalities like with Word.Application.
You don't have to use a shell, use COM factory directly:
Function openWordApp() As Object
On Error Resume Next
Set openWordApp = CreateObject("Word.Application")
If openWordApp Is Nothing Then
msgBox "Word not installed on this machine"
Else
openWordApp.Visible = True
End If
End Function
In the caller, check if the returned value Is Nothing.

Word.application.Selection.Find.Text With multiple Word applications open

I'm having a problem where when I use the Word.Application.Selection.Find set of commands, I run into a NullReferenceException. In this program I initialize a new Word document as such:
Dim wrdApp As Word.application = New Word.Application
Dim wrdDoc As Word.Document = New Word.Document
wrdApp.Visible = True
With wrdDoc
wrdApp.Selection.Find.ClearFormatting()
wrdApp.Selection.Find.Text = sequenceObject(i, 1, 1, 1).cat
wrdApp.Selection.Find.Forward = True
wrdApp.Selection.Find.Wrap = Word.WdFindWrap.wdFindContinue
wrdApp.Selection.Find.Execute
End With
When I have no windows open prior to the code running, I have no problem. If I have another word document open at the time, the error occurs at the first Find command; it used to occur at the ClearFormatting line. I commented it out, then it occurred at the Find.Text line. I have the correct object libraries referenced and imported.
I figured it out, I wasn't actually specifying which document to select. By using
wrdApp.ActiveDocument.ActiveWindow
I was able to solve the problem.

MS Word Object Reference issue

what object reference do I need to make this function work .
Dim oDoc As Word.Application =
I have already selected Microsoft Word 12.0 Object Library
but i still get this error
this function simply finds and opens the Ms Word.exe then takes a file and adds it to the Ms Word file .
I'm sure that i'm missing an object reference but i don't not know which it is .
No missing reference. But you are trying to cast a Word.Document to Word.Application with oDoc, the cast is not valid.
It should be
Dim oDoc As Word.Document = oWord.Documents.Add()
Instead of
Dim oDoc As Word.Application = oWord.Documents.Add()

opening excel in vb.net error

what do you think is wrong with the commented line in my vb.net code below? it returns the error message "member not found". thank you.
Public Function tae(ByVal SourcePath As String)
Dim oxlApp As Excel.Application
Dim oxlBook As Excel.Workbook
Dim oxlSheet As Excel.Worksheet
oxlApp = CType(CreateObject("Excel.Application"), Excel.Application)
oxlBook = CType(oxlApp.Workbooks.Open(SourcePath), Excel.Workbook) //somethings wrong here
oxlSheet = CType(oxlBook.Worksheets(1), Excel.Worksheet)
oxlApp.Workbooks.Close()
oxlApp.Quit()
End Function
i tried to add the following references and its now ok:
ms excel x object library
ms office x object library
visual basic for applications