简体   繁体   中英

Copy data from specified excel file to word document

can someone please help - I am using this code to copy the data from excel to word:

Sub CreateRapport()

Dim wdApp As Object
Dim wd As Object

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 wd = wdApp.Documents.Add

wdApp.Visible = True

Sheets("Rapport").Activate
Set Rng = ThisWorkbook.ActiveSheet.Range("A1:E76")

Rng.Copy

With wd.Range
    .Collapse Direction:=0                  'Slutet av dokumentet
    .InsertParagraphAfter                   'Lägg till rad
    .Collapse Direction:=0                  'Slutet av dokumentet
    .PasteSpecial False, False, True        'Pasta som Enhanced Metafile
    End With
    End Sub

What would I need to modify in the code to copy the data from a specified Excel file eg "C:\\Book.xlsx" (not ThisWorkbook)? I am a newby in VBA so any hints would help. Thanks!

Sub CreateRapport()

Dim wdApp As Object
Dim wd As Object

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 wd = wdApp.Documents.Add

wdApp.Visible = True

Dim wBK as WorkBook
Set wBK = Workbooks.Open("C:\\test.xlsx")

wBK.Sheets("Rapport").Activate
Set Rng = wBK.ActiveSheet.Range("A1:E76")

Rng.Copy

With wd.Range
    .Collapse Direction:=0                  'Slutet av dokumentet
    .InsertParagraphAfter                   'Lägg till rad
    .Collapse Direction:=0                  'Slutet av dokumentet
    .PasteSpecial False, False, True        'Pasta som Enhanced Metafile
End With
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM