当另一个文档已经打开时,打开Word文档时出现错误4605

时间:2018-02-27 11:08:33

标签: excel vba excel-vba ms-word

当我想打开特定的word文档时,我在错误处理程序上遇到了这个问题。

当我开始时,程序到目前为止所做的是:第一次启动很好。然后,当我再次运行时,程序继续加载,直到我手动关闭Word。之后,Word为我提供了以只读模式访问文件的选项。

我已经在论坛和MSDN上搜索了几个小时,但找不到解决方案。

它也一直在给我

  

错误代码4605

当我第二次运行代码时。

代码:

Sub OpenWord()

Dim WordApp As Word.Application
Set WordApp = CreateObject("Word.Application")
WordApp.DisplayAlerts = wdAlertsNone

On Error GoTo ErrorHandler:
WordApp.Documents.Open ("C:\Users\mvandalen\Desktop\Test.docx")
WordApp.Visible = True
Exit Sub

''just for testing
VariableCheese = 5 + 5

ErrorHandler:
WordApp.Documents.Close <<< Here it gives error 4605
WordApp.Quit
Resume Next

End Sub

最终编辑: 感谢@Brett,我找到了解决方案。我复制了他的代码并删除了以下行(标记为&gt;&gt;&gt;):

Sub final()

    Set TestDoc = GetObject("C:\Users\mvandalen\Desktop\Test.docx")
>>>>If TestDoc Is Nothing Then
    Set Wd = GetObject(, "Word.Application")
    If Wd Is Nothing Then
        Set Wd = CreateObject("Word.Application")
        If Wd Is Nothing Then
            MsgBox "Failed to start Word!", vbCritical
            Exit Sub
        End If
        >>>>f = True
    **Else** Added line
        **MsgBox "Failed to start Word!", vbCritical** Added line
    End If
    >>>Set TestDoc = Wd.Documents.Open("C:\Users\mvandalen\Desktop\Test.docx")
    >>>If TestDoc Is Nothing Then
        >>>MsgBox "Failed to open help document!", vbCritical
        >>>If f Then
            >>>Wd.Quit
        >>>End If
        >>>Exit Sub
    End If
    Wd.Visible = True
>>>Else
    >>>With WordDoc.Parent
        >>>.Visible = True
        >>>.Activate
    >>>End With
>>>End If

End sub

此代码打开文件一次,然后再关闭它。 但由于某种原因,这一行是必需的Set TestDoc = GetObject("C:\Users\mvandalen\Desktop\Test.docx")。如果不是,Word文档将变为只读。

5 个答案:

答案 0 :(得分:0)

我首先要转到File&gt;选项&gt;常规并查看框中是否有复选标记:Open e-mail attachments and other uneditable files in reading view。如果有,请将其删除。

来源:https://answers.microsoft.com/en-us/msoffice/forum/msoffice_word-mso_windows8-mso_2013_release/run-time-error-4605-in-word-2013-no-information/1ca02c04-5cea-484e-bd23-f4d18183c1b2

那就是说,我的感觉是你试图关闭一个已经关闭(或者没有活动)的文件,或者没有错误。

要解决此问题,请执行以下操作:

If Err <> 0 Then
  ''Insert your error handling code here
  Err.Clear
Resume Next

请参阅:https://support.microsoft.com/en-au/help/813983/you-receive-run-time-error-4248-4605-or-5941-when-you-try-to-change-pr

或者,问题是您没有检查文档是否已经打开。这可能导致连续循环。我建议使用类似于下面示例的代码来检测文档是否已经打开。

Set TestDoc = GetObject("C:\Users\mvandalen\Desktop\Test.docx")
If TestDoc Is Nothing Then
    Set Wd = GetObject(, "Word.Application")
    If Wd Is Nothing Then
        Set Wd = CreateObject("Word.Application")
        If Wd Is Nothing Then
            MsgBox "Failed to start Word!", vbCritical
            Exit Sub
        End If
        f = True
    End If
    Set TestDoc = Wd.Documents.Open("C:\Users\mvandalen\Desktop\Test.docx")
    If TestDoc Is Nothing Then
        MsgBox "Failed to open help document!", vbCritical
        If f Then
            Wd.Quit
        End If
        Exit Sub
    End If
    Wd.Visible = True
