【问题标题】:InlineShapes.AddOLEObject without opening the MSWord UIInlineShapes.AddOLEObject 无需打开 MSWord UI
【发布时间】:2020-06-05 19:23:55
【问题描述】:

我正在尝试使用 VBA 从打开的 MSWord 文档中添加嵌入式 OLE(空 MSWord 文档,图标:=True),而无需打开新创建的空 MSWord 文档的用户界面)。以下 VBA 将当前文档中的第二段替换为 MSWord OLE 图标,但它会在自己的窗口中打开 MSWord 图标文档。

Sub InsertOLEobject()
    ActiveDocument.InlineShapes.AddOLEObject _
    ClassType:="Word.Document", DisplayAsIcon:=True, _
    Range:=ActiveDocument.Paragraphs(2).Range 'Replaces the second paragraph with the MSWord Icon
End Sub

谁能给我一些建议?

下面是最新的 VBA 代码来做我想做的事。如果您使用现有文件,InlineShapes.AddOLEObject 将不会打开 Word 用户界面。我只是希望有一种更优雅的方式,而无需创建需要删除的文件。

Sub InsertOLEobject()
    Dim docNewBlank As Document
    Set docNewBlank = Documents.Add
    Set DocA = ActiveDocument
    With docNewBlank
        'Centering the paragraph within the blank document
        .Paragraphs(1).Alignment = wdAlignParagraphCenter
        'Saved the file to a temporary scratch location
        .SaveAs FileName:="c:\temp\Blank.doc"
        .Close 'Close the word document
    End With

    'Add the embedded MSWord OLE Icon
    ActiveDocument.InlineShapes.AddOLEObject _
        ClassType:="Word.Document", _
        FileName:="c:\Temp\Blank.doc", _
        DisplayAsIcon:=True, _
        IconLabel:="Blank.doc", _
        IconFileName:="WINWORD.EXE", _
        Range:=ActiveDocument.Paragraphs(2).Range
        'Set the filename of your choice
        'Set the Range to the destination of the ole icon
        'Set the icon to the path of your WINWORD.EXE
End Sub

