如何以编程方式添加引用 [英] How to add a reference programmatically

查看:287
本文介绍了如何以编程方式添加引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经写了一个程序,运行和消息Skype与信息,如果完成。我需要为 Skype4COM.dll 添加参考,以便通过Skype发送消息。我们在网络和共享文件服务器上有十几台电脑(除其他外)。所有其他计算机都需要能够运行该程序。我希望能够避免手工设定参考。我计划把参考放在一个共享的位置,并在程序运行时以编程方式添加。



我似乎无法弄清楚如何以编程方式添加引用使用VBA到Excel 2007。我知道如何手动执行:打开 VBE - >工具 - >参考文献 - >浏览--_>文件位置和名称 。但这对我的目的来说并不是很有用。我知道有一些方法可以在。



主题:通过代码添加VBA参考库



链接 http://www.vbaexpress.com/kb/getarticle.php?kb_id=267






方式2(直接引用dll)



代码添加了对 Microsoft VBScript正则表达式5.5的引用

  Option Explicit 

Sub AddReference()
Dim VBAEditor As VBIDE.VBE
Dim vbProj As VBIDE.VBProject
Dim chkRef As VBIDE.Reference
Dim BoolExists As Boolean

设置VBAEditor = Application.VBE
设置vbProj = ActiveWorkbook.VBProject

'~~>检查是否已经添加了Microsoft VBScript Regular Expressions 5.5
对于每个chkRef在vbProj.References
如果chkRef.Name =VBScript_RegExp_55然后
BoolExists = True
GoTo CleanUp
结束如果
下一个

vbProj.References.AddFromFileC:\WINDOWS\system32\vbscript.dll\3

清理:
如果BoolExists = True然后
MsgBox参考已存在
Else
MsgBox参考成功添加
结束如果

设置vbProj = Nothing
设置VBAEditor =没有
结束Sub

注意:我没有添加错误处理。建议在您的实际代码中使用它:)



编辑殴打 mischab1 :)


I've written a program that runs and messages Skype with information when if finishes. I need to add a reference for Skype4COM.dll in order to send a message through Skype. We have a dozen or so computers on a network and a shared file server (among other things). All of the other computers need to be able to run this program. I was hoping to avoid setting up the reference by hand. I had planned on putting the reference in a shared location, and adding it programmatically when the program ran.

I can't seem to figure out how to add a reference programmatically to Excel 2007 using VBA. I know how to do it manually: Open VBE --> Tools --> References --> browse --_> File Location and Name. But that's not very useful for my purposes. I know there are ways to do it in Access Vb.net and code similar to this kept popping up, but I'm not sure I understand it, or if it's relevant:

ThisWorkbook.VBProject.References.AddFromGuid _
    GUID:="{0002E157-0000-0000-C000-000000000046}", _
    Major:=5, Minor:=3

So far, in the solutions presented, in order to add the reference programmatically I will need to add a reference by hand and change the Trust Center - which is more than just adding the reference. Though I guess if I follow through with the solutions proposed I will be able to add future references programmatically. Which probably makes it worth the effort.

Any further thoughts would be great.

解决方案

Ommit

There are two ways to add references via VBA to your projects

1) Using GUID

2) Directly referencing the dll.

Let me cover both.

But first these are 3 things you need to take care of

a) Macros should be enabled

b) In Security settings, ensure that "Trust Access To Visual Basic Project" is checked

c) You have manually set a reference to `Microsoft Visual Basic for Applications Extensibility" object

Way 1 (Using GUID)

I usually avoid this way as I have to search for the GUID in the registry... which I hate LOL. More on GUID here.

Topic: Add a VBA Reference Library via code

Link: http://www.vbaexpress.com/kb/getarticle.php?kb_id=267


Way 2 (Directly referencing the dll)

This code adds a reference to Microsoft VBScript Regular Expressions 5.5

Option Explicit

Sub AddReference()
    Dim VBAEditor As VBIDE.VBE
    Dim vbProj As VBIDE.VBProject
    Dim chkRef As VBIDE.Reference
    Dim BoolExists As Boolean

    Set VBAEditor = Application.VBE
    Set vbProj = ActiveWorkbook.VBProject

    '~~> Check if "Microsoft VBScript Regular Expressions 5.5" is already added
    For Each chkRef In vbProj.References
        If chkRef.Name = "VBScript_RegExp_55" Then
            BoolExists = True
            GoTo CleanUp
        End If
    Next

    vbProj.References.AddFromFile "C:\WINDOWS\system32\vbscript.dll\3"

CleanUp:
    If BoolExists = True Then
        MsgBox "Reference already exists"
    Else
        MsgBox "Reference Added Successfully"
    End If

    Set vbProj = Nothing
    Set VBAEditor = Nothing
End Sub

Note: I have not added Error Handling. It is recommended that in your actual code, do use it :)

EDIT Beaten by mischab1 :)

这篇关于如何以编程方式添加引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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