将 .xll 作为插件添加到 Excel

Add .xll as AddIn to Excel

我得到了一个 .xll 文件,我可以通过这样做轻松地将其添加到 excel: 选项 > 加载项 > 浏览 > 双击 .xll 文件 它被导入 + 激活(每次我关闭和打开 Excel 时它都保留在我的 excel 插件中)。

这是我尝试用脚本替换的手动方式。

PowerShell

$excel=New-Object -ComObject excel.application
$excel.RegisterXLL("C:\temp\v-0.0.1-20210906\LS-ZmqRtd-AddIn64.xll")
$excel.Visible = "$True"
#$excel.Quit()

这将创建 Excel 的实例,注册 XLL(我在控制台中得到一个“true”)并显示创建的实例。但是当我转到 AddIns 时,AddIn 不存在。

Python

xl = win32com.client.gencache.EnsureDispatch("Excel.Application")
xl.Visible = True
xl.RegisterXLL(
    "C:/Users/michael.k/Desktop/v-0.0.1-20210906/LS-ZmqRtd-AddIn64.xll"
)
wb = xl.Workbooks.Open("C:/Users/michael.k/Desktop/v-0.0.1-20210906/Test.xlsx")

但这就像 Powershell 脚本一样。

那么..我怎样才能将我的 .xll 文件添加到 Excel 并永久?有什么建议吗?

提前致谢!

我会:

  • 向工作簿打开事件添加一些 VBA 以注册 XLL,这将适用于特定工作簿
  • 为 XLL 写入注册表项(注意 Excel 在写入注册表项时必须关闭) 这是一个 link 到 LUA 的脚本:这将向您展示如何以及您可以用您想要使用的语言重写。 https://jkp-ads.com/articles/AddinsAndSetupFactory.asp

多亏了 link Charles Williams 给了我,我才能够做到这一点。

您可以轻松地创建一个注册表项,让 excel 知道它应该 运行 .xll 文件。

# initializing new variables
$req_path = Get-Item -Path Registry::HKEY_CURRENT_USER\Software\Microsoft\Office.0\Excel\Options |
  Select-Object -ExpandProperty Property
$newest_version = (Get-ChildItem -Path I:\Software\LS-ZmqRtd\Test\ -Directory | sort lastwritetime | Select -Last 1).Name
$full_path_new_version = '/R "I:\Software\LS-ZmqRtd\Test\'+$newest_version+'\LS-ZmqRtd-AddIn64.xll"'
$only_opens = @()
[bool]$ls_zmqrtd_found = $false
[bool]$ls_zmqrtd_updated = $false
$count_opens = 0

# welcome message
echo ">> checking if the LS-ZmqRtd addin is installed and has the newest version.."
Start-Sleep -s 5

# check if there are regkeys that contain 'OPEN' in their name (if yes, add them to the $only_opens array)
foreach ($entry in $req_path)
{
    if ($entry -like "OPEN*")
    {
        $only_opens += $entry
    }
}

if (!$only_opens) # check if array is empty (if yes, add the new regkey for LS-ZmqRtd)
{
    echo ">> the LS-ZmqRtd addin couldn't be found.. adding it to excel now."
    Start-Sleep -s 2
    New-ItemProperty -Path HKCU:\Software\Microsoft\Office.0\Excel\Options -Name OPEN -PropertyType String -Value $full_path_new_version
    echo ">> addin was added to excel successfully - this requires Excel to be fully closed and re-opened."
}
else # if no, check if one of the regkeys have the LS-ZmqRtd path value (if found, set $ls_zmqrtd_found to true - else remain false)
{  
    foreach ($open in $only_opens)
    {
        $value = (Get-ItemProperty -Path "HKCU:\Software\Microsoft\Office.0\Excel\Options" -Name $open).$open
        if ($value -eq $full_path_new_version)
        {
            $ls_zmqrtd_found = $true
        }
        else
        {
            echo ">> found an old version of LS-ZmqRtd.. replacing it with the new one now."
            Start-Sleep -s 2
            Set-ItemProperty -Path HKCU:\Software\Microsoft\Office.0\Excel\Options -Name $open -Value $full_path_new_version
            $ls_zmqrtd_updated = $true
        }
        $count_opens += 1
    }


    if ($ls_zmqrtd_found -eq $true) # if $ls_zmqrtd_found is true, there is nothing to do
    {
        echo ">> found that the newest version of LS-ZmqRtd is already installed - nothing to do here."
    }
    elseif ($ls_zmqrtd_updated -eq $true)
    {
        echo ">> updated LS-ZmqRtd to the newest version - an update requires Excel to be fully closed and re-opened."
    }
    else # if $ls_zmqrtd_found is false, increment the last OPEN's number by 1 and add the new reqkey for LS-ZmqRtd
    {
        $new_reg_key = "OPEN" + ($count_opens+1)
        echo ">> the LS-ZmqRtd addin couldn't be found.. adding it to excel now."
        Start-Sleep -s 2
        New-ItemProperty -Path HKCU:\Software\Microsoft\Office.0\Excel\Options -Name $new_reg_key -PropertyType String -Value $full_path_new_version
        echo ">> addin was added to excel successfully - this requires Excel to be fully closed and re-opened."
    }
}

此脚本检查 .xll 文件是否已在注册表项中命名。

  • 如果是并且它有我们提供的最新版本 -> 什么都不做
  • 如果是但版本旧 -> 更新注册表项的值
  • 如果没有 -> 创建注册表项并将值设置为我们最新的
    提供的版本