Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.43 KB | None | 0 0
  1. Sub Copy_Paste_Report_1_Graph_to_new_word_document()
  2. '
  3. 'Copy/Paste An Excel Chart Into a New Word Document
  4. '(VBE > Tools > References > Microsoft Word 12.0 Object Library)
  5.  
  6. 'Excel Objects
  7. Dim ChartObj As ChartObject
  8.  
  9. 'Word Objects
  10. Dim WordApp As Word.Application
  11. Dim myDoc As Word.Document
  12. Dim WordTable As Word.Table
  13.  
  14. 'Optimize Code
  15. Application.ScreenUpdating = False
  16. Application.EnableEvents = False
  17.  
  18.  
  19.  
  20. 'Copy Chart from Excel
  21. Set ChartObj = Worksheets("External Dashboard").ChartObjects("Chart 1")
  22.  
  23. 'Create an Instance of MS Word
  24. On Error Resume Next
  25.  
  26. 'Is MS Word already opened?
  27. Set WordApp = GetObject(class:="Word.Application")
  28.  
  29. 'Clear the error between errors
  30. Err.Clear
  31.  
  32. 'If MS Word is not already open then open MS Word
  33. If WordApp Is Nothing Then Set WordApp = CreateObject(class:="Word.Application")
  34.  
  35. 'Handle if the Word Application is not found
  36. If Err.Number = 429 Then
  37. MsgBox "Microsoft Word could not be found, aborting."
  38. GoTo EndRoutine
  39. End If
  40.  
  41. On Error GoTo 0
  42.  
  43. 'Make MS Word Visible and Active
  44. WordApp.Visible = True
  45. WordApp.Activate
  46.  
  47. 'Create a New Document
  48. Set myDoc = WordApp.Documents.Add
  49.  
  50. 'Copy Excel Chart
  51. ChartObj.Copy
  52.  
  53. 'Paste Chart into MS Word
  54. myDoc.Paragraphs(1).Range.PasteSpecial Link:=False _
  55.  
  56. EndRoutine:
  57. 'Optimize Code
  58. Application.ScreenUpdating = True
  59. Application.EnableEvents = True
  60.  
  61. 'Clear The Clipboard
  62. Application.CutCopyMode = False
  63. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement