+ Reply to Thread
Results 1 to 5 of 5

Macro to copy worksheet into word

  1. #1
    Registered User
    Join Date
    11-12-2007
    Posts
    30

    Macro to copy worksheet into word

    Hi all,
    I need a macro that will copy a worksheet into a new word document. I don't want the data to be linked, I just want it to paste the values, like when you do Paste Special> Values. There is a specify range of data I want to copy- Cells B2:H80.

    I've created the following code from stuff I found on the internet and by adjusting it a bit. I'm not a programmer, so this is a shot in the dark.

    Sub OpenSpecificDoc()
    Dim wrdApp As Word.Application

    Set wrdApp = CreateObject("word.Application")

    wrdApp.Documents.Add



    wrdApp.Visible = True


    Range("b2:h88").Select
    Selection.Copy

    wrdApp.Selection.PasteSpecial Link:=False, DataType:=wdPasteMetafilePicture, _
    Placement:=wdInLine, DisplayAsIcon:=False


    End Sub
    The problem with this is that it doesn't paste the entire selection, can anyone explain why it doesn't and how I can remedy this?
    Thank you,
    Pete
    Last edited by cannon_lab; 05-21-2008 at 07:49 AM.

  2. #2
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,641
    Hi Pete,

    Copy the following into a standard VBA code module in the workbook from which you want to copy data.

    Please Login or Register  to view this content.
    You can alter the value of the constant strRangeToCopy to suit the range you wish to copy.

    This routine will use an existing instance of Word if one exists, or will open a new one if Word is not already running.

    Hope the above helps. Please let me know how you get on.

    Reards,

    Greg M

  3. #3
    Registered User
    Join Date
    11-12-2007
    Posts
    30

    Ongoing Issues

    Thanks Greg, that works well, but I was hoping for more of a fix on what I had out together.

    The code I wrote made is more like a picture being pasted onto the word document, which is what I required. The code that you have written is more like a copy and paste, which means that the range selected will go over two pages. Also, I can set the page set up in Excel for it to fit on one page, but using your code the far right hand side is cut off- it falls off the end and needs resizing.

    Essentially I need to why the code I wrote didn't copy the entire range.

    Alternatively, how can edit the code you have written to make the range fit onto one page?

    Any help?

    Thanks,
    Pete

  4. #4
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,641
    Hi again Pete,

    Here's a cheap & cheerful way of accommodating the table on a single sheet. The macro routine adjusts the top & bottom margins and the table row height so that all of the information fits on a single sheet.

    Just use the following for the "With appWord / End With" chunk of my previous code:

    Please Login or Register  to view this content.
    I'll see if I can find out why your approach doesn't seem to be working correctly.

    Regards,

    Greg M


    P.S. Oops! I've just realised you want to copy rows 2 to 80, not 20 to 80 as my code does - sorry!
    Last edited by Greg M; 05-21-2008 at 07:44 PM.

  5. #5
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,641
    Hi Pete,

    At the risk of stating the obvious, I think the amount of information you're trying to paste as a metafile is just too much to fit on a single sheet. Almost certainly all of the data are being copied to the clipboard, but when the paste operation is performed the application fits as much of the picture as it can onto the sheet & just forgets about the remainder.

    I think you're going to have to try the "less sophisticated" approach like playing with margins, row heights & font sizes if you want to fit everything on a single sheet.

    Best regards,

    Greg M

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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