将 .xll 文件添加为 Excel 的 AddIn。

5

我有一个.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时,插件并不存在。

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中并使其永久留在那里?有什么建议吗?

提前感谢!


嗨@Theo,谢谢你的回复。 我在*.xla指南中也看到过这个,但可惜对我来说这并不起作用。 我收到了一个错误,说“AddIns对象的Add属性无法分配”。 - Michael K
你是如何使用.Add()方法的?你只需要提供名称,而不是整个文件路径。 - Theo
我是这样使用它的:$excel.AddIns.Add("LS-ZmqRtd-AddIn64.xll").Installed = $true - Michael K
你可以尝试使用 $excel.AddIns.Add("LS-ZmqRtd-AddIn64.xll", $true).Installed = $true 或者 AddIns2 对象。 - Theo
很遗憾,你给我的代码行不起作用。它仍然显示属性无法赋值。AddIns2?那该如何执行?能给我一个例子吗? :) - Michael K
显示剩余3条评论
2个回答

1
感谢Charles Williams提供的链接,我能够以稍微不同的方式完成它。
您可以轻松创建一个注册表键,让Excel知道它应该运行.xll文件。
# initializing new variables
$req_path = Get-Item -Path Registry::HKEY_CURRENT_USER\Software\Microsoft\Office\16.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\16.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\16.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\16.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\16.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 文件是否已经在注册表键中命名。

  • 如果是,并且它拥有我们最新提供的版本 -> 不执行任何操作
  • 如果是,但是版本过旧 -> 更新注册表键的值
  • 如果不是 -> 创建注册表键并将值设置为我们最新提供的版本

1
我会做以下两个方案之一:
  • 在工作簿打开事件中添加一些VBA代码以注册XLL,这将适用于特定的工作簿。
  • 编写XLL的注册表键(注意,在编写注册表键时必须关闭Excel)。以下是一个LUA脚本的链接,可用于演示如何编写注册表键,您可以按照需要使用其他语言进行重写。 https://jkp-ads.com/articles/AddinsAndSetupFactory.asp

谢谢Charles,非常感谢你提供的链接!那让我有足够的信息来自己创建一个解决方法。 - Michael K

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接