如何粘贴到表格的第二列?

时间:2014-08-18 19:50:44

标签: vba word-vba

我正在为Microsoft Word 2007编写一个宏。该宏应该制作一个n x 2表格,然后将选中的一张照片插入到每个单元格中。相反,它会在第一列中为每个单元格放置两张照片而在第二列中不放置任何照片。

With Application.ActiveDocument    
    Dim myTable As Word.Table
    Set myRange = ActiveDocument.Content
    ActiveWindow.View.TableGridlines = True
    Set myTable = .Tables.Add(Range:=myRange, NumRows:=Fix((Num / 2) + 0.5), NumColumns:=2)
        For I = 1 To Num
        oRow = Fix((I / 2) + 0.5) '
        If (I Mod 2) = 1 Then oCol = 1
        If (I Mod 2) = 0 Then oCol = 2
        myTable.Cell(oRow, oCol).Select
        Set pic = .InlineShapes.AddPicture(FileName:=sDir(I), LinkToFile:=False, SaveWithDocument:=True)
            With pic
            .LockAspectRatio = msoFalse
            .Height = InchesToPoints(2.25)
            .Width = InchesToPoints(3)
            End With
        Selection.Collapse
    Next
End With

1 个答案:

答案 0 :(得分:0)

这样做:

Dim tblCell as Word.Cell

因为对于可读性和智能感知,使用强类型变量通常更容易:

Set tblCell = myTable.Cell(oRow, oCol)
Set pic = tblCell.Range.InlineShapes.AddPicture(FileName:=sDir(I), LinkToFile:=False, SaveWithDocument:=True)
        With pic
        .LockAspectRatio = msoFalse
        .Height = InchesToPoints(2.25)
        .Width = InchesToPoints(3)
        End With

解释:

在你的调试中,你做了:

  

我尝试更换“设置图片.InlineShapes.AddPicture...End withSelection.TypeText ("HI")

哪个应该有用,因为您直接使用Selection插入文本。但是,由于您只有ActiveDocument(您使用With ActiveDocument开始编码)

.InlineShapes.AddPicture(...

你最初使用的方法不起作用似乎很奇怪。当我在2010年完成它时,它只将所有图片放在FIRST单元格中。

相关问题