1

Can someone please help with a simple code to import data from an Excel file into a word document using Word VBA?

1 Answer 1

3

See Code below, this should be useful in most applications

 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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.