Sub CopyToWord()
Dim objWord As New Word.Application
'copying the range that I want to paste in Word
With ThisWorkbook.Worksheets("grid_copy")
.Range("b1:AA42").CopyPicture xlScreen
End With
'pasting the picture in a new Word document
With objWord
.Documents.Add
.Selection.Paste
.Selection.ShapeRange.Height = 651
.Selection.ShapeRange.Width = 500
'Those two lines don't work to resize the picture that i'm pasting in word
.Visible = True
End With
End Sub
The code is actualy working but I'm not capable of applying the resize of the image that I want. Do you guys know a way that I can resize the picture that i'm pasting in Word coming form a range in Excel?
Try:
Sub CopyToWord()
'copying the range that I want to paste in Word
ThisWorkbook.Worksheets("grid_copy").Range("b1:AA42").CopyPicture xlScreen
'pasting the picture in a new Word document
Dim wdApp As New Word.Application, wdDoc As Word.Document, wdImg As Word.InlineShape
With wdApp
.Visible = True
.ScreenUpdating = False
Set wdDoc = .Documents.Add
With wdDoc
.Range.Paste
Set wdImg = .InlineShapes(1)
With wdImg
.Height = 651
.Width = 500
End With
End With
.ScreenUpdating = True
End With
End Sub
Related
I've created a macro to paste an Excel table into word but the macro isn't working, and I can't figure out what I'm doing wrong. See code below for reference.
I've also checked the "Reference" --> "Microsoft Word 16.0 Object Library"
TIA!
'
' CreateLabels Macro
Dim objWord
Dim objDoc
Set objWord = CreateObject("Word.Application")
Set objDoc = objWord.Documents.Open("C:\Users\username\Desktop\New Template.doc")
Dim x As Workbook
'Open Excel and Copy labels
Set x = Workbooks.Open("Excel file path")
With x.Sheets("Receiving Labels")
LastRow = .Range("A" & .Rows.Count).End(xlUp).Row
End With
x.Sheets("Receiving Labels").Range("A1:E" & LastRow).Copy
With objDoc.Paragraphs(objDoc.Paragraphs.Count).Range
'All formatting goes here
.Paste
.Font.Name = "Calibri"
.Font.Color = wdColorBlack
.Font.Bold = False
.Font.Italic = False
.Font.Allcaps = False
.Font.Size = 8
End With
objWord.Visible = True
End Sub
Try this:
Sub ExcelToWord()
'PURPOSE: Copy/Paste An Excel Table Into a New Word Document
'NOTE: Must have Word Object Library Active in Order to Run _
(VBE > Tools > References > Microsoft Word 12.0 Object Library)
'SOURCE: www.TheSpreadsheetGuru.com
Dim tbl As Excel.Range
Dim WordApp As Word.Application
Dim myDoc As Word.Document
Dim WordTable As Word.Table
'Optimize Code
Application.ScreenUpdating = False
Application.EnableEvents = False
'Copy Range from Excel
Set tbl = ThisWorkbook.Worksheets(Sheet1.Name).ListObjects("Table1").Range
'Create an Instance of MS Word
On Error Resume Next
'Is MS Word already opened?
Set WordApp = GetObject(class:="Word.Application")
'Clear the error between errors
Err.Clear
'If MS Word is not already open then open MS Word
If WordApp Is Nothing Then Set WordApp = CreateObject(class:="Word.Application")
'Handle if the Word Application is not found
If Err.Number = 429 Then
MsgBox "Microsoft Word could not be found, aborting."
GoTo EndRoutine
End If
On Error GoTo 0
'Make MS Word Visible and Active
WordApp.Visible = True
WordApp.Activate
'Create a New Document
Set myDoc = WordApp.Documents.Add
'Copy Excel Table Range
tbl.Copy
'Paste Table into MS Word
myDoc.Paragraphs(1).Range.PasteExcelTable _
LinkedToExcel:=False, _
WordFormatting:=False, _
RTF:=False
'Autofit Table so it fits inside Word Document
Set WordTable = myDoc.Tables(1)
WordTable.AutoFitBehavior (wdAutoFitWindow)
EndRoutine:
'Optimize Code
Application.ScreenUpdating = True
Application.EnableEvents = True
'Clear The Clipboard
Application.CutCopyMode = False
End Sub
Sub CopyToWord()
Dim objWord As New Word.Application
'copying the range that I want to paste in Word
With ThisWorkbook.Worksheets("grid_copy")
.Range("b1:AA42").CopyPicture xlScreen
End With
'pasting the picture in a new Word document
With objWord
.Documents.Add
.Selection.Paste
.Selection.ShapeRange.Height = 651
.Selection.ShapeRange.Width = 500
'Those two lines don't work to resize the picture that i'm pasting in word
.Visible = True
End With
End Sub
The code is actualy working but I'm not capable of applying the resize of the image that I want. Do you guys know a way that I can resize the picture that i'm pasting in Word coming form a range in Excel?
Try:
Sub CopyToWord()
'copying the range that I want to paste in Word
ThisWorkbook.Worksheets("grid_copy").Range("b1:AA42").CopyPicture xlScreen
'pasting the picture in a new Word document
Dim wdApp As New Word.Application, wdDoc As Word.Document, wdImg As Word.InlineShape
With wdApp
.Visible = True
.ScreenUpdating = False
Set wdDoc = .Documents.Add
With wdDoc
.Range.Paste
Set wdImg = .InlineShapes(1)
With wdImg
.Height = 651
.Width = 500
End With
End With
.ScreenUpdating = True
End With
End Sub
I am a total VBA novice, but I've managed to cobble together some code which allows me to export an excel 2010 chart into a new Word 2010 document. The only problem I have is that I would like to unlink the chart from excel once it is exported, so that it doesn't change when excel is updated. I've looked everywhere, but nothing seems to work.
The only other code which seems to fit the bill takes the chart and saves it as an image prior to pasting it, but this would not work as I have nowhere to save the image - users in our organisation do not have access to the C:\ drive, and without knowing everyone's user details, I cannot write code which will do the job.
The code I've written so far is this, which does work, but does not unlink:
Sub Copy_Paste_Report_1_Graph_to_new_word_document()
'
'Copy/Paste An Excel Chart Into a New Word Document
'(VBE > Tools > References > Microsoft Word 12.0 Object Library)
'Excel Objects
Dim ChartObj As ChartObject
'Word Objects
Dim WordApp As Word.Application
Dim myDoc As Word.Document
Dim WordTable As Word.Table
'Optimize Code
Application.ScreenUpdating = False
Application.EnableEvents = False
'Copy Chart from Excel
Set ChartObj = Worksheets("External Dashboard").ChartObjects("Chart 1")
'Create an Instance of MS Word
On Error Resume Next
'Is MS Word already opened?
Set WordApp = GetObject(class:="Word.Application")
'Clear the error between errors
Err.Clear
'If MS Word is not already open then open MS Word
If WordApp Is Nothing Then Set WordApp = CreateObject(class:="Word.Application")
'Handle if the Word Application is not found
If Err.Number = 429 Then
MsgBox "Microsoft Word could not be found, aborting."
GoTo EndRoutine
End If
On Error GoTo 0
'Make MS Word Visible and Active
WordApp.Visible = True
WordApp.Activate
'Create a New Document
Set myDoc = WordApp.Documents.Add
'Copy Excel Chart
ChartObj.Copy
'Paste Chart into MS Word
myDoc.Paragraphs(1).Range.PasteSpecial Link:=False _
EndRoutine:
'Optimize Code
Application.ScreenUpdating = True
Application.EnableEvents = True
'Clear The Clipboard
Application.CutCopyMode = False
End Sub
I've probably included more than I need to, but like I said, I'm a novice.
Use .CopyPicture and .PasteSpecial methods:
Sub Copy_Paste_Report_1_Graph_to_new_word_document()
Dim ChartObj As ChartObject
Dim WordApp As Word.Application
Dim myDoc As Word.Document
Set ChartObj = Worksheets("External Dashboard").ChartObjects("Chart 1")
Set WordApp = CreateObject(class:="Word.Application")
WordApp.Visible = True
WordApp.Activate
Set myDoc = WordApp.Documents.Add
ChartObj.CopyPicture xlScreen, xlPicture
myDoc.Paragraphs(1).Range.PasteSpecial
End Sub
I'm trying to copy a content from excel into a bookmark in MS word. But I'm getting run time error 424. Kindly help me with it. I'm very new to Visual basics and programming as well. I have attached my code.
Thanks
Sub WordDoc()
Dim wrdApp As Object
Dim Number As String
Dim wrdDoc As Object
Set wrdApp = CreateObject("Word.Application")
wrdApp.Visible = True
Set wrdDoc = wrdApp.Documents.Open("H:\IP Automation\createDoc.docx")
Number = Worksheets("Sheet1").Range("A2")
Call InsBookmark(ID, Number)
End Sub
Sub InsBookmark(strBMName, strVariable)
If strVariable <> "" Then
If ActiveDocument.Bookmarks.Exists(ID) Then
ActiveDocument.Bookmarks(ID).Select
Selection.Delete
Selection.InsertAfter (strVariable)
End If
End If
End Sub
You shouldn't seperate this into two subs, as the word doc will not persist across them so "ActiveDocument" wont work. just copy the code from the second sub into the first and replace ActiveDocument with wrdDoc
This should work for you. Give it a go and see how you get along.
Sub Export_Table_Word()
'Name of the existing Word doc.
Const stWordReport As String = "Final Report.docx"
'Word objects.
Dim wdApp As Word.Application
Dim wdDoc As Word.Document
Dim wdbmRange As Word.Range
'Excel objects.
Dim wbBook As Workbook
Dim wsSheet As Worksheet
Dim rnReport As Range
'Initialize the Excel objects.
Set wbBook = ThisWorkbook
Set wsSheet = wbBook.Worksheets("PwC Contact Information")
Set rnReport = wsSheet.Range("Table1")
'Initialize the Word objets.
Set wdApp = New Word.Application
Set wdDoc = wdApp.Documents.Open(wbBook.Path & "\" & stWordReport)
Set wdbmRange = wdDoc.Bookmarks("Report").Range
Dim tbl As Table
For Each tbl In wdDoc.Tables
tbl.Delete
Next tbl
'If the macro has been run before, clean up any artifacts before trying to paste the table in again.
On Error Resume Next
With wdDoc.InlineShapes(1)
.Select
.Delete
End With
On Error GoTo 0
'Turn off screen updating.
Application.ScreenUpdating = False
'Copy the report to the clipboard.
rnReport.Copy
'Select the range defined by the "Report" bookmark and paste in the report from clipboard.
With wdbmRange
.Select
.Paste
End With
'Save and close the Word doc.
With wdDoc
.Save
.Close
End With
'Quit Word.
wdApp.Quit
'Null out your variables.
Set wdbmRange = Nothing
Set wdDoc = Nothing
Set wdApp = Nothing
'Clear out the clipboard, and turn screen updating back on.
With Application
.CutCopyMode = False
.ScreenUpdating = True
End With
MsgBox "The report has successfully been " & vbNewLine & _
"transferred to " & stWordReport, vbInformation
End Sub
I wrote below code to take screenshot form Excel document and paste on word document.It works fine However I am unable to convert This Word document into PDF and error displays"Object does not support this property or method".It seems that i defined Variable (Objword as variant) not correct.Please any one can help.
Sub CopyToWordPicture()
Dim WDApp As Word.Application
Dim WDDoc As Word.Document
Dim objword As variant
Set objword = CreateObject("Word.Application")
objword.Visible = True
objword.Documents.Open "C:\Automation\BH Report\Daily&BH RAN KPI report_ok.docx"
Workbooks.Open Filename:="C:\Automation\BH Report\Daily_Hourly KPI Template.xlsx"
Sheets("BH_Graphs").Select
Range("A1:k50").CopyPicture xlPrinter
With objword
'.Documents.Add
.Selection.Paste
.Visible = True
End With
'WDApp.Visible = True
' WDApp.Selection.GoToNext wdGoToPage
Windows("Daily_Hourly KPI Template.xlsx").Activate
Sheets("Daily_Graphs").Select
Range("A1:J50").CopyPicture xlPrinter
With objword
'.Documents.Add
.Selection.Paste
.Visible = True
End With
'export as PDF
objword.ExportAsFixedFormat OutputFileName:="C:\Automation\BH Report\Daily&BH RAN KPI report.pdf", _
ExportFormat:=wdExportFormatPDF
end sub