检查Word文档是否已经打开+错误处理 [英] Check if Word Document is already opened + Error Handling

查看:38
本文介绍了检查Word文档是否已经打开+错误处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,谢谢您的提前答复.

Hello and thank you for your answers in advance.

我正在使用excel-vba打开Word文档,并将其保存为新名称.这实际上工作正常.

I am opening a word document using excel-vba and save it under a new name. This is actually working fine.

但是如果已经打开了具有新名称的word文档,则会出现问题!

But problems occur if the word document with the new name is already opened!

比方说有一个运行脚本的按钮,用户第二次运行它,并且创建的文件仍处于打开状态.用户可能会在excel中更改某些内容,现在想检查新的word文档看起来像后记后的样子.他将再次单击该按钮.它将打开模板(进行所有更改)并尝试保存它,但是无法打开,因为它已经打开,并且可能使用旧名称(模板)而不是新文件来保存此文档.因此它将覆盖并销毁模板文件(在测试过程中多次获取)!

Let's say there is a button to run the script and the user runs it the second time, and has the created file still opened. The user might change something in excel and now wants to check how the new word document would look like afterwords. He will click the button again. It will open the template (do all changes) and try to save it, but can't because it is already opened and it might save this document with the old name (template) instead of a new file. Therefor it will overwrite and destroy the template file (got this several times during testing)!

因此,我需要一些适当的代码和更好的错误处理.我的第一个想法是检查带有文件名的文档是否已经存在.但是它并没有尽力而为:

Therefore I am in need of some proper code and a better Error-Handling. My first thought is to check if the document with the filename already exists. But it does not quite do its job:

Sub CreateWordDocument()
    Dim TemplName, CurrentLocation, DocumentName, Document As String
    Dim WordDoc, WordApp, OutApp As Object

    With table1
        TemplName = table1.Range("A1").Value 'Get selected template name
        CurrentLocation = Application.ActiveWorkbook.Path 'working folder
        Template = CurrentLocation + "\" + TemplName
        DocumentName = .Range("A2").Value
        Document = CurrentLocation + "\" + DocumentName + ".docx"

    'Open Word Template
    On Error Resume Next 'if Word is already running
    Set WordApp = GetObject("Word.Application")
    If Err.Number <> 0 Then
        'Launch a new instance of Word
        Err.Clear
        Set WordApp = CreateObject("Word.Application")
        WordApp.Visible = True 'Make the application visible to the user
    End If

    'if document is already opened in word than close it
    'if its not possible to close it - end application to prevent any damage to the template
    On Error GoTo notOpen
        Set WordDoc = WordApp.Documents(DocumentName + ".docx")
    On Error GoTo closeError
        WordDoc.Close
    notOpen:
        'Open the template
        Set WordDoc = WordApp.Documents.Open(Filename:=Template, ReadOnly:=False) 'Open Template
    'save with new name
    WordDoc.SaveAs Document
    closeError: 
    'open a message box and tell user to close and run again.

在当前阶段,它只是从"Set WordDoc = WordApp...."跳到notOpened.有什么建议可以解决这个问题吗?

At the current stage it just jumpes from "Set WordDoc = WordApp. ..." to notOpened. Any suggestions how to solve this issue?

推荐答案

添加此功能:

Public Function FileIsOpen(FullFilePath As String) As Boolean

    Dim ff As Long

    On Error Resume Next

    ff = FreeFile()
    Open FullFilePath For Input Lock Read As #ff
    Close ff
    FileIsOpen = (Err.Number <> 0)

    On Error GoTo 0

End Function 

然后在您的代码中使用:

Then use in your code:

If Not FileIsOpen(DocumentName & ".docx") Then
    Set WordDoc = WordApp.Documents.Open(Filename:=Template, ReadOnly:=False)
Else
    'Do something else because the file is already open.
End If

文档名称必须是文档的完整路径.

The document name must be the full path to the document.

其他方面:

只有 Document 是一个字符串,而 OutApp 是一个对象.所有其他变量都是 Variants .

Only Document is a string, and OutApp is an object. All other variables are Variants.

Dim TemplName, CurrentLocation, DocumentName, Document As String  
Dim WordDoc, WordApp, OutApp As Object

应为:

Dim TemplName As String, CurrentLocation As String, DocumentName As String, Document As String
Dim WordDoc As Object, WordApp As Object, OutApp As Object 


VBA通​​常使用 + 进行添加,并使用& 进行串联.


VBA generally uses + for addition, and & for concatenation.

DocumentName + ".docx"  

最好写成

DocumentName & ".docx"  


文档单词.由于代码在 Excel 中,因此在这里应该不会引起太多问题,但要记住一点.


Document is a reserved word in Word. It shouldn't cause too much problem here as the code is in Excel, but something to keep in mind.

这篇关于检查Word文档是否已经打开+错误处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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