复制表,使用vba指定从excel到word的位置

时间:2014-06-15 11:41:04

标签: excel vba excel-vba word-vba

我使用此代码将表格从excel复制到word

    Sub exceltoword()
Dim rangeToCopy As Range
Set rangeToCopy = Range("A1").CurrentRegion
Dim wordApp As Word.Application
Set wordApp = New Word.Application
wordApp.Visible = True
Dim wordDoc As Word.Document
Set wordDoc = wordApp.Documents.Open("C:\Users\mohammad.taha\AppData\Roaming\Microsoft\Templates\ARABBANK -SALARIES STATEMENT.dotx")
wordDoc.Application.Selection.Find.Text = "H"
wordDoc.Application.Selection.Find.Execute
wordDoc.Application.Selection.MoveDown Unit:=wdLine
rangeToCopy.Copy
wordDoc.Words(1).PasteExcelTable False, False, False
End Sub

但表格被粘贴到文档的第一行,我想将表格粘贴到文档中间的特定位置,我应该如何修改此代码?

1 个答案:

答案 0 :(得分:0)

尝试使用以下代码:

Sub exceltoword()
    Dim rangeToCopy As Range
    Set rangeToCopy = Range("A1").CurrentRegion
    Dim wordApp As Word.Application
    Set wordApp = New Word.Application
    wordApp.Visible = True
    Dim wordDoc As Word.Document
    Set wordDoc = wordApp.Documents.Open("C:\Users\mohammad.taha\AppData\Roaming\Microsoft\Templates\ARABBANK -SALARIES STATEMENT.dotx")
    wordDoc.Application.Selection.Find.Execute "H"
    wordApp.Selection.MoveRight Unit:=wdCharacter, Count:=1
    wordApp.Selection.TypeParagraph
    wordApp.Selection.TypeParagraph
    rangeToCopy.Copy
    wordApp.Selection.PasteExcelTable False, False, False
    End Sub

我添加了两段。如果在您的模板中看起来不太好,可以将其删除。