在VBA宏中以编程方式设置DLL搜索路径 [英] Programmatically set DLL search path in VBA macro

查看:207
本文介绍了在VBA宏中以编程方式设置DLL搜索路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题

  • 我有一个单词模板,该模板使用VBA的Declare语句链接到dll,该dll的路径可以在VBA宏中确定
  • 我想将此部署到用户%APPDATA%\ Microsoft \ Word \ STARTUP目录
  • 我不想永久更改用户的PATH环境变量(暂时可以,但是似乎不起作用,因为直到应用程序重新启动,他们才会刷新)
  • I have a word template which uses VBA's Declare statement to link to a dll, whose path can be determined within the VBA macro
  • I want to delploy this to the users %APPDATA%\Microsoft\Word\STARTUP directory
  • I DON'T want to permanently change the user's PATH environment variable (temporarily would be OK, but this doesn't seem to work as they don't get refreshed until application restart)

尝试的解决方案

我尝试使用ThisDocument.VBProject.CodeModule.AddFromString(code)动态添加带有Declare语句的代码,该代码在从普通目录加载模板时有效,但是当模板在Word \ STARTUP中时,会出现以下错误:

I tried dynamically adding the code with the Declare statements using ThisDocument.VBProject.CodeModule.AddFromString(code) which works when loading the template from a normal directory, but when the template is within Word\STARTUP, it gives the following error:

运行时错误"50289":

Run-time error '50289':

由于 项目受到保护.

Can't perform operation since the project is protected.

当模板位于Word \ STARTUP中时,将注册表项"HKEY ___ LOCAL_MACHINE \ Software \ Microsoft \ Office \ 11.0 \ Word \ Security \ AccessVBOM"设置为1并不能解决此问题.

And setting the registry key "HKEY___LOCAL_MACHINE\Software\Microsoft\Office\11.0\Word\Security\AccessVBOM" to 1 doesn't fix this when the template is in Word\STARTUP

我真的很努力寻找解决方案.如果有人知道这样做的方法,那就太好了.

I'm really struggling to find a solution. If anyone knows a way to do this, that would be great.

推荐答案

坦率地说,我不知道使用所有这些VBA代码注入,LoadLibrary()调用的程序集生成以及我所见过的技术有什么问题用于完成此简单任务.在我的项目中,我使用简单的代码从与工作簿相同的位置加载dll,如下所示:

Frankly, I don't know what's the problem with using all those VBA code injection, assembly generation for LoadLibrary() calls, etc techniques that I've seen used for this simple task. In my project I use simple code to load dll from the same location as the workbook, like this:

Declare Function MyFunc Lib "MyDll.dll" (....) As ...

Sub Test()
  ....
  ChDir ActiveWorkbook.Path
  ... = MyFunc(....)
End Sub

至少在Excel 2003中,从当前路径加载dll没有问题,请将ChDir设置为DLL具有的任何路径.您可能还需要更改与当前路径分开的当前驱动器.无论您当前的路径在哪里,您都只需要在第一次函数调用之前执行一次,在此之后DLL保持连接状态,因此您可以在workbook_open中执行一次,而以后不再理会该路径.为此,我在DLL中提供了一个空的伪函数.我认为MS Word在这方面没有什么不同.

Excel 2003 at least, has no problem loading the dll from the current path, Set ChDir to whatever path your DLL has. You might also need to change your current drive which is separate from current path. You have to do it only once, before the first function call, after it the DLL stays attached no matter where your current path is, so you may do it once in workbook_open and not bother about the path later. I provide an empty dummy function in the DLL just for this pupose. I don't think MS Word is any different on this.

Private Declare Sub Dummy Lib "MyDLL.dll" ()

Private Sub Workbook_Open()
    ChDrive Left$(Me.Path, 1)
    ChDir Me.Path
    Dummy
End Sub

这篇关于在VBA宏中以编程方式设置DLL搜索路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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