Else
    With WordDoc.Parent
        .Visible = True
        .Activate
    End With
End If

如果文档已经打开,此代码将激活该文档。

来源:https://social.msdn.microsoft.com/Forums/en-US/29265e5f-8df9-4cab-8984-1afb9b110d2f/in-excel-use-vba-to-check-if-a-word-document-is-open?forum=isvvba

根据您的新信息,另一个可能的原因是Visual Basic已经建立了对Word的引用,因为一行代码调用了Word对象,方法或属性,而没有使用Word对象变量限定元素。在结束程序之前,Visual Basic不会释放此引用。当代码运行多次时,此错误引用会干扰自动化代码。要解决此问题,请更改代码,以便使用适当的对象变量限定对Word对象,方法或属性的每次调用。

最近解释这是一篇Excel文章:https://support.microsoft.com/en-hk/help/178510/excel-automation-fails-second-time-code-runs

为了帮助你,我需要知道:

  1. 您使用的是哪个版本的Word。

  2. 您使用的是MacOS还是Windows。

  3. 您的宏安全设置是什么?

  4. 如果你杀了所有Word进程,错误仍会显示?

  5. 文件是否已准备就绪或受到其他保护?

  6. 如果您打开文档并在进入“开发工具”选项卡并运行宏时它处于活动窗口中,是否仍会出现错误?

  7. 鉴于我们知道该文档一直受到保护,请尝试通过进入信任中心并确保不会勾选Word 2003/7二进制文档和模板来删除保护。

答案 1 :(得分:0)

在更仔细地查看代码时,我认为问题在于您不会释放Word对象。由于此代码在Excel中运行,因此这些对象将保留在内存中,而不会在宏结束时释放。而且臭名昭着地试图打开一个仍然打开的文档存在问题 - 因为你在内存中有一个对象让它打开。

查看我对您的代码的更改,如下所示 - Set [variable] = Nothing行。

