I am trying to amend a VBA macro to enable pasting of an Excel range (as a picture, for formatting purposes) to a Word bookmark.
Sub test2()
Dim objWord As Object
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets("PREMIUMS")
Set objWord = CreateObject("Word.Application")
objWord.Visible = True
objWord.Documents.Open "C:\TEST\BTM Macro Template.docx"
With objWord.ActiveDocument
.Bookmarks("PLAN_1_SHEET").Range.Text = ws.Range("A34").Value
.Bookmarks("PLAN_2_SHEET").Range.Text = ws.Range("BTM_PREM").Value
End With
Set objWord = Nothing
End Sub
The macro pastes a single cell text reference fine ("A34"), but using the same code for a range "BTM_PREM") returns a type mismatch error.
I know it is due to the range not being a string, but can't seem to identify how to amend this line to enable pasting of "BTM_PREM", as a picture, at the "PLAN_2_SHEET" bookmark.
.Bookmarks("PLAN_2_SHEET").Range.Text = ws.Range("BTM_PREM").Value
This is a piece of code that works for me:
ActiveWorkbook.Sheets("Lease 1").Range("B16:AF25").CopyPicture Appearance:=xlScreen, Format:=xlPicture
wdoc.Bookmarks("Bkmrk1").Range.Paste
Application.CutCopyMode = False
It's not a complete macro, just a part of it so you'll have to adjust a bit, But I think you get the idea.
you could use Copy() method on Excel Range object and then either Paste() or PasteSpecial() or PasteExcelTable() Word Range object methods, like follows:
ws.Range("BTM_PREM").Copy
.Bookmarks("PLAN_2_SHEET").Range.Paste
or
ws.Range("BTM_PREM").Copy
.Bookmarks("PLAN_2_SHEET").Range.PasteSpecial Link:=True
or
ws.Range("BTM_PREM").Copy
.Bookmarks("PLAN_2_SHEET").Range.PasteExcelTable LinkedToExcel:=True, WordFormatting:=False, RTF:=True
Related
how to insert a picture which is in an Excel Celle (C1) by VBA in a new created Word document in the header without formatting(no cell color)?
logo.copy
Set objHeader = myDoc.Sections(1).Headers(1).Range
objHeader.Paste
thank you!
Please, try the next way:
Sub InsertHeaderPict()
'copy picture from Excel (open session, active sheet):
Dim appExcel As Excel.Application, ws As Excel.Worksheet
Set appExcel = GetObject(, "Excel.Application")
Set ws = appExcel.ActiveWorkbook.ActiveSheet
ws.Shapes("Picture 1").CopyPicture xlScreen, xlBitmap 'use here your real picture name
'create a table of a row, 3 columns and paste the copied picture in its first cell:
Dim oSec As Word.Section, rng As Range
Set oSec = ActiveDocument.Sections(1)
Set rng = oSec.Headers(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary).Range
With rng
.Tables.Add Range:=rng, NumRows:=1, NumColumns:=3, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:=wdAutoFitWindow
With .Tables(1)
.Borders.InsideLineStyle = wdLineStyleNone
.Borders.OutsideLineStyle = wdLineStyleNone
.Rows.SetLeftIndent LeftIndent:=-37, RulerStyle:=wdAdjustNone
.Cell(1, 1).Range.PasteSpecial
End With
End With
End Sub
There must be a picture in the active sheet of the Excel open session. Use this real picture name instead of "Picture 1" and run the code.
I believe you have the right idea, just have to use copypicture and pastespecial
This is a snippet of my code that does basically the same thing I'm just using the shape object instead of a range.
Set reportHeader = masterReport.Sections.Item(1).Headers(wdHeaderFooterPrimary).Range
masterWorkbook.Worksheets("Template").Shapes("LogoSmall").CopyPicture
reportHeader.PasteSpecial
Thanks again to this post where I originally found this answer.
So I'm trying to understand what is wrong with my code. All I'm doing is taking some charts in my Excel workbook and exporting them to a Word document but I keep getting an error if I try to paste them a certain way. Here's my code:
Sub ExportingToWord_MultiplePages2()
'Declare Word Variables
Dim WrdApp As Word.Application
Dim WrdDoc As Word.Document
'Declare Excel Variables
Dim ChrtObj As ChartObject
'Create a new instance of Word
Set WrdApp = New Word.Application
WrdApp.Visible = True
'Create a new word document
Set WrdDoc = WrdApp.Documents.Add
'Loop through the charts on the active sheet
For Each ChrtObj In ActiveSheet.ChartObjects
'Copy the chart
ChrtObj.Chart.ChartArea.Copy
**'THIS WON'T RETURN AN ERROR**
With WrdApp.Selection
.PasteAndFormat Type:=wdChartPicture
End With
'**THIS WILL RETURN THE ERROR**
With WrdApp.Selection
.PasteAndFormat Type:=wdChartLinked
End With
'Clear the Clipboard.
Application.CutCopyMode = False
Next ChrtObj
End Sub
This is the weird part because I've provided two different ways to paste, the first one I paste it as a chart picture and that works fine. However, if I try wdChart or wdChartLinked it won't work! I get Error 4605 "Command Not Avaiable".
Any thoughts as to why this would be the case?
So I found a workaround to the problem, but I'm still not sure why PasteFormat will not work with a linked chart.
If I replace:
'**THIS WILL RETURN THE ERROR**
With WrdApp.Selection
.PasteAndFormat Type:=wdChartLinked
End With
With the following, I no longer get an error:
'**THIS WILL NOT RETURN AN ERROR**
With WrdApp.Selection
.PasteSpecial Link:=True, DataType:=wdPasteOLEObject
End With
I guess it has to do something with the format of the chart or something, but I still find it strange that I can paste it as a picture using PasteFormat but not as a linked chart.
I am trying to copy multiple Excel charts and paste them to a Word document, on separate pages, as the data type linked OLEObject but I am getting a run-time error.
Run-time error '5343':
Word cannot obtain the data for the
{00020832-0000-0000-C000-000000000046 link.
This is code that I've used in the past but literally, the only thing I changed in this code is to add an outer loop that processes the worksheets in the active workbook. Since adding that outer loop it no longer works, which is a little strange to me because I don't really see what is different.
It works for the first sheet (the currently active one), but fails when the loop moves to the next sheet. It does not matter whether the chart is pasted with or without a link.
Here is the full code for your reference:
Sub ExportingToWord_MultipleCharts()
'Declare Word Variables
Dim WrdApp As Word.Application
Dim WrdDoc As Word.Document
Dim SecCnt As Integer
'Declare Excel Variables
Dim ChrtObj As ChartObject
Dim Rng As Range
'Create a new instance of Word
Set WrdApp = New Word.Application
WrdApp.Visible = True
WrdApp.Activate
'Create a new word document
Set WrdDoc = WrdApp.Documents.Add
'Loop through each worksheet in the active workbook.
For Each WrkSht In ActiveWorkbook.Worksheets
'Loop through the charts on the active sheet
For Each ChrtObj In WrkSht.ChartObjects
'Copy the chart
ChrtObj.Chart.ChartArea.Copy
'Paste the Chart in the Word Document
With WrdApp.Selection
.PasteSpecial Link:=True, DataType:=wdPasteOLEObject, Placement:=wdInLine
End With
'Add a new page to the document.
WrdApp.ActiveDocument.Sections.Add
'Go to the newly created page.
WrdApp.Selection.GoTo What:=wdGoToPage, Which:=wdGoToNext
Next ChrtObj
Next WrkSht
End Sub
It returns the error on the following line:
'Paste the Chart in the Word Document
With WrdApp.Selection
.PasteSpecial Link:=True, DataType:=wdPasteOLEObject, Placement:=wdInLine
End With
I found a workaround, but it still doesn't explain why the error is happening. What I had to do is activate the actual worksheet in the loop.
'***ACTIVATE THE WORKSHEET IN ORDER TO REMOVE THE ERROR***
WrkSht.Activate
For whatever reason, this seemed to remove the error from popping up. However, I find this strange because when I've exported charts from PowerPoint I am not required to activate the worksheet in order to copy it. Here is the code with the adjustments, I've called out the section I added.
Sub ExportingToWord_MultipleCharts()
'Declare Word Variables
Dim WrdApp As Word.Application
Dim WrdDoc As Word.Document
Dim SecCnt As Integer
'Declare Excel Variables
Dim ChrtObj As ChartObject
Dim Rng As Range
'Create a new instance of Word
Set WrdApp = New Word.Application
WrdApp.Visible = True
WrdApp.Activate
'Create a new word document
Set WrdDoc = WrdApp.Documents.Add
'Loop through each worksheet in the active workbook.
For Each WrkSht In ActiveWorkbook.Worksheets
'***ACTIVATE THE WORKSHEET IN ORDER TO REMOVE THE ERROR***
WrkSht.Activate
'Loop through the charts on the active sheet
For Each ChrtObj In WrkSht.ChartObjects
'Copy the chart
ChrtObj.Chart.ChartArea.Copy
'Paste the Chart in the Word Document
With WrdApp.Selection
.PasteSpecial Link:=False, DataType:=wdPasteOLEObject, Placement:=wdInLine
End With
'Add a new page to the document.
WrdApp.ActiveDocument.Sections.Add
'Go to the newly created page.
WrdApp.Selection.GoTo What:=wdGoToPage, Which:=wdGoToNext
Next ChrtObj
Next WrkSht
End Sub
I'm aware that my question may sound/be trivial, but I couldn't find the solution anywhere...and I'm exhausted.
I'm writing a macro to automatize a report generation in Word. At some stage, I need to insert some chart, which is located as a chartsheet from excel...but no way. Here's my code
Sub copy_pic_excel()
Dim xlsobj_2 As Object
Dim xlsfile_chart As Object
Dim chart As Object
Set xlsobj_2 = CreateObject("Excel.Application")
xlsobj_2.Application.Visible = False
Set xlsfile_chart = xlsobj_2.Application.Workbooks.Open("path_to_file.xlsx")
Set chart = xlsfile_chart.Charts("sigma_X_chart")
chart.Select
chart.Copy
With Selection
.PasteSpecial Link:=False, DataType:=wdPasteEnhancedMetafile, _
Placement:=wdInLine, DisplayAsIcon:=False
End With
End Sub
But it keeps showing the error message: "Run-time error '5342': The specified data type is unavailable."
I have no clue why it isn't pasting the chart. I thought to use the clipboard via 'MSForms.DataObject', but i seems that it only works with text (or strings). As far as I understand I have everything that is required, but obviously there's something missing.
Any idea?
If you make excel application visible xlsobj_2.Application.Visible = True, you can see what really happened: when you execute this line chart.Copy, it just copies chart sheet into new workbook. To fix it, use chart.ChartArea.Copy instead:
Sub copy_pic_excel()
Dim xlsobj_2 As Object
Dim xlsfile_chart As Object
Dim chart As Object
Set xlsobj_2 = CreateObject("Excel.Application")
xlsobj_2.Application.Visible = False
Set xlsfile_chart = xlsobj_2.Application.Workbooks.Open("path_to_file.xlsx")
Set chart = xlsfile_chart.Charts("sigma_X_chart")
chart.Select
chart.ChartArea.Copy
With Selection
.PasteSpecial Link:=False, DataType:=wdPasteEnhancedMetafile, _
Placement:=wdInLine, DisplayAsIcon:=False
End With
'clean up
Set xlsfile_chart = Nothing
xlsobj_2.Quit
Set xlsobj_2 = Nothing
End Sub
also note that I've added clean up part of code to exit from excel application and clean memory.
The following code copies a single value into a bookmark in Word. I need it to copy a range of values like "A6:G20".
Sub test()
Dim objWord As Object
Dim ws As Worksheet
Set ws = Workbooks("Portfolio1").Sheets("Print")
Set objWord = CreateObject("Word.Application")
objWord.Visible = True
objWord.Documents.Open "D:Q.docx" ' change as required
With objWord.ActiveDocument
.Bookmarks("monthtable").Range.Text = ws.Range("C6").Value ' here I need range of values to be selected instead of a single cell
End With
Set objWord = Nothing
End Sub
If suitable, you could copy and paste the range:
Range("A6:G20").Copy
.Bookmarks("monthtable").Range.PasteExcelTable False, False, False
There are a number of other Paste methods if you don't wish to paste as an Excel table. Use Word's VB Editor to discover these, or the Word Macro Recorder.
This is of course the important part:
With objWord.ActiveDocument
.Bookmarks("monthtable").Range.Text = ws.Range("C6").Value ' here i need range of values to be selected instead of a single cell
End With
Here you need to cycle through the ws.Range("..."), and for each cell in that range, concatenate that value onto the .Bookmarks.Range.Text value (Rather than setting it equal, which would overwrite). It's probably a good idea to first concatenate this into a string variable and then set the bookmark to that string variable's value, since that would avoid some potential interop issues.