关闭/释放VBA中的Word对象? [英] Close/Release Word object in VBA?

查看:520
本文介绍了关闭/释放VBA中的Word对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  Sub OpenManual()

'Word.Application.Documents.Open\\filePath\FormFlow到MSExcel\FeedSampleReport-Manual.docx

Dim objWord As Object
设置objWord = CreateObject(Word.Application)
objWord.Visible = True

objWord.Documents.Open\\filePath\FormFlow到MSExcel\FeedSampleReport-Manual。 docx

End Sub

这给了我2个问题: p>


  1. 文档打开,但在后台。用户不知道文档已经打开,除非他们知道在任务栏中检查Microsoft Word。

  2. 当我尝试关闭我收到的单词文档:
    此文件正在被其他应用程序或用户使用。 (C:\Users\Me\AppData ... \Normal.dotm)

当我在对话框上点击OK时,我收到一个另存为屏幕。



如果我取消了这一点,并尝试关闭空白的Microsoft Word实例I然后得到:



已经对影响全局模板Normal进行了更改。您要保存这些更改吗?



然后如果我点击否,所有内容都会关闭。



<有人可以帮我解决这两个问题吗?我需要以某种方式释放对象吗?从来没有见过这个。



编辑



编码方法:

  Sub OpenManual()
'Word.Application.Documents.Open\\filePath\\ \\ FormFlow到MSExcel\FeedSampleReport-Manual.docx

'从Excel打开现有的Word文档
Dim objWord As Object
设置objWord = CreateObject(Word.Application )
objWord.Visible = True

'应该打开前端
objWord.Activate

'将目录路径和文件名更改到该位置
'您要从Excel
objWord.Documents.Open\\PathPath\FormFlow到MSExcel\FeedSampleReport-Manual.docx

objWord.Quit
Set objWord = Nothing

End Sub

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


  1. 手动打开最前沿,但我立即收到该文件正在由另一个应用程序或用户使用。 (C:\Users\Me\AppData\ ... \Normal.dotm)

  2. 我按OK并收到另存为对话框

  3. 取消另存为对话框,并提供我的手册文档。

  4. 当我点击红色X关闭文档时,我收到已经对影响全局模板Normal进行了更改。您要保存这些更改吗?我点击否,文档关闭。

如果此文档是第一个字我开了:


  1. 文档打开。

  2. 点击 objWord.Quit 行文档立即关闭。

我只是希望文档打开最前沿,允许用户在需要时查看手册以获得帮助,并让他们酌情关闭文档。

解决方案

所以Word所要求您保存全局模板的问题是因为已经有一个Word打开的副本具有普通模板的权限。当您使用 CreateObject 设置您的Word对象时,您将第二次加载Word,将普通模板打开为只读。



您需要做的是检查Word是否打开,如果是抓住Word的副本。如果不是那么你可以打开Word。

  Sub OpenManual()
Dim objWord As Object

'我们需要继续错误,因为如果Word不是
'打开GetObject行将给出一个错误
On Error Resume Next
设置objWord = GetObject(,Word.Application)

'我们试图得到Word,但如果没有,那么它不是打开
如果objWord是没有,然后
设置objWord = CreateObject(Word.Application)
结束If

'重置错误警告是一个很好的做法
On Error GoTo 0

'打开您的文档并确保其显示并在打开
objWord后激活。 Documents.Open\\filePath\FormFlow到MSExcel\FeedSampleReport-Manual.docx
objWord.Visible = True
objWord.Activate

设置objWord = Nothing
End Sub

另一个很好的代码是缓冲显示警报。这将阻止您要保存类型对话框出现。



objWord.DisplayAlerts = 0


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:

  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)

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:

  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.

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

  1. The document opens.
  2. 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

这篇关于关闭/释放VBA中的Word对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