简体   繁体   English

使用Word VBA从Word中的Excel文件复制数据

[英]Copying Data from an Excel File in Word using Word VBA

有人可以用简单的代码来帮助您使用Word VBA将数据从Excel文件导入到Word文档中吗?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM