MS Word Object Reference issue - vb.net

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()

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

Run Time Error Generated when opening Microsoft Word Document from Excel VBA

Receiving a Runtime Error '13' exception when creating a word application object. I also was experiencing relatively extreme delays (on the order of 20-30 seconds) of running prior to the exception. Any idea what this could be from? I searched Stackoverflow and I haven't seen anything that was very similar.
I have never worked with an word doc from excel before so this is something new to me.
Code Below:
Public Sub GetRawData()
'Meant to translate data from a raw word file and format into excel
Dim filePath As String
Dim objWord As Application
Set objWord = CreateObject("Word.Application")
Dim objDoc As Word.Document
'File management vars
Dim oneLine As Paragraph
Dim lineText As String
filePath = "U:\Research_Dev Docs\DevFolder\Word Doc Translation In Excel For Phys\testWordDoc.docx"
'Set word doc object using standard file directory and file name
Set objDoc = objWord.Documents.Open(Filename:=filePath, Visible:=True)
For Each oneLine In objDoc.Paragraphs
'Pull in each line and eventually parse
lineText = oneLine.Range.Text
'DEBUG OUTPUT TO THE SCREEN FOR TESTING
MsgBox (lineText)
Next oneLine
End Sub

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.

Opening word doc from VBA

I have a subroutine in MS Access that opens a specific word doc file. The gist of the code is
Dim wrdApp As Word.Application
Dim wrdDoc As Word.Document
Dim filepath as String
'Open Word
Set wrdApp = CreateObject("Word.Application")
wrdApp.Visible = True
filepath = CurrentProject.Path & "\Prospect Profiles\Account Profile Form.doc"
'Open the file
Set wrdDoc = wrdApp.Documents.Open(filepath)
So here's the kicker - The subroutine works for all but one of our employees. We're all on the same Citrix environment, and we all have the same version of Access and Word. For this user, the subroutine does not give an error - it smiply opens a blank instance of ms word but it never opens the word doc file. What gives?
Aha! It turns out that this user had made a copy of an older version of our database front end and he wasn't accessing our current, updated version. Thanks for all the help y'all!

Error when creating an instance of Word in 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