我使用以下代码将新单词模板应用于现有word文档,但我收到此错误:
错误:异常类型:System.Management.Automation.MethodInvocationException 异常消息:使用“1”参数调用“Paste”的异常:“对象引用未设置为对象的实例。” 异常详细信息:System.NullReferenceException:未将对象引用设置为对象的实例。 在System.Object.GetType() 在System.Management.Automation.Adapter.EffectiveArgumentType(Object arg) at System.Linq.Enumerable.WhereSelectArrayIterator
2.MoveNext() at System.Linq.Buffer
1..ctor(IEnumerable1 source) at System.Linq.Enumerable.ToArray[TSource](IEnumerable
1 source) 在System.Management.Automation.Adapter.FindBestMethod(MethodInformation []方法,PSMethodInvocationConstraints invocationConstraints,Object [] arguments,String& errorId,String& errorMsg,Boolean& expandParamsOnBest) at System.Management.Automation.Adapter.GetBestMethodAndArguments(String methodName,MethodInformation [] methods,PSMethodInvocationConstraints invocationConstraints,Object [] arguments,Object []& newArguments) 在System.Management.Automation.ComMethod.InvokeMethod(PSMethod方法,Object []参数) 在System.Management.Automation.Adapter.BaseMethodInvoke(PSMethod方法,PSMethodInvocationConstraints invocationConstraints,Object []参数)
完整代码:
function applydoctemplate ()
{
param(
[string]$srcfile,
[string]$dpath
)
$source = $srcfile
$destination = $dpath
$TparentPath = $env:USERPROFILE
$template = Join-Path -Path $TparentPath -ChildPath “Documents\xxxxxxx\Template.dotx”
Add-Type -AssemblyName office
[ref]$SaveFormat = "microsoft.office.interop.word.WdSaveFormat" -as [type]
$word = New-Object -ComObject word.application
$word.visible = $false
$word2 = New-Object -ComObject word.application
$word2.visible = $false
$doc = $word.documents.open($source)
$doc2 = $word2.documents.add($template)
$range = $doc.Range()
$copy=""
$copy = $range.Copy()
$range2 = $doc2.Range()
$range2.Paste($copy)
$doc2.saveas([ref]$destination,[ref]$saveFormat::wdFormatDocument)
$word.Application.ActiveDocument.Close()
$word.Quit()
$word2.Application.ActiveDocument.Close()
$word2.Quit()
}
这对少数民族不起作用。 任何建议。
答案 0 :(得分:0)
$template = ($word.Templates | Where-Object {
$.Name -match "Normal.dot"
}).Path