Placing chart at bookmark location using VBA

D

dave.cuthill

I am using the method as discussed at http://msdn.microsoft.com/en-us/library/ff629397.aspx
to create a chart but the chart I create ends up being placed at the
top of the document rather than at a bookmark location. Could someone
suggest a method of relocating the chart to a bookmark located a few
pages into the document. I tried copying the chart and the pasting it
in the location I want but that does not work.
 
D

Doug Robbins - Word MVP

It would help to see the code that you are using.

The code shown below, when run from Word when a document, containing a
bookmark named chart at the location where you want the chart to appear, is
the active document, uses late binding to access an Excel Workbook that
contains a sheet with the name "Chart" that contains the chart. It then
copies the chart and pastes it into the range of the bookmark in the Word
document. In this case, the bookmark in the Word document is created by
selecting a cell of a table that has its dimensions set to the size that you
want the chart to appear, and then assigning the bookmark to the cell that
is selected.

For more information on this, see the Chart Merge section of the following
page of fellow MVP Graham Mayor's website:

http://www.gmayor.com/ManyToOne.htm

The following code is a stripped down version of the code in that add-in
that you can download from that site.

Dim xlapp As Object
Dim xlbook As Object
Dim xlsheet As Object
Dim xlchart As Object
Dim Excelwasnotrunning As Boolean
On Error Resume Next
Set xlapp = GetObject(, "Excel.Application")
If Err Then
Excelwasnotrunning = True
Set xlapp = CreateObject("Excel.Application")
End If
On Error GoTo ErrMsg
With xlapp
Set xlbook = .Workbooks.Open("Drive:\Path\Filename)
Set xlsheet = xlbook.Worksheets("Chart")
Set xlchart = xlsheet.ChartObjects(1)
End With
xlchart.CopyPicture Appearance:=xlScreen, Format:=xlPicture
ActiveDocument.Bookmarks("Chart").Range.Paste
If Excelwasnotrunning = True Then
xlapp.Quit
End If
Set xlapp = Nothing
Set xlsheet = Nothing
Set xlchart = Nothing
ErrMsg:
If Err.Number > 0 Then
MsgBox Err.Number & vbCr & Err.Description
Exit Sub
End If
End Sub
 
D

dave.cuthill

Here is what I am using currently based on the reference I mention in
my original posting.

Sub AddBarChart()
Dim jointChart As Chart
Dim chartWorkSheet As Excel.Worksheet

Set jointChart = ActiveDocument.Shapes.AddChart.Chart
Set chartWorkSheet = jointChart.ChartData.Workbook.Worksheets(1)

Set oWordDoc = ActiveDocument

Set rng = ActiveDocument.Tables(5).Range
rng.Collapse wdCollapseEnd


rng.InsertAfter "" & vbCrLf

rng.Collapse wdCollapseEnd
rng.InsertBreak Type:=wdPageBreak
BMName = "BarChart5"
ActiveDocument.Bookmarks.Add Name:=BMName, Range:=rng


MaxRows = ActiveDocument.Tables(5).Rows.Count
chartWorkSheet.ListObjects("Table1").Resize
chartWorkSheet.Range("A1:F" & MaxRows)
chartWorkSheet.Range("Table1[[#Headers],[Series 1]]").FormulaR1C1
= "Percent Loss"
For j = 1 To 6

For k = 1 To MaxRows
RMov = Len(ActiveDocument.Tables(5).Cell(k, j).Range.text) - 2
chartWorkSheet.Cells(k, j).FormulaR1C1 =
Left(ActiveDocument.Tables(5).Cell(k, j).Range.text, RMov)

Next k
Next j

jointChart.SeriesCollection(5).Delete
jointChart.SeriesCollection(1).Delete
jointChart.SeriesCollection(1).Delete
jointChart.SeriesCollection(1).Delete

With jointChart
.ChartType = xlBarClustered
.HasLegend = False
.ChartArea.Interior.Color = RGB(240, 246, 240)
.ChartArea.Border.Color = RGB(182, 17, 49)
.PlotArea.Interior.Color = RGB(255, 255, 255)
.PlotArea.Border.Color = RGB(0, 0, 0)
.SeriesCollection(1).Interior.Color = RGB(182, 17, 49)

With .Axes(xlValue)
.HasTitle = True
.AxisTitle.text = "Percentage Loss (%)"
.MaximumScale = 100
.HasMajorGridlines = False
.HasMinorGridlines = False

End With

With .Axes(xlCategory)
.HasTitle = True
.AxisTitle.text = "Joint Number"
.HasMajorGridlines = False
.HasMinorGridlines = False
.ReversePlotOrder = True
.Crosses = xlMaximum
End With

End With
jointChart.ChartData.Workbook.Application.Quit
With jointChart.Parent
.Height = InchesToPoints(8.75)
.Width = InchesToPoints(6.5)
End With


End Sub



It would help to see the code that you are using.

The code shown below, when run from Word when a document, containing a
bookmark named chart at the location where you want the chart to appear, is
the active document, uses late binding to access an Excel Workbook that
contains a sheet with the name "Chart" that contains the chart.  It then
copies the chart and pastes it into the range of the bookmark in the Word
document.  In this case, the bookmark in the Word document is created by
selecting a cell of a table that has its dimensions set to the size that you
want the chart to appear, and then assigning the bookmark to the cell that
is selected.

For more information on this, see the Chart Merge section of the following
page of fellow MVP Graham Mayor's website:

http://www.gmayor.com/ManyToOne.htm

The following code is a stripped down version of the code in that add-in
that you can download from that site.

Dim xlapp As Object
Dim xlbook As Object
Dim xlsheet As Object
Dim xlchart As Object
Dim Excelwasnotrunning As Boolean
On Error Resume Next
Set xlapp = GetObject(, "Excel.Application")
If Err Then
    Excelwasnotrunning = True
    Set xlapp = CreateObject("Excel.Application")
End If
On Error GoTo ErrMsg
With xlapp
    Set xlbook = .Workbooks.Open("Drive:\Path\Filename)
    Set xlsheet = xlbook.Worksheets("Chart")
    Set xlchart = xlsheet.ChartObjects(1)
End With
xlchart.CopyPicture Appearance:=xlScreen, Format:=xlPicture
ActiveDocument.Bookmarks("Chart").Range.Paste
If Excelwasnotrunning = True Then
    xlapp.Quit
End If
Set xlapp = Nothing
Set xlsheet = Nothing
Set xlchart = Nothing
ErrMsg:
    If Err.Number > 0 Then
        MsgBox Err.Number & vbCr & Err.Description
        Exit Sub
    End If
End Sub

--
Hope this helps,

Doug Robbins - Word MVP
dkr[atsymbol]mvps[dot]org




I am using the method as discussed at
http://msdn.microsoft.com/en-us/library/ff629397.aspx
to create a chart but the chart I create ends up being placed at the
top of the document rather than at a bookmark location. Could someone
suggest a method of relocating the chart to a bookmark located a few
pages into the document. I tried copying the chart and the pasting it
in the location I want but that does not work.- Hide quoted text -

- Show quoted text -
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top