+ Reply to Thread
Results 1 to 2 of 2

Moving a chart from Excel to Word

Hybrid View

  1. #1
    Registered User
    Join Date
    06-25-2010
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2003
    Posts
    34

    Question Moving a chart from Excel to Word

    I have the following code so far:

    'Open Word
            Dim wrdApp As word.Application
            Dim wrdDoc As word.Document
            Set wrdApp = CreateObject("Word.Application")
            wrdApp.Visible = True
            Set wrdDoc = wrdApp.Documents.Add
    'Create charts
            With Sheet1.ChartObjects.Add(Left:=390, Width:=375, Top:=25, Height:=225)
            .chart.SetSourceData Source:=Sheet22.Range("A1:L2")
            .chart.ChartType = xlColumnClustered
            .chart.ChartTitle.Characters.Text = "REVERB1 Downtime " & StartDate & "-" & EndDate
            .chart.SetElement (msoElementPrimaryValueAxisTitleRotated)
            .chart.Axes(xlValue, xlPrimary).AxisTitle.Text = "Total Minutes"
            .chart.Legend.delete
            End With
            
            With Sheet1.ChartObjects.Add(Left:=775, Width:=375, Top:=25, Height:=225)
            .chart.SetSourceData Source:=Sheet22.Range("A3:L4")
            .chart.ChartType = xlColumnClustered
            .chart.ChartTitle.Characters.Text = "REVERB2 Downtime " & StartDate & "-" & EndDate
            .chart.SetElement (msoElementPrimaryValueAxisTitleRotated)
            .chart.Axes(xlValue, xlPrimary).AxisTitle.Text = "Total Minutes"
            .chart.Legend.delete
            End With
            
            With Sheet1.ChartObjects.Add(Left:=390, Width:=375, Top:=260, Height:=225)
            .chart.SetSourceData Source:=Sheet22.Range("A5:L6,A8:L8")
            .chart.ChartType = xlColumnClustered
            .chart.SetElement (msoElementPrimaryValueAxisTitleRotated)
            .chart.Axes(xlValue, xlPrimary).AxisTitle.Text = "Total Minutes"
            .chart.HasTitle = True
            .chart.ChartTitle.Characters.Text = "REVERBs Downtime " & StartDate & "-" & EndDate
            End With
    A new Word file opens great, but what I need to do from here is modify the chart code so that the charts will be created in the word document instead of sheet1.

    This thread is also posted at http://www.mrexcel.com/forum/showthread.php?t=482691 but I have not received any help there yet.

    Any suggestions are greatly appreciated.
    Last edited by nbuk; 07-21-2010 at 11:30 AM.

  2. #2
    Registered User
    Join Date
    06-25-2010
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2003
    Posts
    34

    Re: Moving a chart from Excel to Word

    Solution:

            Dim wrdApp As word.Application
            Dim wrdDoc As word.Document
            Set wrdApp = CreateObject("Word.Application")
            wrdApp.Visible = True
            Set wrdDoc = wrdApp.Documents.Add
            
            With Sheet1.ChartObjects.Add(Left:=390, Width:=375, Top:=25, Height:=225)
            .chart.SetSourceData Source:=Sheet22.Range("A1:L2")
            .chart.ChartType = xlColumnClustered
            .chart.ChartTitle.Characters.Text = "REVERB1 Downtime " & StartDate & "-" & EndDate
            .chart.SetElement (msoElementPrimaryValueAxisTitleRotated)
            .chart.Axes(xlValue, xlPrimary).AxisTitle.Text = "Total Minutes"
            .chart.Legend.Delete
            .chart.CopyPicture Appearance:=xlScreen, Format:=xlPicture
            End With
            
            wrdApp.Selection.PasteSpecial Link:=False, DataType:=wdPasteMetafilePicture, _
            Placement:=wdInLine, DisplayAsIcon:=False
            
            With Sheet1.ChartObjects.Add(Left:=775, Width:=375, Top:=25, Height:=225)
            .chart.SetSourceData Source:=Sheet22.Range("A3:L4")
            .chart.ChartType = xlColumnClustered
            .chart.ChartTitle.Characters.Text = "REVERB2 Downtime " & StartDate & "-" & EndDate
            .chart.SetElement (msoElementPrimaryValueAxisTitleRotated)
            .chart.Axes(xlValue, xlPrimary).AxisTitle.Text = "Total Minutes"
            .chart.Legend.Delete
            .chart.CopyPicture Appearance:=xlScreen, Format:=xlPicture
            End With
            
            wrdApp.Selection.PasteSpecial Link:=False, DataType:=wdPasteMetafilePicture, _
            Placement:=wdInLine, DisplayAsIcon:=False
            
            With Sheet1.ChartObjects.Add(Left:=390, Width:=375, Top:=260, Height:=225)
            .chart.SetSourceData Source:=Sheet22.Range("A5:L6,A8:L8")
            .chart.ChartType = xlColumnClustered
            .chart.SetElement (msoElementPrimaryValueAxisTitleRotated)
            .chart.Axes(xlValue, xlPrimary).AxisTitle.Text = "Total Minutes"
            .chart.HasTitle = True
            .chart.ChartTitle.Characters.Text = "REVERBs Downtime " & StartDate & "-" & EndDate
            .chart.CopyPicture Appearance:=xlScreen, Format:=xlPicture
            End With
            
            wrdApp.Selection.PasteSpecial Link:=False, DataType:=wdPasteMetafilePicture, _
            Placement:=wdInLine, DisplayAsIcon:=False
            
            Set wrdDoc = Nothing
            Set wrdApp = Nothing
            
            'Delete charts
            Dim chtObj As ChartObject
    
            For Each chtObj In Sheet1.ChartObjects
                chtObj.Delete
            Next
    Hope this helps someone.

+ 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