使用Selection.PasteSpecial插入图片不起作用

时间:2015-12-04 11:42:08

标签: vba word-vba

我使用此代码从剪贴板插入图片:

Selection.PasteSpecial Link:=False, DataType:=wdPasteDeviceIndependentBitmap, Placement:=wdInLine, _
            DisplayAsIcon:=False

此代码适用于Word 2007但不适用于Word 2013.在W07中,图片被复制到文档中,使用W13我只获得一个空白文档。

我在没有DataTypeSelection.PasteSpecial Placement:=wdInLine)的情况下尝试过但仍然相同。

剪贴板中的图片正确,如"插入"

所示

enter image description here

我怎样才能让它发挥作用?感谢

使用选择更新代码:

Selection.EndKey Unit:=wdStory
ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=1, NumColumns:=2
Selection.Cells.SetWidth ColumnWidth:=CentimetersToPoints(11.5), RulerStyle:=wdAdjustNone
Selection.Rows.SpaceBetweenColumns = CentimetersToPoints(0.25)
Selection.Move Unit:=wdColumn, Count:=1
Selection.SelectColumn
Selection.Cells.SetWidth ColumnWidth:=CentimetersToPoints(4.5), RulerStyle:=wdAdjustNone
Selection.Rows.SpaceBetweenColumns = CentimetersToPoints(0.25)
Selection.SelectColumn
Selection.Cells.HeightRule = wdRowHeightAuto
With Selection.Rows
    .Alignment = wdAlignRowLeft
    .AllowBreakAcrossPages = True
    .SetLeftIndent LeftIndent:=CentimetersToPoints(0), RulerStyle:=wdAdjustNone
End With
Selection.Tables(1).Select
Selection.Borders(wdBorderTop).LineStyle = wdLineStyleNone
Selection.Borders(wdBorderLeft).LineStyle = wdLineStyleNone
Selection.Borders(wdBorderBottom).LineStyle = wdLineStyleNone
Selection.Borders(wdBorderRight).LineStyle = wdLineStyleNone
Selection.Borders(wdBorderHorizontal).LineStyle = wdLineStyleNone
Selection.Borders(wdBorderVertical).LineStyle = wdLineStyleNone
Selection.MoveLeft Unit:=wdCharacter, Count:=1

Selection.PasteSpecial Link:=False, DataType:=wdPasteDeviceIndependentBitmap, Placement:=wdInLine, _
            DisplayAsIcon:=False

 Selection.MoveRight Unit:=wdCharacter, Count:=1

1 个答案:

答案 0 :(得分:1)

附加代码有帮助 - 我编辑它以使用Word对象模型而不是选择。它现在应该执行得更快,更可预测,更容易阅读。您的代码中不清楚的是您是否要更改第一列或第二列的列设置以及图片是否应位于第一个或第二个单元格中。如果我弄错了,你可以简单地改变索引号(从1到2)。

我将PasteSpecial更改为粘贴到单元格区域(而不是“到”单元格)。如果“目标”不包含细胞的结构存储(细胞末端标记),则可能有所帮助。试一试......

Dim rngDoc as Word.Range, rngTbl as Word.Range
Dim tbl as Word.Table, col1 as Word.Column
Set rngDoc = ActiveDocument.Content
rngDoc.Collapse wdCollapseEnd  'End of the document
Set tbl = ActiveDocument.Tables.Add Range:=rngDoc, NumRows:=1, NumColumns:=2
Set rngTbl = tbl.Range
rngTbl.Cells.SetWidth ColumnWidth:=CentimetersToPoints(11.5),
       RulerStyle:=wdAdjustNone
tbl.Rows.SpaceBetweenColumns = CentimetersToPoints(0.25)
tbl.Columns(1).Cells.SetWidth ColumnWidth:=CentimetersToPoints(4.5), 
       RulerStyle:=wdAdjustNone
tbl.Columns(1).Cells.HeightRule = wdRowHeightAuto
With tbl.Rows
  .Alignment = wdAlignRowLeft
  .AllowBreakAcrossPages = True
  .SetLeftIndent LeftIndent:=CentimetersToPoints(0), 
          RulerStyle:=wdAdjustNone
End With
With tbl
  .Borders(wdBorderTop).LineStyle = wdLineStyleNone
  .Borders(wdBorderLeft).LineStyle = wdLineStyleNone
  .Borders(wdBorderBottom).LineStyle = wdLineStyleNone
  .Borders(wdBorderRight).LineStyle = wdLineStyleNone
  .Borders(wdBorderHorizontal).LineStyle = wdLineStyleNone
  .Borders(wdBorderVertical).LineStyle = wdLineStyleNone
End With
Set rng = tbl.Cell(1,1).Range
rng.Collapse wdCollapseStart
rng.PasteSpecial Link:=False, DataType:=wdPasteDeviceIndependentBitmap, 
    Placement:=wdInLine, DisplayAsIcon:=False