Close / Free Word object in VBA?

I have the following code to open the guide for the Excel Workbook application that I 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 questions:

  • The document opens, but in the background. The user does not know that the document was opened if they do not know to check Microsoft Word on the taskbar.
  • When I try to close a document document, I get: This file is being used by another application or user. (C: \ Users \ Me \ AppData ... \ Normal.dotm)

When I click ok in this dialog, I get the "Save As" screen.

If I canceled this and try to close an empty instance of Microsoft Word, I get:

Changes have been made that affect the global Normal template. Do you want to save these changes?

Then, if I click No, everything will finally close.

Can someone help me with these two issues? Do I need to somehow free an object? Never seen this before.

EDIT

After using the @ 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 another Word document open and click the button, the following will happen:

  • At the initial stage, the manual opens, but I immediately get This file is in use by another application or user. (C:\Users\Me\AppData\...\Normal.dotm) This file is in use by another application or user. (C:\Users\Me\AppData\...\Normal.dotm)
  • I click OK and I get the Save As dialog.
  • Cancel the “Save As” dialog and present my “Guide” document.
  • When I click the "Red X" button to close the document, I get Changes have been made that affect the global template, Normal. Do you want to save those change? 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 the word that I open:

  • The document opens.
  • As soon as the code hits the objWord.Quit line, the document closes immediately.

I just want the document to come to the forefront, allowing users to view the Guide for help when they need it, and let them close the document as they see fit.

+4
source share
3 answers

Thus, the problem that you encounter with Word, offering to save the global template, is that there is already a copy of Word open that has rights to the Normal template. When you use CreateObject to set a Word object, you load Word a second time, which opens a regular read-only template.

What you need to do is check if Word is open or not, and if it captures this copy of Word. If not, you can open 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 nothing then it isn't open If objWord Is Nothing Then Set objWord = CreateObject("Word.Application") End If 'It 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 

Another nice line of code is disabling display alerts. This will bring up the “you want to save” dialog boxes.

objWord.DisplayAlerts = 0

+10
source

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 an object to nothing should eliminate the connection

+2
source

I had the same problem. And only the 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. Final Word Application from excel vba

0
source

Source: https://habr.com/ru/post/1486915/


All Articles