Open Excel from Word - Select and paste data

FredrikNilsen

New Member
Joined
Jan 25, 2021
Messages
26
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2013
Platform
  1. Windows
Not sure if this is better posted in a Word VBA forum, but I'll try here first:

I have a Word template, and I want to have a macro to open the file dialogue, select an Excel file, select all data in Sheet1, and copy and paste it to a bookmark ("Data") in the Word template (the data should probably not be linked). I have this code for the first part, but I'm not sure how to approach the rest.

VBA Code:
Sub SelectExcelFile()

    Dim xlApp As Excel.Application
    Dim xlWbk As Excel.Workbook
        
    Dim strFile As String
    
    strFile = Excel.Application.GetOpenFilename()
    Workbooks.Open (strFile)
    
    Set xlApp = Excel.Application
    xlApp.Visible = False
        
End Sub
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
For example, assuming your new document has already been created:
VBA Code:
Sub CopyExcelTable()
Application.ScreenUpdating = False
Dim xlApp As Excel.Application, xlWkBk As Excel.Workbook, xlRng As Excel.Range
Dim StrWkBkNm As String, lRow As Long, lCol As Long
With Application.FileDialog(FileDialogType:=msoFileDialogFilePicker)
  .AllowMultiSelect = False
  .Filters.Add "Excel Files", "*.xl*", 1
  .FilterIndex = 1
  'Use Show method to display File Picker dialog box and return user's action
  If .Show = -1 Then
    StrWkBkNm = .SelectedItems(1)
  Else
    Exit Sub
  End If
End With
If UBound(Split(StrWkBkNm, ".xls")) = 0 Then Exit Sub
Set xlApp = CreateObject("Excel.Application")
With xlApp
  .Visible = False
  Set xlWkBk = .Workbooks.Open(FileName:=StrWkBkNm, ReadOnly:=True, AddToMRU:=False)
  With xlWkBk
    With .Sheets(1)
      lRow = .UsedRange.Cells.SpecialCells(xlCellTypeLastCell).Row
      lCol = .UsedRange.Cells.SpecialCells(xlCellTypeLastCell).Column
      Set xlRng = .Range("A1:" & .Cells(lRow, lCol)).Range
      xlRng.Copy
    End With
    .Close False
  End With
  .Quit
End With
ActiveDocument.Bookmarks("Data").Range.Paste
Set xlRng = Nothing: Set xlWkBk = Nothing: Set xlApp = Nothing
Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,828
Messages
6,181,217
Members
453,024
Latest member
Wingit77

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top