Копирование данных из файла Excel в Word с использованием Word VBA

Может кто-нибудь помочь с простым кодом для импорта данных из файла Excel в текстовый документ с помощью Word VBA?

1 ответ

Решение

Смотрите код ниже, это должно быть полезно в большинстве приложений

 Sub InsertEventData()

'inserting just the used data from an excel sheet


Dim ExcelProgram As Object
Dim EventFile As Object
Dim EventData As Object



'You can use someother Code to develop the Excel File Path
SomeExcelEventFile = "C:\Users\User1\Documents\ExcelSheet1.xls"

'Starting Excel
Set ExcelProgram = CreateObject("Excel.Application")
'Not allowing it to be visible
ExcelProgram.Application.Visible = False

'Opening the desired Test Module Specific Event
Set EventFile = ExcelProgram.Application.Workbooks.Open(SomeExcelEventFile)


'Selecting used range in the Event Log from the Excel file
Set EventData = EventFile.ActiveSheet.UsedRange

'If you want to copy a specific range of cells
'Set EventData = EventFile.ActiveSheet.Range("C1:F50")


'Copying Event Log from the opened Excel File
EventData.Copy

'Pasting Event Log into Word Doc
Selection.PasteAndFormat Type:=wdFormatOriginalFormatting


'Clearing the Office Clipboard
    Dim oData   As New DataObject 'object to use the clipboard
    oData.SetText text:=Empty 'Clearing the text
    oData.PutInClipboard 'Putting Empty text in the clipboard to empty it

'Remove alert Messages from the Excel Program when closing
ExcelProgram.DisplayAlerts = False


'Quiting the Excel Application
ExcelProgram.Quit

'clean up Objects for next use
Set ExcelProgram = Nothing
Set EventFile = Nothing



 End Sub
Другие вопросы по тегам