将 .xll 作为 AddIn 添加到 Excel

Mic*_*ael 5 python excel powershell ms-office office-addins

我得到了一个 .xll 文件,可以通过执行以下操作轻松添加到 Excel: 选项 > 插件 > 浏览 > 双击 .xll 文件 它会被导入并激活(每次关闭并打开 Excel 时,它都会保留在我的 excel 插件中)。

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

电源外壳

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

这将创建一个 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")
Run Code Online (Sandbox Code Playgroud)

但这的行为类似于 Powershell 脚本。

那么..如何将我的 .xll 文件添加到 Excel 中以永久保留在那里?有什么建议么?

提前致谢!

Mic*_*ael 1

感谢查尔斯·威廉姆斯给我的链接,我能够做一些不同的事情。

您可以轻松创建一个注册表项,让 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."
    }
}
Run Code Online (Sandbox Code Playgroud)

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

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