尝试使用Powershell脚本安装PowerPoint的加载项



我为powerpoint制作了一个插件,并找到了一个要安装powershell的脚本。这是脚本:

[CmdletBinding()]
Param(
[Parameter(Position=0,Mandatory=$true)]
[String]$AddinPath,
[Parameter(Mandatory=$false)]
[Switch]$Reinstall,
[Parameter(Mandatory=$false)]
[Switch]$NoCopy
)
# Ensure that any errors we receive are considered fatal
$ErrorActionPreference = 'Stop'
# The path to the default folder to copy Excel add-ins
$ExcelAddinsPath = Join-Path $env:APPDATA 'MicrosoftAddIns'
if (Test-Path -Path $AddinPath -PathType Leaf) {
$Addin = Get-ChildItem -Path $AddinPath
if ($Addin.Extension -NotIn ('.ppam')) {
Write-Error 'The file does not appear to be a Power Point add-in.'
}
} else {
Write-Error 'The add-in file path does not appear to be valid.'
}
try {
Add-type -AssemblyName office
$Application = New-Object -ComObject powerpoint.application
$Application.visible = [Microsoft.Office.Core.MsoTriState]::msoTrue
$slideType = "microsoft.office.interop.powerpoint.ppSlideLayout" -as [type]
$blanklayout = $slideType::ppLayoutTitleOnly
$presentation = $application.Presentations.add()
$slide = $Presentation.Slides.Add($presentation.Slides.Count + 1, 15)
} catch {
Write-Error 'Microsoft Excel does not appear to be installed.'
}
try {
$PowerPointAddins = $Application.Addins
$AddinInstalled = $PowerPointAddins | ? { $_.Name -eq $Addin.Name }

if (!$AddinInstalled -or $Reinstall) {
if (!(Test-Path -Path $ExcelAddinsPath -PathType Container)) {
New-Item -Path $ExcelAddinsPath -ItemType Directory  
}
if (!$NoCopy) {
Copy-Item -Path $Addin.FullName -Destination $ExcelAddinsPath -Force
$Addin = Get-ChildItem -Path (Join-Path $ExcelAddinsPath $Addin.Name)
$Addin.IsReadOnly = $true
}


$NewAddin = $presentation.Application.AddIns.Add('C:UsersAdminAppDataRoamingMicrosoftAddInsAddinthatIcreated.ppam', $true)
$NewAddin.Installed = $true
Write-Host ('Add-in "' + $Addin.BaseName + '" successfully installed!')
} else {
Write-Host ('Add-in "' + $Addin.BaseName + '" already installed!')
}
} finally {
$Application.Quit()
}

此脚本将.ppam文件放入AddiFile中,但出现错误并显示以下消息:

找不到"的过载;添加";以及自变量计数:";2〃

当脚本执行此指令时,会出现此消息(第60行(:

$NewAddin = $presentation.Application.AddIns.Add('C:UsersAdminAppDataRoamingMicrosoftAddInsAddinthatIcreated.ppam', $true)

问题的根源是什么?我该如何解决?谢谢你帮我!

该问题是由Excel和PowerPoint方法混淆引起的。尽管名称相同,但函数原型却不同。人们可以通过谷歌搜索excel addins.addpowerpoint addins.add来找到可接受的版本,这将在微软的文档页面上提供点击量。

对于Powerpoint,AddIns.Add()方法采用单个参数,即文件名。

对于Excel,AddIns.Add()方法采用两个参数,即文件名和复制文件。

作为解决方案,将调用更改为只有一个参数,如

$NewAddin = $presentation.Application.AddIns.Add('C:UsersAdminAppDataRoamingMicrosoftAddInsAddinthatIcreated.ppam')

最新更新