+ Reply to Thread
Results 1 to 4 of 4

Macro for copying from excel and pasting into word file

  1. #1
    Registered User
    Join Date
    10-05-2012
    Location
    Huntington Beach, CA
    MS-Off Ver
    Excel 2007
    Posts
    14

    Macro for copying from excel and pasting into word file

    Can this be done?

    I wrote some code in order to grab some data in an excel spreadsheet (TodaysOrders.xlsm).
    I would like to grab this data and paste it into a Word file (Template.docx) inside of a table, but I don't know if this is possible. I wrote some code that loops and grabs the info in Excel and includes copying the data, but I don't know how to paste into Word.
    Any help/guidance/advice would be much appreciated. I have learned much from people in this forum, and I can't wait to contribute to others as well. Thank you. Here is my code below:

    Please Login or Register  to view this content.
    TodaysOrders.xlsmTemplate.docx
    Last edited by jeffreybrown; 10-19-2012 at 07:41 PM. Reason: Please use code tags...Thanks.

  2. #2
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,532

    Re: Macro for copying from excel and pasting into word file

    First you must reference the Word object library. In the VBA development window, select Tools, References, then look for "Microsoft Word x.0 Object Library" in the list, where x is whatever version of Word is in there. Then you can run the following code:

    Please Login or Register  to view this content.
    This assumes the Word doc is not already open. It will generally paste this at the beginning of the document. You do not say how you want this pasted. My example pastes it just as if you simply did a manual Paste. That is, you will get a Word table that is formatted like the Excel cells. The Word document is left open.

    Also, see my signature for instructions on code tags, which are required by the rules.

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code. Posting code without them makes your code hard to read and difficult to be copied for testing. Highlight your code and click the # at the top of your post window. For more information about these and other tags, found here
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

  3. #3
    Registered User
    Join Date
    10-05-2012
    Location
    Huntington Beach, CA
    MS-Off Ver
    Excel 2007
    Posts
    14

    Re: Macro for copying from excel and pasting into word file

    Hi 6StringJazzer,

    Thanks for your help and the clarification concerning the rules here. I appreciate it.
    I tried running the code you gave me and followed the steps, but I encountered some issues. Instead, I declared the word application as an object, and went from there and it worked well.

    I also wrote some code for a macro in word, and I tried to write some VBA script in Excel so that I can simply 'Call' the Word macro to function, but its proving trickier than I thought.

    Anyhow, thanks for your help

  4. #4
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,532

    Re: Macro for copying from excel and pasting into word file

    Quote Originally Posted by kitcargray View Post
    I also wrote some code for a macro in word, and I tried to write some VBA script in Excel so that I can simply 'Call' the Word macro to function, but its proving trickier than I thought.
    I believe you can do something like this, but you still have to use the code in Excel to open the Word file first, then you can use the Application.Run method to run the macro in the Word file. I have never actually done this but I know you can do it from one Excel file to another so I imagine it could be done from Excel to Word. In your case, since it's just a paste, you wouldn't gain any advantage from doing it like that unless there was some complexity in determining the paste location.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1