如何将带有标题的多个表从Excel复制到带有宏的Word女士?

时间:2016-06-05 08:49:43

标签: excel vba excel-vba

我有这个代码来复制excel表并将每个表的标题提供给Word女士

Sub RoundedRectangle2_Click()
Dim wdApp As Object
Dim wd As Object
Dim OrderNo As Long
Dim OrderNo2 As Long
Dim WrkSht As Worksheet
Dim strValue As String

On Error Resume Next
Set wdApp = GetObject(, "Word.Application")
If Err.Number <> 0 Then
    Set wdApp = CreateObject("Word.Application")
End If
On Error GoTo 0

Set WrkSht = Sheet1
Set wd = wdApp.Documents.Add
wdApp.Visible = True

WrkSht.Activate
OrderNo = 1
For OrderNo2 = OrderNo To 16
    OrderNo = Cells(OrderNo2, 1).End(xlDown).Row
    Set Rng = ActiveCell.Range("A" & OrderNo2 & ":C" & OrderNo)
    strValue = Cells(OrderNo2 + 1, 1)

    Rng.Copy
    With wd.Range
    wdApp.Selection.TypeText Text:=strValue
        .Collapse Direction:=0
        .InsertParagraphAfter
        .Collapse Direction:=0
        .PasteSpecial False, False, True
    End With
    wdApp.Selection.TypeParagraph

    OrderNo2 = OrderNo + 1
Next
End Sub

例如我在excel上有下表

product | sub_porduct | price |
A       |    1231     |   6   |
A       |    3331     |   5   |
A       |    1233     |   9   |

product | sub_porduct | price |
B       |    1299     |   10  |
B       |    1001     |   9   |

product | sub_porduct | price |
C       |    1871     |  15   |
C       |    1854     |  17   |
C       |    1782     |  19   |
C       |    1771     |  18   |

问题在于我把TypeText放在哪里,他们总是写在Word女士身上,如下所示。

A
B
C

product | sub_porduct | price |
A       |    1231     |   6   |
A       |    3331     |   5   |
A       |    1233     |   9   |

product | sub_porduct | price |
B       |    1299     |   10  |
B       |    1001     |   9   |

product | sub_porduct | price |
C       |    1871     |  15   |
C       |    1854     |  17   |
C       |    1782     |  19   |
C       |    1771     |  18   |

我的预期是在每个表之前写入标题,并为ms上的每个标题提供格式header1。字

A
product | sub_porduct | price |
A       |    1231     |   6   |
A       |    3331     |   5   |
A       |    1233     |   9   |

B
product | sub_porduct | price |
B       |    1299     |   10  |
B       |    1001     |   9   |

C
product | sub_porduct | price |
C       |    1871     |  15   |
C       |    1854     |  17   |
C       |    1782     |  19   |
C       |    1771     |  18   |

请帮助修复代码。对于任何帮助都有很多帮助

0 个答案:

没有答案