将图像从excel插入到表格中

时间:2015-10-08 14:45:04

标签: vba excel-vba word-vba word-template excel

我需要从excel调用word文件,并创建一个模板。模板将有3个图像UpperLeft,Upper Right和中央图像。

我认为这将是通过表格的最佳结果。

由于某些原因,我无法从excel创建表格。

Private Sub CommandButton13_Click()   
    'Using early binding, not late-binding
    Dim wsDoc As Word.Document
    Dim wsApp As Word.Application
    Dim wsRng As Word.Range
    Dim wsTable as Word.Table

    Dim intNoOfRows
    Dim intNoOfColumns

    Dim s As Word.InlineShape
    Dim shp As Word.Shape

    intNoOfRows = 4
    intNoOfColumns = 2

    Set wsApp = New Word.Application
    wsApp.Visible = True

    Set wsDoc = wsApp.Documents.Add
    Set wsRange = wsDoc.Content

    Set wsTable = wsDoc.Tables.Add(wsRange, intNoOfRows, intNoOfColumns)
    wsTable.Borders.Enable = True
    wsTable.Cell(1, 1).Range.InlineShapes.AddPicture _
                       UserForm1.txtImageLogoAdecco
    wsTable.Cell(1, 2).Range.ParagraphFormat.Alignment = wdAlignParagraphRight
    wsTable.Cell(1, 2).Range.InlineShapes.AddPicture _
                       UserForm1.txtImageLogoClient
    Set wsRng = wsTable.Cell(2, 1).Range
    With wsRng.Paragraphs.Add

    wsTable.Cell(2, 1).Merge MergeTo:=wsTable.Cell(2, 2)
    wsTable.Cell(2, 1).Height = 520
    wsTable.Cell(2, 1).Range.Paragraphs.Add
    wsTable.Cell(3, 1).Merge MergeTo:=objTable.Cell(3, 2)
    wsTable.Cell(3, 1).Range.Text = "Prepared by:" & "  " & UserForm1.txtPrepared
    wsTable.Cell(4, 1).Merge MergeTo:=objTable.Cell(4, 2)
    wsTable.Cell(4, 1).Range.ParagraphFormat.Alignment = wdAlignParagraphRight
    wsTable.Cell(4, 1).Range.Text = "Belgrade," & " " & Format(Date, "MMMM DD, YYYY ")

    Set wsRange = Nothing
    Set wsTable = Nothing
    Set wsDoc = Nothing
    Set wsApp = Nothing
End Sub

1 个答案:

答案 0 :(得分:0)

嗯,它有点清晰,但并不完全。我已经冒昧地编辑您的代码,以便它正确,一致且可读(没有所有空行)!

我将从明确的部分开始:<<我想将光标向下移动几行并写一些文本。 >>

要将焦点移动到表格下方,您将获得表格的范围,然后将其折叠。例如:

设置wsRange = wsTable.Range wsRange.Collapse wdCollapseEnd '现在范围在表格后面的段落中 wsRange.Text ="表格后面的文字"

"带背景图片我想将其转换为形状。我想发送iy背后的文字"

这是我不清楚的部分。您是说要再插入一张图像并将其放在文本后面?使用Shapes.Add方法并将WrapFormat.Type设置为wdWrapBehind