Excel的VBA;从excel复制数据并将其粘贴到不同位置的单词模板中

时间:2016-06-09 13:44:11

标签: excel-vba vba excel

我在Excel工作表中有一些数据,这些数据是由VBA从另一张工作表中的主数据中提取的。数据显示在A& A列中的Excel表格中。乙

我有一个MS-Word模板,详情如下:

“估计相当于卢比。 [B1]基于上述[B2]的核准费率,由主管当局批准。根据这一估计,投标金额相当于卢比。 [B3]受邀参加投标号[B4]日期[B5]已在[B7]开幕 这个估计为卢比。 [B1]已经[b5]“

批准

我想在Excel中创建一个宏,通过该宏将“B”列的不同单元格的值粘贴到模板中代替相应的[]来创建新的单词doc。

请帮帮我。

1 个答案:

答案 0 :(得分:0)

假设您的数据位于“Sheet1”的单元格B1到B7中,则以下代码会将所需文本粘贴到文档“C:\ TestFolder \ Test.docx”中。显然,您可以编辑文件名和路径名以适应。

Sub PasteStuffIntoWord()
Dim ws As Worksheet
Dim TextToPaste As String
Dim objWord As Object
Dim objSelection As Object

Set ws = Worksheets("Sheet1")
With ws
    TextToPaste = "An estimate amounting to Rs. " & .Range("B1").Value & _
    " based on approved rates for the above " & .Range("B2").Value & _
    " was sanctioned by the competent authority. Against this estimate tenders amounting to Rs. " _
    & .Range("B3").Value & " were invited vide Tender No. " & .Range("B4").Value & " Dated " _
    & .Range("B5").Value & " which has opened on " & .Range("B7").Value & " This estimate for Rs. " _
    & .Range("B1").Value & " has been approved on " & .Range("B5").Value
End With

    Set objWord = CreateObject("word.Application")
    objWord.documents.Open "C:\TestFolder\Test.docx"
    objWord.Visible = True
    Set objSelection = objWord.Selection
    objSelection.TypeText TextToPaste

End Sub