Results 1 to 16 of 16

Thread: Pushing Charts and Ranges from Excel to Word

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #6
    Site Admin
    Jedi Master
    VBAX Guru Jacob Hilderbrand's Avatar
    Joined
    Jun 2004
    Location
    Roseville, CA
    Posts
    3,712
    Location
    How much do you love me?

    Check this out.

    Make sure to set a reference to MS Word Object Library.

    Option Explicit
     
    Private 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
        FilePath = ThisWorkbook.Path
        FileName = "WorkWithExcel.doc"
        LastRow = Sheets("Summary").Range("A65536").End(xlUp).Row
        Set appWrd = CreateObject("Word.Application")
        appWrd.Visible = True
        On Error Resume Next
        Set objDoc = appWrd.Documents.Open(FilePath & "\" & FileName)
        On Error GoTo 0
        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
        For x = 2 To LastRow
            SheetChart = ThisWorkbook.Sheets("Summary").Range("A" & x).Text
            SheetRange = ThisWorkbook.Sheets("Summary").Range("B" & x).Text
            BookMarkChart = ThisWorkbook.Sheets("Summary").Range("C" & x).Text
            BookMarkRange = ThisWorkbook.Sheets("Summary").Range("D" & x).Text
            appWrd.Selection.Goto What:=wdGoToBookmark, Name:=BookMarkRange
            ThisWorkbook.Sheets(SheetRange).UsedRange.Copy
            appWrd.Selection.Paste
            appWrd.Selection.Goto What:=wdGoToBookmark, Name:=BookMarkChart
            ThisWorkbook.Sheets(SheetChart).ChartObjects(1).Copy
            appWrd.Selection.Paste
        Next
    End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •