2017-06-29 83 views
1

我嘗試插入一個txt文件,文件與插入對象在文檔中

$doc.selection.InlineShapes.AddOLEObject($txtFile) 

但是當我運行它,我得到一個錯誤信息:

The program used to create this object is..... that program is either not installed on your computer or it is not responding

可能是什麼問題? TNX

代碼:

$Global:path = "C:\Users\user\desktop" 
$txtFile  = New-Item $Global:path -Name TxtFile.txt 
$docx   = New-Object -ComObject Word.Application 
$docx.Visible = $true 
$docxFileName = $docx.Documents.add() 

$docx.Selection.range.InsertAfter("hello") 
$docx.Selection.InlineShapes.AddOLEObject($txtFile) 
$docxFileName.SaveAs([ref]$Global:path,[ref]$SaveFormat::wdFormatDocument) 

$docx.Quit() 
+2

請張貼代碼 – gms0ulman

+0

tnx。我現在提出了 – Olive

回答

0

使用此。請閱讀代碼中的評論。
檢查出the documentation for AddOLEObject欲知更多信息。

$Global:path = "C:\Users\user\desktop" 

# I've added the -Force flag. because if the file already exists, 
# $txtFile will contain an error instead of the object you expect 
# $Global:path > $path because you don't need to specify the scope when you use a variable. 
$txtFile  = New-Item $path -Name TxtFile.txt -Force 
$docx   = New-Object -ComObject Word.Application 
$docx.Visible = $true 
$docxFileName = $docx.Documents.add() 

$docx.Selection.range.InsertAfter("hello") 

# This line should contain an empty argument for ClassType, then the file path for FileName 
$docx.Selection.InlineShapes.AddOLEObject("",$txtFile.FullName) 

# again, $global:path is not required. 
# you can specify a path and the docx extension will be added automagically 
$docxFileName.SaveAs("$path\somefilename") 

$docx.Quit() 
+0

謝謝!它現在正在工作:) – Olive