集成自动添加引用到子(vba)

时间:2016-10-14 14:13:08

标签: vba excel-vba reference word-vba excel

如何将激活引用的代码(如果尚未激活)集成到我已经获得的代码的开头?问题似乎是新添加的引用不被识别为活动,如果我尝试激活并在同一个子中调用它们...任何建议?当我单独运行它们时它们运行得很好。我收到4120运行时错误(错误参数),这也会在未激活引用时发生

Sub test ()
Dim strGUID As String, theRef As Variant, i As Long

 'Update the GUID you need below.
strGUID = "{00020905-0000-0000-C000-000000000046}"


 '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
'Check if the reference isn't already activatet - can cause issues when stepping through but works just fine
    If ThisWorkbook.VBProject.References.Item(i).GUID = strGUID Then
    GoTo ContinueSub
    End If
Next i

 'Add the reference
ThisWorkbook.VBProject.References.AddFromGuid _
GUID:=strGUID, Major:=1, Minor:=0
Run "ContractPrep"

ContinueSub:
Dim wdApp As Object, wdDoc As Object

'Bunch of code here and then the error occurs here:
Set wdApp = CreateObject("Word.Application")
Set wdDoc = wdApp.Documents.Open(Temp)
'show the word document - put outside of loop for speed later
wdApp.Visible = True

    wdDoc.Application.Selection.Wholestory
    openxml = wdDoc.Application.Selection.WordOpenXML   
    wdDoc.Application.Selection.Delete

'Rest of code
End Sub

0 个答案:

没有答案