我試圖使用Excel工作表中的信息自動填充Word文檔。我希望用戶能夠選擇所需的excel文件,並希望我的宏從excel文件的不同頁面複製特定範圍,並將其粘貼到word中的特定區域。到目前爲止,我可以打開我選擇的excel文件,但不知道如何選擇範圍並粘貼到特定位置。此外,我正在使用單詞VBA做所有這些工作,並且還希望使用單詞VBA複製粘貼excel範圍。我試圖從名爲Main的表格複製單元格C25,並從名爲summary的表格中將表格A5複製到C28。單元格C25是一個名字,所以我想把它放在一個單詞文檔中,在行後請註明親愛的名字和張貼表,請看下面的摘要:。這是我迄今爲止所能夠選擇的excel文件並打開它。如何從工作表中複製excel範圍,並將其放入特定位置,然後將其放入使用word vba的特定位置中的文字
Sub Macro1_FileOpen()
'
' Macro1_FileOpen Macro
'
'
'Dim FilePath As Object
'Dim dSource As String
'With Me.Application.FileDialog(Microsoft.Office.Core.MsoFileDialogType.msoFileDialogOpen)
' .AllowMultiSelect = True
'.Filters.Clear
'.Filters.Add "Excel Files", "*.xls;*.xlw"
' .Filters.Add "All Files", "*.*"
' If .Show = True Then
' .Execute()
'End If
'End With
Dim xlApp As Object
Dim xlBook As Object
Dim dSource As String
Dim ExcelWasNotRunning As Boolean
Dim MyDOc As Word.Document
Dim fd As FileDialog
Set fd = Application.FileDialog(msoFileDialogFilePicker)
With fd
.Title = "Select the file that you wish to use."
.InitialFileName = strFolder
.Filters.Clear
.Filters.Add "Excel Files", "*.xls?"
.AllowMultiSelect = False
If .Show = -1 Then
dSource = .SelectedItems(1)
Else
MsgBox "You cancelled the operation"
Exit Sub
End If
End With
On Error Resume Next
Set xlApp = GetObject(, "Excel.Application")
If Err Then
ExcelWasNotRunning = True
Set xlApp = CreateObject("Excel.Application")
End If
On Error GoTo 0
With xlApp
Set xlBook = .Workbooks.Open(dSource)
MsgBox "The workbook" & vbCr & dSource & vbCr & "is open!"
'Insert code to do what you need to do with the workbook here
Set xlBook = Nothing
End With
If ExcelWasNotRunning Then
xlApp.Quit
End If
Set xlBook = Nothing
Set xlApp = Nothing
Set fd = Nothing
End Sub