1

前几天我发布了这个脚本,试图找到一种在“另存为”时更改文件扩展名的好方法。我已经解决了这个问题,但是从今天早上开始,脚本将无法正常运行。这是我收到的错误消息:

Processing : C:\users\xxx\Desktop\ht\Automatic_Post-Call_Survey.htm
Exception calling "SaveAs" with "16" argument(s): "This is not a valid file name.
Try one or more of the following:
* Check the path to make sure it was typed correctly.
* Select a file from the list of files and folders."
At C:\users\xxx\Desktop\hd.ps1:11 char:20
+     $opendoc.saveas <<<< ([ref]"$docpath\$doc.FullName.doc", [ref]$saveFormat);
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException

如果“16”是错误代码,则表示无法删除目录......但它看起来好像我根本没有要求 - 除非某处有一些默认参数。我很困惑。有人有其他想法可以尝试吗?

$docpath = "c:\users\xxx\desktop\do"
$htmPath = "c:\users\xxx\desktop\ht"

$srcfiles = Get-ChildItem $htmPath -filter "*.htm*"
$saveFormat = [Enum]::Parse([Microsoft.Office.Interop.Word.WdSaveFormat], "wdFormatDocument"); 
$word = new-object -comobject word.application 
$word.Visible = $False        
$filename = ($_.fullname).substring(0,($_.FullName).lastindexOf("."))

function saveas-document   {         
        $opendoc = $word.documents.open($doc.FullName);         
    $opendoc.saveas([ref]"$docpath\$filename", [ref]$saveFormat);         
    $opendoc.close();
}       
ForEach ($doc in $srcfiles)     {
    Write-Host "Processing :" $doc.FullName         
    saveas-document        
    $doc = $null   
}   

$word.quit(); 
4

1 回答 1

3

这应该做你需要的,但不是最好的设计:)

$docpath = "c:\users\xxx\desktop\do"
$htmPath = "c:\users\xxx\desktop\ht"


$srcfiles = Get-ChildItem $htmPath -filter "*.htm*"

$saveFormat = [Enum]::Parse([Microsoft.Office.Interop.Word.WdSaveFormat], "wdFormatDocument"); 

$global:word = new-object -comobject word.application 

$word.Visible = $False        

#$filename = ($_.fullname).substring(0,($_.FullName).lastindexOf("."))

function saveas-document ($docs)  {         
    $opendoc = $word.documents.open($docs);    
    $savepath = $docs -replace [regex]::escape($htmPath),"$docpath"
    $savepath = $savepath -replace '\.html*', '.doc'
    $opendoc.saveas([ref]"$savepath", [ref]$saveFormat);         
    $opendoc.close();
}       
ForEach ($doc in $srcfiles)     {
    Write-Host "Processing :" $doc.FullName         
    saveas-document  -doc  $doc.FullName     
    $doc = $null   
}   

$word.quit();
于 2012-12-13T14:42:18.907 回答