You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

通过PowerShell执行Word宏时OLEObject标签不显示问题

问题:通过PowerShell插入Excel OLE对象后标签不显示

我有一个可在Word书签位置插入Excel(.xlsx)文档的VBA宏,直接在Word中执行该宏时一切正常,但通过PowerShell调用执行时,插入的文档对应的OLEObject标签不显示。右键点击对象选择「对象/转换」可看到标签存在,无需修改点击「确定」后标签即可正常显示。

同时尝试仅用PowerShell实现相同插入功能,不依赖Word宏,仍出现相同的标签不显示问题。


执行Word宏的PowerShell代码

$word = New-Object -ComObject Word.Application
$document = $word.Documents.Open("$path\myfile.docm")
$document.Application.Run("InsertXLSX")
$document.save()
$document.SaveAs2("$path\myfile.docx", [Microsoft.Office.Interop.Word.WdSaveFormat]::wdFormatDocumentDefault)
$document.Close()
$word.Quit()

Word宏(VBA)简化代码

Sub InsertXLSX()
    Dim path As String
    path = "..."
    Dim ExcelPath As String
    Dim IconPath As String
    ExcelPath = path + "\Data\xlsx\"
    IconPath = path + "\Data\excel.ico"
    
    bookmark= "toto"

    ActiveDocument.InlineShapes.AddOLEObject _
    FileName:=ExcelPath + bookmark + ".xlsx", _
    ClassType:="Excel.Sheet", _
    DisplayAsIcon:=True, _
    IconIndex:=0, _
    IconFileName:=IconPath, _
    IconLabel:=bookmark, _
    Range:=ActiveDocument.Bookmarks(bookmark).Range

    Set selectionRange = Nothing
End Sub

仅用PowerShell实现的代码

$word = New-Object -ComObject Word.Application
$document = $word.Documents.Open("pathtofolder\docfile.docx")

$ClassType     = "Excel.Sheet"
$FileName      = "pathtofolder\clsxfile.xlsx"
$LinkToFile    = $False
$DisplayAsIcon = $True
$IconFileName  = "pathtofolder\icone.ico"
$IconIndex     = 19 # or 0 or 1 or whatever
$IconLabel     = "this is the label"

$oleObject = $document.Bookmarks.Item("mybookmark").range.InlineShapes.AddOLEObject($ClassType,$FileName,$LinkToFile,$DisplayAsIcon,$IconFileName,$IconIndex,$IconLabel)

$document.Save()
$word.Quit()

问题效果示例

OLE对象标签不显示效果

内容的提问来源于stack exchange,提问作者Jean-Philippe Lasson

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.09 08:01:24