在 VBA 中关闭/释放 Word 对象?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17177412/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 15:54:12  来源:igfitidea点击:

Close/Release Word object in VBA?

vbaexcel-vbams-wordexcel-2007word-vba

提问by Analytic Lunatic

I have the following code to open the manual for an Excel Workbook application I have developed:

我有以下代码可以打开我开发的 Excel 工作簿应用程序的手册:

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:

这给了我两个问题:

  1. 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.
  2. 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)
  1. 文档打开,但在后台。用户不知道文档已经打开,除非他们知道在任务栏中检查 Microsoft Word。
  2. 当我尝试关闭我收到的 word 文档时: 该文件正在被另一个应用程序或用户使用。(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:

如果我取消它并尝试关闭空白的 Microsoft Word 实例,我将得到:

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

已做出影响全局模板 Normal 的更改。是否要保存这些更改?

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:

尝试@Layman-Coders 方法后:

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:

当我打开另一个 Word 文档并单击该按钮时,会发生以下情况:

  1. 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)
  2. I press OK and receive the Save As dialogue.
  3. Cancel out of the Save As dialogue and am presented my Manual document.
  4. 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.
  1. 手册在最前面打开,但我立即收到 This file is in use by another application or user. (C:\Users\Me\AppData\...\Normal.dotm)
  2. 我按 OK 并收到另存为对话框。
  3. 取消另存为对话框并显示我的手册文档。
  4. 当我单击红色 X 关闭文档时,我收到Changes have been made that affect the global template, Normal. Do you want to save those change?我单击否并关闭文档。

If this document is the first instance of word I have opening:

如果此文档是我打开的第一个单词实例:

  1. The document opens.
  2. As soon as code hits the objWord.Quitline the document immediately closes.
  1. 文档打开。
  2. 一旦代码到达该objWord.Quit行,文档就会立即关闭。

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.

我只是想让文档打开到最前面,允许用户在需要时查看手册以获得帮助,并让他们自行决定关闭文档。

回答by CuberChase

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 CreateObjectto set your Word object you are loading up Word a second time which opens Normal template as read only.

因此,Word 要求您保存全局模板的问题是因为已经打开了一个 Word 副本,它有权访问 Normal 模板。当您CreateObject用来设置 Word 对象时,您将再次加载 Word,这会以只读方式打开 Normal 模板。

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.

您需要做的是检查 Word 是否打开,以及它是否抓取了 Word 的副本。如果不是,那么您可以打开 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

objWord.DisplayAlerts = 0

回答by dennythecoder

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

将对象设置为空应该消除连接

回答by Sharunas Bielskis

I had the same problem. And only code below closed this Word window:

我有同样的问题。只有下面的代码关闭了这个 Word 窗口:

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

我在excel vba Closing word application的答案之一中找到了此代码