簡體   English   中英

VBA Word 將選定的內嵌形狀添加到表格單元格

[英]VBA Word Add selected inline shape to a table cell

我在 Word 中選擇了一個內聯形狀對象,並嘗試使用 VBA 將其粘貼(移動)到表格單元格中。 但我找不到任何 API 來完成這項工作。 我想這樣做

// C#
myCell.InlineShapes.Add(selection.InlineShapes[1]);

但是我找不到 VBA 或 C# 中的 API 可以做到這一點。

我發現這個 SO 鏈接幾乎可以完成工作,但它直接將圖片從文件加載到表格單元格中。

如何將所選圖像放入單元格(在 Shapes 或 InlineShapes 中)? 謝謝

更新:

我在這里找到了 Cindy Meister 的另一個例子,為我提供了正確方法的提示。 謝謝你,辛迪!!

這段代碼終於奏效了。 我必須通過剪貼板並獲取單元格范圍才能使用 PasteSpecial 從剪貼板中提取圖片。 奇怪的。 但它奏效了。 也許有更好的方法。

// C#
sel.CopyAsPicture();
var cell = table.Cell(row, 1);

// Must use cell.PasteSpecial to pull in the image from the clipboard
var range = cell.Range;
range.PasteSpecial(null, null, WdOLEPlacement.wdInLine, false,
    WdPasteDataType.wdPasteShape);

試試這個代碼:

Sub MoveShapeToTable()
    Dim ish As InlineShape, tblNew As Table
    
    On Error Resume Next
    Set ish = Selection.InlineShapes(1)
    If Err.Number <> 0 Then
        MsgBox "No InlineShapes selected", vbCritical
        Exit Sub
    End If
    On Error GoTo 0
    
    ish.Range.Cut
    With ActiveDocument
        Set tblNew = .Tables.Add(Range:=.Range.Characters.Last, NumRows:=2, NumColumns:=1, _
                     DefaultTableBehavior:=wdWord9TableBehavior)
        tblNew.Cell(1, 1).Range.Paste
    End With
End Sub

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM