使用Word VBA从Word中的Excel文件复制数据 [英] Copying Data from an Excel File in Word using Word VBA

查看:310
本文介绍了使用Word VBA从Word中的Excel文件复制数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

推荐答案

请参见下面的代码,这在大多数应用程序中应该很有用

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

这篇关于使用Word VBA从Word中的Excel文件复制数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