我得到了一個 .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()
這將創建一個 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 中以永久保留?有什么建議?
提前致謝!
uj5u.com熱心網友回復:
我要么:
- 向作業簿打開事件添加一些 VBA 以注冊 XLL,這將適用于特定的作業簿
- 為 XLL 撰寫注冊表項(注意,在撰寫注冊表項時必須關閉 Excel)這是一個指向 LUA 腳本的鏈接:這將向您展示如何以及如何以您想要使用的語言重寫。 https://jkp-ads.com/articles/AddinsAndSetupFactory.asp
uj5u.com熱心網友回復:
感謝查爾斯威廉姆斯給我的鏈接,我能夠做到這一點有點不同。
您可以輕松創建一個注冊表項,讓 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-File 是否已在注冊表項中命名。
- 如果是,并且它有我們提供的最新版本 -> 什么都不做
- 如果是但版本舊 -> 更新注冊表項的值
- 如果沒有 -> 創建注冊表項并將值設定為我們
提供的最新版本
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/316434.html
標籤:Python 擅长 电源外壳 微软Office 办公室插件
上一篇:在PowerShell中將大型blob從SQLServer提取到檔案需要很長時間
下一篇:檢查沒有RSAT的廣告組成員資格
