+ Reply to Thread
Results 1 to 8 of 8

Copy range from Excel Spreadsheet and paste into saved Word Document as picture

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    03-04-2014
    Location
    Birmingham, England
    MS-Off Ver
    Excel 2019
    Posts
    758

    Copy range from Excel Spreadsheet and paste into saved Word Document as picture

    Hello,

    My current code below, copies data in a specific cell to a bookmark in a saved Word Document. Is there a way I could edit my code to past a Range (i.e. B2:G23) to the bookmark in the word document as a picture?

    Private Sub CommandButton5_Click()
    
    Dim objWord As Object
    Dim ws As Worksheet
    
        Set ws = ThisWorkbook.Sheets("Word Data")
    
        Set objWord = CreateObject("Word.Application")
    
        objWord.Visible = True
    
        objWord.documents.Open "C:\Users\Christopher.Ellis\Desktop\CPA Test\CE1.docx" ' change as required
    
        With objWord.ActiveDocument
            .bookmarks("ContactWOP1").Range.Text = ws.Range("B2").Value
            .bookmarks("CE1").Range.Text = ws.Range("J25").Value
        
        End With
     
        Set objWord = Nothing
    
    
    End Sub
    Any help much appreciated

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

    Re: Copy range from Excel Spreadsheet and paste into saved Word Document as picture

    Hi there,

    See if the following code does what you need:

    
    
    Option Explicit
    
    
    Private Sub CommandButton5_Click()
    
    '   Excel parameters
        Const sRANGE_TO_COPY    As String = "B2:G23"
        Const sSHEET_NAME       As String = "Word Data"
    
    '   Word parameters
        Const sBOOKMARK_NAME    As String = "ContactWOP1"
        Const sTARGET_PATH      As String = "H:\"
        Const sTARGET_NAME      As String = "Doc1.docx"
    
        Dim objWord             As Object
        Dim wks                 As Worksheet
    
        Set wks = ThisWorkbook.Sheets(sSHEET_NAME)
    
            wks.Range(sRANGE_TO_COPY).CopyPicture
    
        Set objWord = CreateObject("Word.Application")
    
            objWord.Visible = True
    
            objWord.documents.Open sTARGET_PATH & sTARGET_NAME
    
            objWord.ActiveDocument.Bookmarks(sBOOKMARK_NAME).Range.Paste
     
        Set objWord = Nothing
        Set wks = Nothing
    
    End Sub
    The highlighted values may be edited to suit your requirements.


    Hope this helps - please let me know how you get on.

    Regards,

    Greg M

  3. #3
    Forum Contributor
    Join Date
    03-04-2014
    Location
    Birmingham, England
    MS-Off Ver
    Excel 2019
    Posts
    758

    Re: Copy range from Excel Spreadsheet and paste into saved Word Document as picture

    Hi Greg,

    Many thanks for that - it works perfectly. Is there a way I could incorporate more ranges into the code, to paste other ranges of data as pictures to specific bookmarks in the document?

  4. #4
    Forum Contributor
    Join Date
    03-04-2014
    Location
    Birmingham, England
    MS-Off Ver
    Excel 2019
    Posts
    758

    Re: Copy range from Excel Spreadsheet and paste into saved Word Document as picture

    Hi Greg,

    Many thanks for that - it works perfectly. Is there a way I could incorporate more ranges into the code, to paste other ranges of data as pictures to specific bookmarks in the document?

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

    Re: Copy range from Excel Spreadsheet and paste into saved Word Document as picture

    Hi again,

    Thanks for your feedback. I think the following (untested) code should do what you need:

    
    
    
    Option Explicit
    
    
    Private Sub CommandButton5_Click()
    
    '   Excel parameters
        Const sSHEET_NAME       As String = "Word Data"
    
    '   Word parameters
        Const sTARGET_PATH      As String = "H:\"
        Const sTARGET_NAME      As String = "Doc1.docx"
    
        Dim sBookmarkName       As String
        Dim sRangeToCopy        As String
        Dim vaDataValues        As Variant
        Dim objWord             As Object
        Dim iRowNo              As Integer
        Dim wks                 As Worksheet
    
        ReDim vaDataValues(1 To 4, 1 To 2)
    
        vaDataValues(1, 1) = "A11:B19"
        vaDataValues(1, 2) = "Bookmark_1"
        vaDataValues(2, 1) = "G22:I35"
        vaDataValues(2, 2) = "Bookmark_2"
        vaDataValues(3, 1) = "M33:P47"
        vaDataValues(3, 2) = "Bookmark_3"
        vaDataValues(4, 1) = "T44:W57"
        vaDataValues(4, 2) = "Bookmark_4"
    
        Set objWord = CreateObject("Word.Application")
    
            objWord.Visible = True
    
            objWord.documents.Open sTARGET_PATH & sTARGET_NAME
    
        Set wks = ThisWorkbook.Sheets(sSHEET_NAME)
    
            For iRowNo = LBound(vaDataValues, 1) To UBound(vaDataValues, 1)
    
                sRangeToCopy = vaDataValues(iRowNo, 1)
                sBookmarkName = vaDataValues(iRowNo, 2)
    
                wks.Range(sRangeToCopy).CopyPicture
    
                objWord.ActiveDocument.Bookmarks(sBookmarkName).Range.Paste
     
            Next iRowNo
     
        Set objWord = Nothing
        Set wks = Nothing
    
    End Sub
    As before, the highlighted values may be changed to suit your requirements.


    Hope this helps - please let me know how you get on.

    Regards,

    Greg M

  6. #6
    Forum Contributor
    Join Date
    03-04-2014
    Location
    Birmingham, England
    MS-Off Ver
    Excel 2019
    Posts
    758

    Re: Copy range from Excel Spreadsheet and paste into saved Word Document as picture

    Thanks Greg, that is excellent. Much appreciated.

    One final thought after testing, is there a way the code could be amended to copy a single cell, or a range to a bookmark in the same word document but where it is pasted as text, as opposed to picture.

    So the code would paste some ranges as pictures and some as text?

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

    Re: Copy range from Excel Spreadsheet and paste into saved Word Document as picture

    Hi again,

    Many thanks for your feedback and also for the Reputation increase - much appreciated!

    I think that something along the following lines should do what you need:

    
    
    Option Explicit
    
    
    Private Sub CommandButton5_Click()
    
    '   Excel parameters
        Const sSHEET_NAME       As String = "Word Data"
    
    '   Word parameters
        Const sTARGET_PATH      As String = "H:\"
        Const sTARGET_NAME      As String = "Doc1.docx"
    
        Dim bCopyAsPicture      As Boolean
        Dim sBookmarkName       As String
        Dim sRangeToCopy        As String
        Dim vaDataValues        As Variant
        Dim objWord             As Object
        Dim iRowNo              As Integer
        Dim wks                 As Worksheet
    
        ReDim vaDataValues(1 To 4, 1 To 3)
    
        vaDataValues(1, 1) = "A11"
        vaDataValues(1, 2) = "Bookmark_1"
        vaDataValues(1, 3) = False              '   Copy as Text
        vaDataValues(2, 1) = "G22:I35"
        vaDataValues(2, 2) = "Bookmark_2"
        vaDataValues(2, 3) = True               '   Copy as Picture
        vaDataValues(3, 1) = "K33:N35"
        vaDataValues(3, 2) = "Bookmark_3"
        vaDataValues(3, 3) = True               '   Copy as Picture
        vaDataValues(4, 1) = "S44"
        vaDataValues(4, 2) = "Bookmark_4"
        vaDataValues(4, 3) = False              '   Copy as Text
    
        Set objWord = CreateObject("Word.Application")
    
            objWord.Visible = True
    
            objWord.documents.Open sTARGET_PATH & sTARGET_NAME
    
        Set wks = ThisWorkbook.Sheets(sSHEET_NAME)
    
            For iRowNo = LBound(vaDataValues, 1) To UBound(vaDataValues, 1)
    
                sRangeToCopy = vaDataValues(iRowNo, 1)
                sBookmarkName = vaDataValues(iRowNo, 2)
                bCopyAsPicture = vaDataValues(iRowNo, 3)
    
                If bCopyAsPicture = True Then
                      wks.Range(sRangeToCopy).CopyPicture
                Else: wks.Range(sRangeToCopy).Copy
                End If
    
                objWord.ActiveDocument.Bookmarks(sBookmarkName).Range.Paste
    
            Next iRowNo
    
        Set objWord = Nothing
        Set wks = Nothing
    
    End Sub

    If you are likely to be dealing with a large number of ranges, it would probably be much easier to enter all of the Range / Bookmark Name / Copy Method information on a worksheet, and to pass all of that information to the above routine in the form of an array.

    Hope this helps - as always, please let me know how you get on.

    Best regards,

    Greg M

  8. #8
    Forum Contributor
    Join Date
    03-04-2014
    Location
    Birmingham, England
    MS-Off Ver
    Excel 2019
    Posts
    758

    Re: Copy range from Excel Spreadsheet and paste into saved Word Document as picture

    Hi Greg - one final thought. Is there a way to define the size of each picture?

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Copy Excel range a number of sheets as pictures to MS Word, each picture on new Word page.
    By IGTsmith in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-14-2018, 09:23 AM
  2. Macro to copy/paste picture of table in Excel worksheet into a Word document
    By grbears in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 09-25-2017, 11:25 AM
  3. Copy comment picture to word document
    By nielsskovjoergensen in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-09-2017, 07:42 AM
  4. Copy and paste entire word document into excel spreadsheet
    By shanqo in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 04-17-2015, 10:22 AM
  5. [SOLVED] Copy excel range and paste special picture (metafile) into existing word dot VBA
    By Markcuss in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 12-06-2012, 08:21 PM
  6. Copy data from Excel and paste it in a table in word document
    By shekhar in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 12-04-2012, 05:28 AM
  7. Replies: 1
    Last Post: 10-01-2012, 11:11 PM

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