[英]Error 4605 on opening Word document when another document is already open
因此,當我想打開特定的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,我找到了一個解決方案。 我復制了他的代碼並刪除了以下幾行(標記為>>>):
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文檔將變為只讀。
首先,轉到“文件”>“選項”>“常規”,然后查看框中是否有復選標記: Open e-mail attachments and other uneditable files in reading view
中Open e-mail attachments and other uneditable files in reading view
。 如果有,請將其刪除。
來源: https : //answers.microsoft.com/zh-cn/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/zh-cn/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/zh-cn/help/178510/excel-automation-fails-second-time-code-runs
為了幫助您更多,我需要知道:
您正在使用哪個版本的Word。
您使用的是MacOS還是Windows。
您的宏安全設置是什么?
如果您殺死了所有Word處理程序,該錯誤仍然顯示嗎?
文件是僅准備就緒還是受到其他保護?
如果打開文檔並轉到“開發人員”選項卡並運行宏,則該文檔位於活動窗口中,是否仍會發生錯誤?
鑒於我們知道文檔一直受到保護,請嘗試進入信任中心並取消選中Word 2003/7 Binary Documents and Templates,以取消保護。
在更仔細地查看代碼時,我認為問題在於您沒有釋放Word對象。 由於此代碼是從Excel內部運行的,因此這些對象被保留在內存中,而在宏結束時不會被釋放。 眾所周知,Word在嘗試打開仍處於打開狀態的文檔時會遇到問題-因為在內存中有一個對象使其保持打開狀態。
在下面查看我對代碼的更改Set [variable] = Nothing
lines。
(請注意,您在代碼示例中混合了變量名稱“ TestDoc”和“ WordDoc”(我只是復制了它,因此,就目前而言,該代碼無法正確運行。)
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
將文檔另存為模板(.dotx),然后將.Open()
更改為.Add()
。
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的引用,因此無需調用CreateObject("Word.Application")
。
刪除對Word Library的引用,並將WordApp
和WordDoc
聲明為Object
,或使用New
關鍵字。
這樣,您可以同時打開任意多個實例。
請嘗試以下代碼。 它:
•啟動Word(如果尚未運行)。
•打開文檔(如果尚未打開)。
•如果打開文檔,則在編輯后保存並關閉文檔。
•如果啟動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
您必須仔細處理已經運行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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.