【问题讨论】:

    标签: vba ms-word


    【解决方案1】:

    FWIW 我不确定我是否完全遵循了您正在尝试做的事情,但我想知道这是否会满足您的需求(除了文档不是很空并且将是 .docx 而不是 .doc ):

    Sub embedNearlyEmptyWordDoc()
    Dim appFullName As String
    ' Windows only, perhaps there is a better place to get this string
    appFullName = Application.Path & Application.PathSeparator & "winword.exe"
    With ActiveDocument
    ' insert a space somewhere "safe" (not necessarily where I have put it
      .Range(0, 0).Text = " "
      .Range(0, 1).Copy
      .Range(0, 0).Text = ""
    End With
    ' Alter this to put the doc where you want
    ActiveDocument.Paragraphs(2).Range.PasteSpecial IconIndex:=0, Link:=False, Placement:=wdInLine, DisplayAsIcon:=True, DataType:=wdPasteOLEObject, IconFileName:=appFullName, IconLabel:="Blank.docx"
    End Sub
    

    【讨论】:

    • 我可以使用您的解决方案进行一些修改,以避免在 C:\temp 中创建“blank.doc”;我试过了,它奏效了。在尝试它时,您最终打开了一个灯泡来关闭“ActiveDocument”的解决方案。谢谢你“有点刻薄”!
    【解决方案2】:

    最终的解决方案是在插入Word OLE Icon后添加“ActiveWindow.Close”,名称为空;这将关闭刚刚由“ActiveDocument.InlineShapes.AddOLEObject”打开的窗口。所以我不必先创建一个空白.doc;所以我删除了那个 VBA 代码。

    有人问我在这个项目中的目标是什么;我的目标是在文档中剪切表格和图像并将它们粘贴到 Word OLE 对象图标中。将所有图像和表格封装到 Word OLE 对象图标中的结果 word 文件可用作 Rational DOORS (DOORS) 导出插件的输入。我可以直接导出 DOORS 中的图像和表格,以便在 DOORS 中查看它们,但是如果您曾经双击 DOORS 中的图像或表格,您会发现 OLE 对象在单元格中打开并且很难编辑,调整大小或位置。我将表格函数与 inlineshapes 子函数分开;我的 VBA 代码如下:

    '================================================================================
    ' Cut all Tables and paste them into Word OLE Icons
    '================================================================================
    Sub IconizeInlineTables()
    '================================================================================
    Dim iTable As Table
    Dim RngA As Range
    Dim RngB As Range
    Dim RngC As Range
    Dim DocA As Document
    Dim DocC As Document
    Set DocA = ActiveDocument
    
    For Each iTable In DocA.Tables
        Set RngA = iTable.Range 'Go to the start of the table
        RngA.Expand Unit:=wdTable
        Set RngB = RngA.Duplicate 'Duplicate the range to keep track of table location
        RngB.Collapse Direction:=wdCollapseEnd 'Collapse the Duplicate range to the point after the Table
        RngA.Select
    
        'MsgBox "Tables Left to Process =" & DocA.Tables.Count
    
        Selection.Cut 'Cut the Table and put it in the paste buffer
    
        'Insert the MSWord.doc OLE icon
    
        ActiveDocument.InlineShapes.AddOLEObject _
            ClassType:="Word.Document", _
            FileName:="", _
            LinkToFile:=False, _
            DisplayAsIcon:=True, _
            IconFileName:="WINWORD.EXE", _
            IconLabel:="Table-" & iTableCount & ".doc", _
            Range:=RngB
        ActiveWindow.Close
    
        'Expand the range to include the OLE icon then select the new range
    
        With RngB
            .Expand Unit:=wdParagraph
            .Paragraphs(1).Style = wdStyleBodyText 'Apply the Body Text style
            .Paragraphs(1).Format.Alignment = wdAlignParagraphCenter 'Center the paragraph
            .InlineShapes(1).Select 'Select the inlineshape containing the OLE
        End With
    
        'Make sure that the InlineShape is the OLE icon
    
        If InStr(1, RngB.InlineShapes(1).OLEFormat.ProgID, "Word.Document.", vbTextCompare) Then
            Set DocC = RngB.InlineShapes(1).OLEFormat.Object 'Select the OLE Object
            Set RngC = DocC.Paragraphs(1).Range 'Select the first Paragraph within the OLE Object
            With RngC
                .Paragraphs(1).Style = wdStyleBodyText 'Apply the Body Text style
                .Paragraphs(1).Format.Alignment = wdAlignParagraphCenter 'Center the paragraph
                .Collapse Direction:=wdCollapseEnd 'Collapse to just beyond end of paragraph
                .MoveEnd Unit:=wdCharacter, Count:=-1 'Back the RngC up one character to not include the paragraph marker
                .Select
                .Paste 'Paste the Table cut with RngA.cut here
            End With
        Else 'Something I did not expect happened
            MsgBox "Error: " & Selection.InlineShapes(1).OLEFormat.ProgID & "Not Expected"
        End If
    Next
    End Sub
    
    '================================================================================
    ' Cut all Figures and paste them into Word OLE Icons
    '================================================================================
    Sub IconizeInlineShapes()
    '================================================================================
    Dim iShape As InlineShape
    Dim RngA As Range
    Dim RngB As Range
    Dim RngC As Range
    Dim DocA As Document
    Dim DocC As Document
    Set DocA = ActiveDocument
    
    Selection.GoTo What:=wdGoToSection, Which:=wdGoToFirst 'Start at the beginning of the document
    
    For Each iShape In DocA.InlineShapes
        If iShape.Type = wdInlineShapePicture Then
            Set RngA = iShape.Range 'Set the range to the image
            Set RngB = RngA.Duplicate 'Duplicate the range to keep track of location
            RngB.Collapse Direction:=wdCollapseEnd 'Collapses to the end of the selection
            RngA.Select
            Selection.Cut 'Cut the image and put it in the paste buffer
           'Insert the MSWord OLE icon
            ActiveDocument.InlineShapes.AddOLEObject _
                ClassType:="Word.Document", _
                FileName:="", _
                LinkToFile:=False, _
                DisplayAsIcon:=True, _
                IconFileName:="WINWORD.EXE", _
                IconLabel:="Figure-" & DocA.InlineShapes.Count & ".doc", _
                Range:=RngB
            ActiveWindow.Close
    
            'The inserted OLE is the next InlineShape to be found so you don't have to select it just loop around to next iShape
    
        ElseIf InStr(1, iShape.OLEFormat.ProgID, "Word.Document.", vbTextCompare) Then 'Make sure iShape OLE object was next
            Set DocC = iShape.OLEFormat.Object 'Select the OLE object
            Set RngC = DocC.Paragraphs(1).Range 'Select the first Paragraph within the OLE Object
            RngC.Paragraphs(1).Style = wdStyleBodyText 'Apply the Body Text style
            RngC.Paragraphs(1).Format.Alignment = wdAlignParagraphCenter 'Center the paragraph
            RngC.Collapse Direction:=wdCollapseEnd 'Collapse to just beyond end of paragraph
            RngC.MoveEnd Unit:=wdCharacter, Count:=-1 'Back the RngC up one character to not include the paragraph marker
            RngC.Select
            RngC.Paste 'Paste the image cut with RngA.cut here
        Else 'Something I did not expect happened
            MsgBox "Warning: " & iShape.OLEFormat.ProgID & "Not handled by this software"
        End If
    Next
    End Sub
    
    

    请注意,“IconFileName:="WINWORD.EXE”是通用的,但可能需要针对您的特定安装进行修改。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-27
      • 1970-01-01
      相关资源
      最近更新 更多