简体   繁体   English

VBA Word 将选定的内嵌形状添加到表格单元格

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

I have an inline shape object selected in Word and am trying to paste it (move it) into a table cell using VBA.我在 Word 中选择了一个内联形状对象,并尝试使用 VBA 将其粘贴(移动)到表格单元格中。 But I can't find any API to do the job.但我找不到任何 API 来完成这项工作。 I would like to do this我想这样做

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

But there are no APIs in VBA or C# that I can find to do that.但是我找不到 VBA 或 C# 中的 API 可以做到这一点。

I found this SO link that almost does the job , but it loads a picture directly from a file into the table cell.我发现这个 SO 链接几乎可以完成工作,但它直接将图片从文件加载到表格单元格中。

How can I get the selected image into the cell (either in Shapes or InlineShapes)?如何将所选图像放入单元格(在 Shapes 或 InlineShapes 中)? Thank you谢谢

UPDATE:更新:

I found another example by Cindy Meister here to give me a hint on the right approach.我在这里找到了 Cindy Meister 的另一个例子,为我提供了正确方法的提示。 Thank you, Cindy!!谢谢你,辛迪!!

This code finally worked.这段代码终于奏效了。 I had to go through the clipboard and get the cell range to pull in the picture from the clipboard using PasteSpecial.我必须通过剪贴板并获取单元格范围才能使用 PasteSpecial 从剪贴板中提取图片。 Weird.奇怪的。 But it worked.但它奏效了。 Maybe there is a better way.也许有更好的方法。

// 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);

Try this code:试试这个代码:

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