我有一个打开的word文档,我正在尝试使用VBA从excel文件中复制一个表。代码用word doc编写。这是因为将来我打算在word文档中放一个按钮,按下该按钮时应该从指定的excel中拉入表格。我的代码如下:
Sub GetTable()
Dim oXL As Excel.Application
Dim wb1 As Excel.Workbook
Dim WorkbookToWorkOn As String
Dim ExcelWasNotRunning As Boolean
On Error Resume Next
Set oXL = GetObject(, "Excel.Application")
If Err Then
ExcelWasNotRunning = True
Set oXL = New Excel.Application
End If
Set wb1 = oXL.Workbooks.Open("*.xlsx")
oXL.Visible = True
wb1.Sheets("**").Range("N3:AB49").Copy
Selection.Collapse Direction:=wdCollapseStart
'Selection.Paste
Selection.PasteExcelTable False, False, False
oXL.Quit
Set oXL = Nothing
End Sub
代码只粘贴我最近复制的内容,而不是表格中的数据
答案 0 :(得分:0)
Set objExcel = CreateObject("Excel.Application")
'Change to your Excel file path
Set wb1 = objExcel.workbooks.Open("C:\Users\Lee Li Fong\Desktop\Website\Spreadsheets For Sales\Copy Excel Table to Word\template Specification.xlsm")
objExcel.Visible = True
wb1.sheets("Specification").Range("A6:F12").Copy 'Change to your sheet name & range
Selection.Collapse direction:=wdCollapseStart
Selection.Paste
objExcel.Quit
Set objExcel = Nothing