Problem pasting charts to Word

sharky12345

Well-known Member
Joined
Aug 5, 2010
Messages
3,388
Office Version
  1. 2016
Platform
  1. Windows
I'm using the following to copy charts and a small range to a Word Template but I am having a couple of issues.

The first one is that I get 'Type Mismatch' twice while it does the pasting and just before it saves the Word document.

The second is that the Charts are not pasting correctly - I have space for 4 but only want 2 Charts pasted, 1 on each page before the range is pasted.

Having space for 4 Charts causes them to be reduced in size when they are pasted. I'm no familiar enough with this code to know where the problem is if anyone can point me in the right direction please?

Code:
Dim oWordDoc As Object, oWord As Object, nt%
Option Explicit
Sub E_W()
 Dim strDate As String
 Dim DirName As String
 
  
 strDate = Format((Date), "ddmmyyyy")
    
    PasteCharts
    DataPaste
    oWordDoc.SaveAs Filename:=ThisWorkbook.Path & "\Data Report - created " & strDate & ".doc", FileFormat:= _
    wdFormatDocument, LockComments:=False
    Set oWordDoc = Nothing
    Set oWord = Nothing
    
    
    
End Sub
Sub PasteCharts()
    Dim sFile$, i%, j%, counter%, mt As Table, wr As Word.Range
    Dim tablen%, nc%, LinNum%, au%
    
    sFile = ThisWorkbook.Path & "\Template.dot"
    On Error Resume Next
    Set oWord = GetObject(, "Word.Application")
    If Err <> 0 Then Set oWord = CreateObject("Word.Application")
    Err.Clear
    On Error GoTo Err_Handler
    nc = Sheets("Charts").ChartObjects.Count
    Set oWordDoc = oWord.Documents.Open(sFile)
    oWord.Visible = True
    oWordDoc.Activate
    If oWordDoc.Tables.Count > 0 Then
        Do
            au = oWordDoc.Tables.Count
            oWordDoc.Tables(au).Delete
        Loop Until au = 1
    End If
    If nc Mod 1 = 0 Then
        nt = nc / 1             ' how many tables are needed
    Else                        ' four charts per page
        nt = (nc \ 1) + 1
    End If
    
    oWord.Selection.EndKey Unit:=wdStory
    Set wr = oWord.Selection.Range
    LinNum = wr.Information(wdFirstCharacterLineNumber)
    If LinNum > 2 Then
        Do              ' eliminate extra lines at the beginning
            wr.Delete
            LinNum = wr.Information(wdFirstCharacterLineNumber)
        Loop Until LinNum = 2
    End If
    For i = 1 To nt
        Set wr = oWordDoc.Range
        With wr
            .Collapse Direction:=wdCollapseEnd
            .InsertParagraphAfter
            .Collapse Direction:=wdCollapseEnd
        End With
        Set mt = oWordDoc.Tables.Add(Range:=wr, NumRows:=2, NumColumns:=2)
    Next
    counter = 1
    tablen = 1
    Do
        For i = 1 To 2
            For j = 1 To 2
                Sheets("Charts").ChartObjects(counter).CopyPicture
                oWordDoc.Tables(tablen).Cell(i, j).Range.Paste
                If counter = nc Then Exit Do
                If counter Mod 1 = 0 Then tablen = tablen + 1
                counter = counter + 1
            Next
        Next
    Loop Until counter = nc + 1
    Exit Sub
Err_Handler:
    MsgBox Err.Description, vbCritical, Err.Number
    Resume Next
End Sub
    
Sub DataPaste()
       
    Dim t As Word.Range, pn%, cn%
    
    Data2Word
    oWordDoc.Bookmarks("\EndofDoc").Select
    pn = oWord.Selection.Information(wdActiveEndPageNumber)
    Do
        oWord.Selection.TypeParagraph
        cn = oWord.Selection.Information(wdActiveEndPageNumber)
    Loop Until cn = pn + 1          ' goes to a new page
    Set t = oWord.Selection.Range
    t.Paste
    With oWordDoc.Tables(nt + 1)
        .PreferredWidthType = wdPreferredWidthPercent
        .PreferredWidth = 100
        .Borders(wdBorderTop).LineStyle = wdLineStyleSingle
        .Borders(wdBorderLeft).LineStyle = wdLineStyleSingle
        .Borders(wdBorderBottom).LineStyle = wdLineStyleSingle
        .Borders(wdBorderRight).LineStyle = wdLineStyleSingle
        .Borders(wdBorderHorizontal).LineStyle = wdLineStyleSingle
        .Borders(wdBorderVertical).LineStyle = wdLineStyleSingle
        .AutoFitBehavior (wdAutoFitWindow)
    End With
    Sheet34.Visible = xlSheetHidden
End Sub
Sub Data2Word()
    Dim d As Worksheet
 
    Set d = Worksheets("Temp")
    Sheet34.Activate
    
    Range("B8:O27").Select
    Selection.Copy
        
End Sub
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Firstly, I know sod-all about Word, but from your "..Charts pasted, 1 on each page before the range is pasted" I've had a go:
Code:
Sub E_W()
Dim strDate As String
Dim DirName As String
strDate = Format((Date), "ddmmyyyy")
PasteCharts2
DataPaste
oWordDoc.SaveAs filename:=ThisWorkbook.Path & "\Data Report - created " & strDate & ".doc", FileFormat:=wdFormatDocument, LockComments:=False
Set oWordDoc = Nothing
Set oWord = Nothing
End Sub

Sub DataPaste()
Dim t As Word.Range, pn%, cn%
Data2Word
oWordDoc.Bookmarks("\EndofDoc").Range.InsertBreak Type:=wdPageBreak
oWordDoc.Bookmarks("\EndofDoc").Select
Set t = oWord.Selection.Range
t.Paste
With oWordDoc.Tables(nt + 1)
  .PreferredWidthType = wdPreferredWidthPercent
  .PreferredWidth = 100
  .Borders(wdBorderTop).LineStyle = wdLineStyleSingle
  .Borders(wdBorderLeft).LineStyle = wdLineStyleSingle
  .Borders(wdBorderBottom).LineStyle = wdLineStyleSingle
  .Borders(wdBorderRight).LineStyle = wdLineStyleSingle
  .Borders(wdBorderHorizontal).LineStyle = wdLineStyleSingle
  .Borders(wdBorderVertical).LineStyle = wdLineStyleSingle
  .AutoFitBehavior (wdAutoFitWindow)
End With
Sheet34.Visible = xlSheetHidden
End Sub

Sub PasteCharts2()
Dim sFile$, i%, j%, counter%, mt As Table, wr As Word.Range
Dim tablen%, nc%, LinNum%, au%
sFile = ThisWorkbook.Path & "\Template.dot"
On Error Resume Next
Set oWord = GetObject(, "Word.Application")
If Err <> 0 Then Set oWord = CreateObject("Word.Application")
Err.Clear
On Error GoTo Err_Handler
nc = Sheets("Charts").ChartObjects.Count
Set oWordDoc = oWord.Documents.Open(sFile)
oWord.Visible = True
oWordDoc.Activate
If oWordDoc.Tables.Count > 0 Then
  Do
    au = oWordDoc.Tables.Count
    oWordDoc.Tables(au).Delete
  Loop Until au = 1
End If
nt = nc            ' how many tables are needed
oWord.Selection.EndKey Unit:=wdStory
Set wr = oWord.Selection.Range
LinNum = wr.Information(wdFirstCharacterLineNumber)
If LinNum > 2 Then
  Do              ' eliminate extra lines at the beginning
    wr.Delete
    LinNum = wr.Information(wdFirstCharacterLineNumber)
  Loop Until LinNum = 2
End If
counter = 1
Do
  Set mt = oWordDoc.Tables.Add(Range:=wr, NumRows:=1, NumColumns:=1)
  Sheets("Charts").ChartObjects(counter).CopyPicture
  mt.Cell(1, 1).Range.Paste
  If counter = nc Then Exit Do
  Set wr = oWordDoc.Range
  With wr
    .Collapse Direction:=wdCollapseEnd
    .InsertBreak Type:=wdPageBreak
    .Collapse Direction:=wdCollapseEnd
  End With
  counter = counter + 1
Loop Until counter = nc + 1
Exit Sub
Err_Handler:
MsgBox Err.Description, vbCritical, Err.Number
Resume Next
End Sub
The size of the charts seem to be related to their original size on the Excel sheet. The code will paste as many charts as there are charts on the Charts sheet, not just restricted to 2.
 
Upvote 0
It's done what I needed so thank you - it has been a real pain to solve this one!
 
Upvote 0

Forum statistics

Threads
1,203,140
Messages
6,053,727
Members
444,681
Latest member
Nadzri Hassan

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top