简体   繁体   English

VBA复制选定的单元格与数据(Excel)。 将它们粘贴到表中的特定行(Word)

[英]VBA Copy selected cells with data (Excel). Paste them in specific row in a table (Word)

I have the following case: 我有以下情况:

In Excel Spreadsheet, There is some data (names, email, phone number, ID number, etc.) The same data should be populated to a given Word form (it exists protection on the word file which allows several rows from a table to be filled in). 在Excel Spreadsheet中,有一些数据(名称,电子邮件,电话号码,ID号等)。应将相同的数据填充到给定的Word表单中(在word文件上存在保护,该保护允许从表中复制几行内容)填写)。 I would like copy the existing data from the Excel file and paste it into the Word file on the proper place (I meant cell with ID number to be copied and to be pasted into row with title ID number, for example). 我想从Excel文件中复制现有数据,然后将其粘贴到Word文件中的适当位置(例如,我要复制具有ID号的单元格并将其粘贴到具有标题ID号的行中)。

Sorry if it is one simple question, but I am begginer in VBA developing and my current knowledge is limited to Excel actions only. 抱歉,如果这是一个简单的问题,但是我是VBA开发的初学者,我目前的知识仅限于Excel操作。

Thank you in advance! 先感谢您!

I am starting with: 我开始:

'Copy 
Set objSelection = Selection 
Selection.Copy 
'Paste the copied 
Set objTempWorkbook = Excel.Application.Workbooks.Add(1) 
Set objTempWorksheet = objTempWorkbook.Sheets(1) 
'Save the temp HTML 
Set objFileSystem = CreateObject("Scripting.FileSystemObject") 
strTempHTMLFile = objFileSystem.GetSpecialFolder(2).Path & "\Temp for Excel" & Format(Now, "YYYY-MM-DD hh-mm-ss") & ".htm" 
Set objTempHTMLFile = objTempWorkbook.PublishObjects.Add(xlSourceRange, strTempHTMLFile, objTempWorksheet.Name, objTempWorksheet.UsedRange.Address) 
objTempHTMLFile.Publish (True)

So, with this code, I am selecting the needed excel cells with data, copy them, paste them in temporary HTML file. 因此,使用此代码,我将选择所需的Excel单元格以及数据,将其复制,然后将其粘贴到临时HTML文件中。 And here my question raises - how to continue saying that I want to spread the data to specific table rows in that word file. 在这里,我的问题提出了—如何继续说我想将数据分布到该Word文件中的特定表行。 I have not worked with the integrated VBA options for Word. 我没有使用Word的集成VBA选项。 Could you please assist in? 你能帮忙吗?

Your start isn't the best one possible. 您的开始并不是最好的开始。 Please try to continue on the path set by the following code. 请尝试继续执行以下代码设置的路径。

Sub VSE()

    ' Word declarations:
    Dim WdApp As Word.Application
    Dim Doc As Word.Document

    ' Excel declartions:
    Dim Fn As String
    Dim RngS As Range                           ' Source
    Dim Arr As Variant

    ' in future you may like to specify the range more elegantly.
    ' So, declare a range which you can later specify in any way you like:
    Set RngS = Selection
    Arr = RngS.Value

    Fn = "C:\My Documents\TestDoc.docx"
    Set WdApp = New Word.Application
    Set Doc = WdApp.Documents.Open(Fn)
End Sub

The idea is that you start in Excel, work in Excel, and let Excel do some fidgeting in Word. 这个想法是,您从Excel开始,在Excel中工作,然后让Excel在Word中做些烦躁。 Therefore you need to have access to the MS Word DLL. 因此,您需要有权访问MS Word DLL。 From Excel's VBE select Tools -> References and enable "Microsoft Word xx.x Object Library". 从Excel的VBE中选择“工具”->“引用”,然后启用“ Microsoft Word xx.x对象库”。 Now you can declare both Word and Excel objects. 现在,您可以声明Word和Excel对象。 Excel will presume Excel by default. Excel将默认使用Excel。 Therefore mention Word in the declaration if you mean Word. 因此,如果您是说Word ,请在声明中提及Word。

The first objects you declare is a Word application, and a document to run in that application. 您声明的第一个对象是Word应用程序,以及在该应用程序中运行的文档。 Then you need a file name for the document you wish to open. 然后,您需要打开文档的文件名。 After you have the file name you can open the document (and perhaps make it be there, invisible in the background without ever showing its face, except for now you probably want to see it). 获得文件名后,您可以打开文档(并且可以将其放置在其中,在后台不可见而不显示其外观,但现在您可能想要查看它)。

You need to get your data out of Excel in a transportable format, meaning an array. 您需要以可移植的格式(即数组)从Excel中获取数据。 So, you declare the Excel range from which you want to copy and write the value of that range to the array you have declared. 因此,您声明要复制的Excel范围并将该范围的值写入已声明的数组。

Presuming that you have a table in your Word document you might now start writing the data from the array to the table. 假定您的Word文档中有一个表,现在可以开始将数据从数组写入表中。 Not clear if you have the table already or just want to start making it at this point. 目前尚不清楚该表是否已经存在,或者只想开始制作它。

An alternative would be to copy the Excel selection and paste it into the Word document as a table. 一种替代方法是复制Excel选择并将其作为表格粘贴到Word文档中。 The infrastructure for all these actions would be the same, ie the one I sketched for you above, except that you wouldn't need the array if you want to paste the table. 所有这些操作的基础结构都是相同的,即上面我为您绘制的基础结构,不同之处在于,如果要粘贴表,则不需要数组。 But let me warn you that pasting Excel tables to Word gives a lot of headache transferring what is almost incompatible formats. 但是,让我警告您,将Excel表粘贴到Word会使几乎不兼容的格式传输变得很头疼。 Pasting an Excel table into Word either requires you to be very expert at table formatting or insensitive to the finer points of document appearance. 将Excel表格粘贴到Word中要么要求您在表格格式方面非常熟练,要么对文档外观的细节不敏感。

Anyway, I hope that gets you started in the right direction. 无论如何,我希望您能朝正确的方向入手。

暂无
暂无

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

相关问题 Excel/VBA - 根据日期将数据复制并粘贴到特定行中的工作表中 - Excel/VBA - Copy and paste data into a worksheet in a specific row based on date VBA将Excel表复制并粘贴到Word文档中 - VBA Copy and Paste Excel Table into Word Document Excel VBA从选定的行复制特定的单元格并将指定的列粘贴到其他工作簿 - Excel VBA copy specific cells from selected rows and paste specified column other workbook 使用Excel VBA将.msg文件中的粘贴表数据复制到Excel工作表中的单独单元格中 - Copy Paste table data from a .msg file into separate cells in an Excel worksheet using Excel VBA Excel VBA复制并粘贴单词中的选定文本 - Excel VBA copy and paste selected text from word 查找最后一行>合并单元格>复制并粘贴到其中 - Excel VBA宏 - Find last row > merge cells > copy and paste into it - Excel VBA macros VBA Excel基于IF语句自动复制和粘贴特定单元格 - VBA Excel Automatically Copy & Paste Specific cells based on IF statement 将单元格/表格从 Excel 复制并粘贴到 Word 模板(并粘贴到正确位置) - Copy and paste Cells/Table from Excel to Word Template (and to the Correct Position) Excel VBA-如何在空白单元格之后复制和粘贴数据组? - Excel VBA - How to copy and paste groups of data following blank cells? 根据行数据复制特定的单元格并粘贴在特定的工作表上 - Copy specific cells according to row data and paste on specific sheet
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM