Word.application.Selection.Find.Text With multiple Word applications open - vb.net

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.

Related

Ignore an "external" error when opening a Word document

I'm attempting to copy the contents of a word document, but the document has embedded vba which causes a compile error anytime I open it.
The error reads
"The code in this project must be updated for use on 64-bit systems."
I expect this error to occur, so how do I tell my vba code in Access to ignore the errors without changing the word doc?
My vba code so far - the DisplayAlerts = wdAlertsNone does not work, neither does setting it to False:
Private Sub cmdQuickLtr_Click()
Dim wApp As Word.Application
Set wApp = CreateObject("Word.Application")
wApp.DisplayAlerts = wdAlertsNone
Dim doc As Object
Set doc = wApp.Documents.Open(Me.tbLetterPath.Value)
doc.Content.Copy
doc.Close
'do something with the copied content
wApp.DisplayAlerts = wdAlertsAll
Set doc = Nothing
Set wApp = Nothing
End Sub
Thank you all in advance.

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

VB.Net working with ActiveDocument in an already running instance of word

I have a small VB.Net application which opens a new word document from a template (opens as Document1.docx for example). It runs some code to find a replace some text.
It then allows the user to edit the open document. There is a small winform open with a button called 'Complete'. When the user clicks this, I want it to save the document and do some further editing etc.
However, I cannot get the code to work to see the active document in the running instance of Word.
Whenever I refer to the activedocument, it throws and exception saying there are no documents open.
The code which is looking to save the active document is in a separate sub from where the Word.Application object is created. Word is running with the document open but it still fails.
I've tried the following to make sure it is getting the instance of word that is open;
WordApp = Marshal.GetActiveObject("Word.Application")
But is still does not see any open documents.
Any suggestions?
EDIT #1 - Sorry for not including the code;
This is the code that starts Word and does some basic find/replace. It then starts form3 which stays in place whilst the user edits the word document. This code runs from a button click on form 2;
Dim oWord As Word.Application
Dim oDoc As Word.Document
Dim path As String
path = "C:\Templates/"
Try
oWord = CreateObject("Word.Application")
oWord.Visible = True
Catch ex As Exception
''Some error handling code not included for the purpose of StackOverflow
End Try
Try
oDoc = oWord.Documents.Add(path & "MG11.dot")
Catch ex As Exception
''Some error handling code not included for the purpose of StackOverflow
End Try
With oWord.Selection.Find
''Find & Replace Code - this works so is not included for Stackoverflow
End With
Form3.Show()
Me.WindowState = FormWindowState.Minimized
Form1.WindowState = FormWindowState.Minimized '(form 1 has no code which effects Word)
End Sub
Form 3 has one button, which when pressed should save the word document. It will give it a filename based on info from elsewhere. Only the basic code is shown here - I just need to get vb.net to see the active document for now. This code is ran on Button_1 Click for Form 3.
Dim WordApp As Word.Application
Dim oDoc As Word.Document = WordApp.ActiveDocument
oDoc = WordApp.ActiveDocument
I've also tried;
Dim WordApp As Word.Application
WordApp = Marshal.GetActiveObject("Word.Application")
Dim oDoc As Word.Document
oDoc = WordApp.ActiveDocument
'once oDoc is set properly I will use SaveAs2 to handle the saving etc
It fails on WordApp.ActiveDocument, stating there are no open documents.
Either make sure oWord is in scope in your Form3. It might already be in scope or you add a Public Word.Applications property to your Form3 and set that property before showing the form.
In the first case you access the ActiveDocument on the existing oWord object in scope, in the second you set the property on the form and access that before showing.
oWord.ActiveDocument

Close a word document that is already open

I'm trying to check whether a specific word document is open or not. If open, then close it and first then reopen again and if not then just open it.
The problem is that I always get a Bad file name 4160 error when it tries to close the document.
Would anyone plz tell me where exactly is the problem in my code.
Thank u in advance.
Dim wdApp As Object
Dim myDoc As Word.Document
Set wdApp = GetObject(, "Word.Application")
If IsFileOpen("C:\Letters\TemporaryLetter.docx") Then
wdApp.Documents("C:\Letters\TemporaryLetter.docx").Close
End If
With wdApp
.Visible = True
.WindowState = 2
End With
Set myDoc = wdApp.Documents.Open("C:\Letters\TemporaryLetter.docx")
Without seeing the function "IsFileOpen" that you are calling I"m guessing it requires the short name of the file (e.g. "TemporaryLetter.docx") not the full path?? Try:
If IsFileOpen("TemporaryLetter.docx") Then
wdApp.Documents("TemporaryLetter.docx").Close
End If
try this.
Set myDoc = word.Documents.Open ("C:\Letters\TemporaryLetter.docx")
If IsFileOpen(mydoc) Then
mydoc.Close(False //Here if you want to save your document or not)
End If

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!