简体   繁体   中英

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). 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).

Sorry if it is one simple question, but I am begginer in VBA developing and my current knowledge is limited to Excel actions only.

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. And here my question raises - how to continue saying that I want to spread the data to specific table rows in that word file. I have not worked with the integrated VBA options for Word. 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. Therefore you need to have access to the MS Word DLL. From Excel's VBE select Tools -> References and enable "Microsoft Word xx.x Object Library". Now you can declare both Word and Excel objects. Excel will presume Excel by default. Therefore mention Word in the declaration if you mean Word.

The first objects you declare is a Word application, and a document to run in that application. 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. 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.

Presuming that you have a table in your Word document you might now start writing the data from the array to the table. 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. 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. 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.

Anyway, I hope that gets you started in the right direction.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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