(请注意,您在代码示例中混合了变量名称&#34; TestDoc&#34;和#34; WordDoc&#34; - 我只是复制了它 - 所以代码,因为它代表,无法正确运行。)

Set TestDoc = GetObject("C:\Users\mvandalen\Desktop\Test.docx")
If TestDoc Is Nothing Then
    Set Wd = GetObject(, "Word.Application")
    If Wd Is Nothing Then
        Set Wd = CreateObject("Word.Application")
        If Wd Is Nothing Then
            MsgBox "Failed to start Word!", vbCritical
            Exit Sub
        End If
        f = True
    End If
    Set TestDoc = Wd.Documents.Open("C:\Users\mvandalen\Desktop\Test.docx")
    If WordDoc Is Nothing Then
        MsgBox "Failed to open help document!", vbCritical
        If f Then
            Wd.Quit
            Set Wd = Nothing
        End If
        Exit Sub
    End If
    Wd.Visible = True
Else
    With WordDoc.Parent
        .Visible = True
        .Activate
    End With
End If
Set WordDoc = Nothing
Set Wd = Nothing

答案 2 :(得分:0)

将文档另存为模板(.dotx)并将 public refPhoto: firebase.storage.Reference = firebase.storage().ref('/Photos'); public myPhoto: any; //Camera and upload takePhoto(uid: string) { Camera.getPicture({ quality: 100, destinationType: Camera.DestinationType.DATA_URL, sourceType: Camera.PictureSourceType.CAMERA, encodingType: Camera.EncodingType.PNG, saveToPhotoAlbum: true }).then(imageData => { this.myPhoto = imageData; this.uploadPhoto(uid); }).catch((err) => { console.log(err); console.log('Cant take photo'); }); } selectPhoto(uid: string): void { Camera.getPicture({ sourceType: Camera.PictureSourceType.PHOTOLIBRARY, destinationType: Camera.DestinationType.DATA_URL, quality: 100, encodingType: Camera.EncodingType.PNG, }).then(imageData => { this.myPhoto = imageData; this.uploadPhoto(uid); }).catch((err) => { console.log(err); console.log('Cant select photo'); }); } private uploadPhoto(uid: string): void { this.refPhoto.child(this.fire.auth.currentUser.uid).child('profileImage.png') .putString(this.myPhoto, 'base64', { contentType: 'image/png' }) .then((savedPicture) => { //good practice is to store this reference ID to Database }).catch((err) => { console.log(err); console.log('Cant upload photo'); }); } 更改为.Open()

.Add()

由于您有对Word的引用,因此无需致电Dim WordApp As Word.Application Dim WordDoc As Word.Document Set WordApp = New Word.Application Set WordDoc = WordApp.Documents.Add "C:\Users\mvandalen\Desktop\Test.dotx" WordApp.Visible = True '... WordDoc.Close wdDoNotSaveChanges

删除对Word Library的引用,并将CreateObject("Word.Application")WordApp声明为WordDoc,或使用Object关键字。

通过这种方式,您可以同时打开任意数量的实例。

答案 3 :(得分:0)

尝试以下代码。它:

•如果Word尚未运行,则启动Word。

•如果文档尚未打开,则打开文档。

•save&amp;如果打开文档,则在编辑后关闭文档。

•如果启动了Word,则退出。

当然,如果要保持文档打开,您可以省略文档关闭和应用程序退出代码。根据您是否要阻止对正在保存的文件的编辑,您可能还需要设置ReadOnly:= True。

Sub OpenWord()
Dim WdApp As Word.Application, WdDoc As Word.Document
Dim bQuit As Boolean, bClose As Boolean
Const StrFlNm As String = "C:\Users\mvandalen\Desktop\Test.docx"
If Dir(StrFlNm) = "" Then
  MsgBox "Cannot find the file:" & vbCr & StrFlNm, vbCritical
  Exit Sub
End If
bQuit = False: bClose = True
On Error Resume Next
Set WdApp = GetObject(, "Word.Application")
If WdApp Is Nothing Then
  Set WdApp = CreateObject("Word.Application")
  On Error GoTo 0
  If WdApp Is Nothing Then
    MsgBox "Can't start Word.", vbExclamation
    Exit Sub
  End If
  bQuit = True
End If
On Error GoTo 0
With WdApp
  .Visible = True
  For Each WdDoc In .Documents
    If WdDoc.FullName = StrFlNm Then
      bClose = False: Exit For
    End If
  Next
  If WdDoc Is Nothing Then
    Set WdDoc = .Documents.Open(Filename:=StrFlNm, ReadOnly:=False, AddToRecentFiles:=False, Visible:=True)
  End If
  With WdDoc
    'Do your document edits here
    If bClose = True Then .Close SaveChanges:=True
  End With
  If bQuit = True Then .Quit
End With
End Sub

答案 4 :(得分:0)

你必须仔细处理已经运行Word会话以及无法获得Word会话的可能性

所以你可以使用辅助函数:

Function GetWord(WordApp As Word.Application) As Boolean
    On Error Resume Next
    Set WordApp = GetObject(, "Word.Application") 'try getting an already running Word instance
    If WordApp Is Nothing Then Set WordApp = CreateObject("Word.Application") ' if unsuccesful then try creating a new Word instance
    GetWord = Not WordApp Is Nothing ' notify the result
End Function

因此您的主要代码将重构如下

Option Explicit

Sub OpenWord()
    Dim WordApp As Word.Application

    If Not GetWord(WordApp) Then 'if unsuccesful in getting/creating a Word session then exit sub
        MsgBox "Couldn't get an existing instance or create a new instance of Word", vbCritical
        Exit Sub
    End If

    With WordApp 'reference the Word session you just got/created
        .DisplayAlerts = wdAlertsNone
        .Visible = True

        On Error GoTo WordErrorHandler:
        .Documents.Open ("C:\Users\mvandalen\Desktop\Test.docx") 

        ' rest of your code exploiting the opened document
    End With

    On Error GoTo 0 'disable Word Error processing

    ' here goes the rest of your code to work without Word object/data



    Exit Sub ' exit not to process statements following 'WordErrorHandler'


WordErrorHandler:
    With WordApp
        If .Documents.Count > 0 Then .Documents.Close '<<< Here it gives error 4605
        .Quit
    End With
    Set WordApp = Nothing
    Resume Next        
End Sub