Close a word document that is already open - vba

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

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.

How to fill a text box in Microsoft Word with data from a MS Access form?

Hello I am having trouble with trying to auto fill a word document with data that is calculated from my MS Access form.
I have been following along with this Youtube video: https://www.youtube.com/watch?v=k176qcg-YCo and when I run the code it opens word but it gets stuck in a loop, I believe. The function is called when the button is clicked, the code is provided below. Any help is highly appreciated!
Function fillwordform()
Dim appword As Word.Application
Dim doc As Word.Document
Dim Path As String
On Error Resume Next
Error.Clear
Set appword = GetObject(, "word.application")
If Err.Number <> 0 Then
Set appword = New Word.Application
appword.Visible = True
End If
Path = "C:\Users\Jay\Documents\Monthly Update.docx"
Set doc = appword.Documents.Open(Path, , True)
With doc
.FormFields("ItmRec").Result = Me.NVCItmRcv
appword.Visible = True
appword.Activate
End With
Set doc = Nothing
Set appword = Nothing
End Function
The "ItmRec" is the name of the text box im trying to fill in Word and the "Me.NVCItmRcv" is the name of the box that I want to pull the number from and put in the text box. If you need anymore details or anything please let me know! Again highly appreciate any help!

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.

WinWord.exe won't close

I am running into issues creating a Word document Word from Excel using
Set wordapp = CreateObject("word.Application")
Set letter = wordapp.Documents.Add
On first run it works fine, on second run it hangs. I think the issue is if I close the document I created using the Close Button in Word, it won't close WinWord.Exe. I think it has to do with whether I saved the document or not. How can I get around this?
I uninstalled/deactivated all of my Add-ins (I heard that could be an issue) to no avail.
try closing and releasing the objects
Set wordapp = CreateObject("word.Application")
Set letter = wordapp.Documents.Add
'release the letter object
set letter = Nothing
'quit the word appication
wordApp.Quit
'release the app object
set wordapp = Nothing

Close/Release Word object in VBA?

I have the following code to open the manual for an Excel Workbook application I have developed:
Sub OpenManual()
'Word.Application.Documents.Open "\\filePath\FormFlow To MSExcel\FeedSampleReport-Manual.docx"
Dim objWord As Object
Set objWord = CreateObject("Word.Application")
objWord.Visible = True
objWord.Documents.Open "\\filePath\FormFlow To MSExcel\FeedSampleReport-Manual.docx"
End Sub
This gives me 2 issues though:
The document opens, but in the background. The user doesn't know the document has opened unless they know to check Microsoft Word in the Taskbar.
When I try to close the word document I receive:
This file is in use by another application or user. (C:\Users\Me\AppData...\Normal.dotm)
When I click ok on that dialogue, I receive a "Save As" screen.
If I cancel out of that and try to close the blank Microsoft Word instance I then get:
Changes have been made that affect the global template, Normal. Do you want to save those changes?
Then if I click No, everything finally closes.
Can anyone help me out with these 2 issues? Do I need to release the object somehow? Have never seen this before.
EDIT:
After trying #Layman-Coders method:
Sub OpenManual()
'Word.Application.Documents.Open "\\filePath\FormFlow To MSExcel\FeedSampleReport-Manual.docx"
'Open an existing Word Document from Excel
Dim objWord As Object
Set objWord = CreateObject("Word.Application")
objWord.Visible = True
' Should open as the forefront
objWord.Activate
'Change the directory path and file name to the location
'of the document you want to open from Excel
objWord.Documents.Open "\\filePath\FormFlow To MSExcel\FeedSampleReport-Manual.docx"
objWord.Quit
Set objWord = Nothing
End Sub
When I have one other word document open and click the button, the following occurs:
Manual opens in the forefront, but I immediately receive This file is in use by another application or user. (C:\Users\Me\AppData\...\Normal.dotm)
I press OK and receive the Save As dialogue.
Cancel out of the Save As dialogue and am presented my Manual document.
When I click the Red X to close the document, I receive Changes have been made that affect the global template, Normal. Do you want to save those change? I click No and the document closes.
If this document is the first instance of word I have opening:
The document opens.
As soon as code hits the objWord.Quit line the document immediately closes.
I am just wanting the document to open to the forefront allowing users to view the Manual for assistance when they need it, and let them close the document at their discretion.
So the problem you are having with Word asking you to save the global template is because there is already a copy Word open which has rights to the Normal template. When you use CreateObject to set your Word object you are loading up Word a second time which opens Normal template as read only.
What you need to do is check if Word is open or not and if it is grab that copy of Word. If it's not then you can open up Word.
Sub OpenManual()
Dim objWord As Object
'We need to continue through errors since if Word isn't
'open the GetObject line will give an error
On Error Resume Next
Set objWord = GetObject(, "Word.Application")
'We've tried to get Word but if it's nothing then it isn't open
If objWord Is Nothing Then
Set objWord = CreateObject("Word.Application")
End If
'It's good practice to reset error warnings
On Error GoTo 0
'Open your document and ensure its visible and activate after openning
objWord.Documents.Open "\\filePath\FormFlow To MSExcel\FeedSampleReport-Manual.docx"
objWord.Visible = True
objWord.Activate
Set objWord = Nothing
End Sub
One other nice line of code is to surpress display alerts. This will stop the 'do you want to save' type dialog boxes from appearing.
objWord.DisplayAlerts = 0
Try something like this:
Sub OpenManual()
'Word.Application.Documents.Open "\\filePath\FormFlow To MSExcel\FeedSampleReport-Manual.docx"
Dim objWord As Object
Set objWord = CreateObject("Word.Application")
objWord.Visible = True
objWord.Activate 'Should make it the forefront (1)
objWord.Documents.Open "\\filePath\FormFlow To MSExcel\FeedSampleReport-Manual.docx"
'If you just want to close it afterwards...
objWord.Quit 'Changes are discarded
'Either way, you should have the following somewhere
Set objWord = Nothing
End Sub
Setting the object to nothing should eliminate the connection
I had the same problem. And only code below closed this Word window:
Dim X As Variant
X = Shell("powershell.exe kill -processname winword", 1)
I found this code in one of the answers Closing word application from excel vba