+ Reply to Thread
Results 1 to 3 of 3

VBA auto copy-paste from Excel to Word works but no source formatting!

Hybrid View

  1. #1
    Registered User
    Join Date
    07-20-2015
    Location
    London
    MS-Off Ver
    2013
    Posts
    2

    VBA auto copy-paste from Excel to Word works but no source formatting!

    I found a code on the Internet and I've adapted to my own use to automate copy-paste. Works great except that when I paste the Excel chart to my word report, the colors get changed to destination theme. I need to keep source formatting and as the report is final, I can't change the color scheme either.

    For some reason Selection.PasteSpecial (wdChart) does not work, it's used as a simple paste. I've got hundreds of reports to paste two dozens of graphs to, I would not want to do if manually!

    'You must set a reference to Microsoft Word Object Library from Tools | References
    
    Option Explicit
    
    Sub ExportToWord()
    
    Dim appWrd          As Object
    Dim objDoc          As Object
    Dim FilePath        As String
    Dim FileName        As String
    Dim x               As Long
    Dim LastRow         As Long
    Dim SheetChart      As String
    Dim SheetRange      As String
    Dim BookMarkChart   As String
    Dim BookMarkRange   As String
    Dim Prompt          As String
    Dim Title           As String
        
        'Turn some stuff off while the macro is running
        Application.ScreenUpdating = False
        Application.EnableEvents = False
        Application.DisplayAlerts = False
        
        'Assign the Word file path and name to variables
        FilePath = ThisWorkbook.path
        FileName = "Trust03.docx"
        
        'Determine the last row of data for our loop
        LastRow = Sheets("Summary").Range("A65536").End(xlUp).Row
        
        'Create an instance of Word for us to use
        Set appWrd = CreateObject("Word.Application")
        
        'Open our specified Word file, On Error is used in case the file is not there
        On Error Resume Next
        Set objDoc = appWrd.Documents.Open(FilePath & "\" & FileName)
        On Error GoTo 0
    
        'If the file is not found, we need to end the sub and let the user know
        If objDoc Is Nothing Then
            MsgBox "Unable to find the Word file.", vbCritical, "File Not Found"
            appWrd.Quit
            Set appWrd = Nothing
            Exit Sub
        End If
        
        'Copy/Paste Loop starts here
        For x = 2 To LastRow
            
            'Use the Status Bar to let the user know what the current progress is
            Prompt = "Copying Data: " & x - 1 & " of " & LastRow - 1 & "   (" & _
                Format((x - 1) / (LastRow - 1), "Percent") & ")"
            Application.StatusBar = Prompt
            
            'Assign the worksheet names and bookmark names to a variable
            'Use With to group these lines together
            With ThisWorkbook.Sheets("Summary")
                SheetChart = .Range("A" & x).Text
    
                BookMarkChart = .Range("C" & x).Text
    
            End With
                
    
    
            'Tell Word to goto the bookmark assigned to the variable BookMarkChart
            appWrd.Selection.Goto What:=wdGoToBookmark, Name:=BookMarkChart
            
            'Copy the data from Thisworkbook
            ThisWorkbook.Sheets(SheetChart).ChartObjects(1).Copy
            
            'Paste into Word
            appWrd.Selection.PasteSpecial (wdChart)
        
        Next
    
        'Turn everything back on
        Application.ScreenUpdating = True
        Application.EnableEvents = True
        Application.DisplayAlerts = True
        Application.StatusBar = False
        
        'Let the user know the procedure is now complete
        Prompt = "The procedure is now completed." & vbCrLf & vbCrLf
        Title = "Procedure Completion"
        MsgBox Prompt, vbOKOnly + vbInformation, Title
        
        'Make our Word session visible
        appWrd.Visible = True
        
        'Clean up
        Set appWrd = Nothing
        Set objDoc = Nothing
    
    End Sub

  2. #2
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,653

    Re: VBA auto copy-paste from Excel to Word works but no source formatting!

    An alternative method is to save the chart as an image file and then insert that image in the Word doc.

    Exporting a Chart to a Word Document
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  3. #3
    Registered User
    Join Date
    07-20-2015
    Location
    London
    MS-Off Ver
    2013
    Posts
    2

    Re: VBA auto copy-paste from Excel to Word works but no source formatting!

    Quote Originally Posted by AlphaFrog View Post
    An alternative method is to save the chart as an image file and then insert that image in the Word doc.

    Exporting a Chart to a Word Document
    That works, however the image formatting and quality is not suitable for printing of the Word reports...

+ 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. Copy and paste formatting of table in Word
    By Aaron092 in forum Word Formatting & General
    Replies: 1
    Last Post: 02-02-2015, 07:41 PM
  2. [SOLVED] Copy and Paste values and source formatting
    By danfullwood in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-02-2013, 04:51 AM
  3. [SOLVED] Pasting Excel range to Word doc Keeping source formatting, using VBA
    By staggers47 in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 06-14-2013, 10:46 AM
  4. Replies: 1
    Last Post: 01-16-2013, 05:36 AM
  5. Replies: 1
    Last Post: 10-01-2012, 11:11 PM
  6. How to disable Paste Special->Keep Source Formatting - Excel 2010
    By tromba in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-11-2012, 03:47 AM
  7. Replies: 0
    Last Post: 07-29-2011, 07:19 AM

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