我不確定我在這裏做錯了什麼。Excel-VBA Misssing Reference在檢查.isbroken並刪除後不會刪除
我使用的是Outlook 2016和Word中2016年
對於共享的項目,我需要與Outlook和Word 2013使用我的代碼,這就要求他們必須到Outlook庫的引用用戶。
當我運行應該檢查並刪除損壞的引用的代碼,然後添加我指定的引用時,它不會刪除缺少的引用,因此我手動刪除缺少的庫,然後運行代碼以添加它們。 這是代碼,在MS社區論壇,這在其他情況下正常工作發現:
Sub AddReference()
Dim strGUID(1 To 7) As String, theRef As Variant, i As Long
strGUID(1) = "{00062FFF-0000-0000-C000-000000000046}" ' Reference for Outlook library (see below reference printer to get more codes)
strGUID(2) = "{00020905-0000-0000-C000-000000000046}" ' Reference for Word library (see below reference printer to get more codes)
strGUID(3) = "{000204EF-0000-0000-C000-000000000046}" ' Reference for VBA library (see below reference printer to get more codes)
strGUID(4) = "{00020813-0000-0000-C000-000000000046}" ' Reference for Excel library (see below reference printer to get more codes)
strGUID(5) = "{2DF8D04C-5BFA-101B-BDE5-00AA0044DE52}" ' Reference for Office library (see below reference printer to get more codes)
strGUID(6) = "{0D452EE1-E08F-101A-852E-02608C4D0BB4}" ' Reference for MS Forms (see below reference printer to get more codes)
strGUID(7) = "{420B2830-E718-11CF-893D-00A0C9054228}" ' Reference for scripting (see below reference printer to get more codes)
On Error Resume Next
'Remove any missing references
For i = ThisWorkbook.VBProject.References.Count To 1 Step -1
Set theRef = ThisWorkbook.VBProject.References.Item(i)
If theRef.isbroken = True Then
ThisWorkbook.VBProject.References.Remove theRef
End If
Next i
For i = 1 To 7
'Clear any errors so that error trapping for GUID additions can be evaluated
Err.Clear
'Add the reference
ThisWorkbook.VBProject.References.AddFromGuid _
GUID:=strGUID(i), Major:=1, Minor:=0
'If an error was encountered, inform the user
Select Case Err.Number
Case Is = 32813
'Reference already in use. No action necessary
Case Is = vbNullString
'Reference added without issue
Case Else
'An unknown error was encountered, so alert the user
MsgBox "A problem was encountered trying to" & vbNewLine _
& "add or remove a reference in this file" & vbNewLine & "Please check the " _
& "references in your VBA project!", vbCritical + vbOKOnly, "Error!"
End Select
Next i
On Error GoTo 0
End Sub
感謝您的輸入。
難道使用後期綁定更容易,所以Outlook版本無關緊要嗎? –
您是否擁有** V **的「信任訪問VBA項目對象模型」?我有這個代碼,它將刪除所有「Missing」引用 –
@ DarrenBartrup-Cook,你說得對,我不知何故更喜歡早期綁定,雖然 –