Powershell无法找到类型[Microsoft.Office.Interop.Word.WdSaveFormat]

时间:2018-11-06 10:28:57

标签: powershell

我正在使用此脚本将DOC转换为HTML

param([string]$docpath,[string]$htmlpath = $docpath)

$srcfiles = Get-ChildItem $docPath -filter "*.doc"
$saveFormat = [Enum]::Parse([Microsoft.Office.Interop.Word.WdSaveFormat], "wdFormatFilteredHTML");
$word = new-object -comobject word.application
$word.Visible = $False

function saveas-filteredhtml
{
    $opendoc = $word.documents.open($doc.FullName);
    $opendoc.saveas([ref]"$htmlpath\$doc.fullname.html", [ref]$saveFormat);
    $opendoc.close();
}

ForEach ($doc in $srcfiles)
{
    Write-Host "Processing :" $doc.FullName
    saveas-filteredhtml
    $doc = $null
}

$word.quit();

不幸的是,当我在ISE控制台中首次运行它时,出现此错误

Unable to find type [Microsoft.Office.Interop.Word.WdSaveFormat].
In F:\PS\NEW\main.ps1:108 car:29
+ ...  = [Enum]::Parse([Microsoft.Office.Interop.Word.WdSaveFormat], "wdFor ...
+                      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidOperation: (Microsoft.Offic...rd.WdSaveFormat:TypeName) [], RuntimeException
+ FullyQualifiedErrorId : TypeNotFound

当我第二次从同一控制台再次运行它时,

我该如何解决问题?谢谢

1 个答案:

答案 0 :(得分:0)

他们在这个论坛上解决了问题:https://gallery.technet.microsoft.com/office/6f7eee4b-1f42-499e-ae59-1aceb26100de/view/Discussions

您将以下行添加到代码的开头:

$wdTypes = Add-Type -AssemblyName 'Microsoft.Office.Interop.Word' -Passthru
$wdSaveFormat = $wdTypes | Where {$_.Name -eq "wdSaveFormat"}