java调用cmd关闭excel_在Excel中关闭对dll的访问

你可以使用的一种方法是运行一个单独的excel实例并像这样自动打开/关闭excel ...(这是我在浏览器中输入的伪代码)

Sub TestMyDll()

Dim xl as New Excel.Application

xl.Workbooks.Open "file"

xl.Run "MyFunctionCall"

xl.Workbooks(1).Close False

xl.Quit

Set xl = Nothing

End Sub

第二种方法是动态加载和卸载dll . 这可能是我会使用的方法 . 作为测试,我将Winhttp.dll复制到另一个目录并将其命名为my.dll . 不要将dll放在与包含代码的工作簿相同的目录中,否则excel可能会加载dll .

Option Explicit

Private Declare Function FreeLibrary Lib "kernel32" (ByVal hLibModule As Long) As Long

Private Declare Function LoadLibrary Lib "kernel32" Alias "LoadLibraryA" (ByVal lpLibFileName As String) As Long

'my.dll is a copy of Winhttp.dll in a directory other than where this workbook is saved.

'Calling this will result in an error unless you call LoadLibrary first

Private Declare Function WinHttpCheckPlatform Lib "my.dll" () As Long

Private Sub Foo()

Dim lb As Long

lb = LoadLibrary("C:\Users\David\Downloads\my.dll")

MsgBox WinHttpCheckPlatform

'I found I had to do repeated calls to FreeLibrary to force the reference count

'to zero so the dll would be unloaded.

Do Until FreeLibrary(lb) = 0

Loop

End Sub

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值