+ Reply to Thread
Results 1 to 2 of 2

VBA - Copy Excel charts to Word

  1. #1
    Registered User
    Join Date
    02-09-2016
    Location
    Australia
    MS-Off Ver
    2010
    Posts
    4

    VBA - Copy Excel charts to Word

    Hi,

    I would like to copy lot of charts from my excel to a Word document in order to create a report.

    See below the code that I use. I have the error message: Application-defined or object-defined error and it put in yellow the line: 'ThisWorkbook.Sheets(SheetChart).ChartObjects(1).Copy'

    My Summary table is

    A B
    Chart Sheet Name Bookmark Name (Chart)
    Top_10_Micro_MEL ChartMEL101
    Top_10_Micro_MEL ChartMEL102
    Top_10_Micro_MEL ChartMEL103
    Top_10_Micro_MEL ChartMEL104
    Top_10_Micro_MEL ChartMEL105
    Top_10_Micro_MEL ChartMEL106
    Top_10_Micro_MEL ChartMEL107
    Top_10_Micro_MEL ChartMEL108
    Top_10_Micro_MEL ChartMEL109
    Top_10_Micro_MEL ChartMEL110



    '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 = "Analysis_Trend_Volume_Report_March_2016_v1.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("B" & x).Text
    End With

    'Tell Word to goto the bookmark assigned to the variable BookMarkChart
    appWrd.Selection.Goto What:=-1, Name:=BookMarkChart

    'Copy the data from Thisworkbook
    ThisWorkbook.Sheets(SheetChart).ChartObjects(1).Copy

    'Paste into Word
    appWrd.Selection.Paste
    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 & "www.VBAExpress.com"
    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 Moderator zbor's Avatar
    Join Date
    02-10-2009
    Location
    Croatia
    MS-Off Ver
    365 ProPlus
    Posts
    15,627

    Re: VBA - Copy Excel charts to Word

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code.

    Posting code between [CODE]Please [url=https://www.excelforum.com/login.php]Login or Register [/url] to view this content.[/CODE] tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    Highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here



    (This thread should receive no further responses until this moderation request is fulfilled, as per Forum Rule 7)

+ 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. Replies: 2
    Last Post: 09-15-2015, 04:12 AM
  2. copying multiple charts from excel to word overwriting previous charts
    By eric45 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 08-01-2013, 11:11 AM
  3. excel charts too big in word
    By joeb33050 in forum Excel Charting & Pivots
    Replies: 1
    Last Post: 06-11-2013, 09:49 AM
  4. Copy charts from excel to word - specific tables
    By h0ps in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-21-2012, 05:32 AM
  5. Copy charts to Word
    By walrasian in forum Excel Charting & Pivots
    Replies: 3
    Last Post: 09-03-2010, 02:26 PM
  6. Copy/paste charts into word
    By Builderbob in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-19-2007, 12:00 AM
  7. Excel Charts in Word
    By dwstrick in forum Excel General
    Replies: 0
    Last Post: 10-07-2006, 12:02 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