Excel to update PowerPoint Presentation - excel

I have a presentation and I have to update it every week. The information I update are a bunch of imagens I generate from a Excel pivot tables (copy from Excel and paste directly on PowerPoint).
Today I can do this doing this:
Set objPPT = CreateObject("PowerPoint.Application")
objPPT.Visible = True
Set PPTPrez =
objPPT.Presentations.Open("\\network_folder\presentation.pptm")
Set pSlide = PPTPrez.Slides(2)
If pSlide.Shapes.Count <> 0 Then
ActiveWorkbook.Sheets("Pivot1").Range("A8:Z18").CopyPicture
pSlide.Shapes.Paste
EndIf
It work flawless... But I need a litle bit more control and precision...
I need to select the current image on slide, delete it and paste the new one in the same location... Some slides have 3 images or more...
I cann't figure it out how to properly tell to VBA what image are what and choose the pivot table with the correct info for that image... I don't even know if this is possible...
But another solution I have tried is how to specify the position and dimensions of the image on the slide... I can before update, delete all imagens... In this scenario, how to specify the dimensions and positioning?
Thanks!!!
Ps.: Sorry my bad english

This example (based on your code) may point you in the right direction. You need to know the powerpoint shape name (which you can get via VBA or via the ribbon Home-Select-Selection Pane.
Option Explicit
Public Sub UpdateShapes()
Dim vPowerPoint As PowerPoint.Application
Dim vPresentation As Presentation
Dim vSlide As Slide
Dim vShapeName As String
Dim vShape, vNewShape
Set vPowerPoint = New PowerPoint.Application
vPowerPoint.Visible = True
' Open the powerpoint presentation
Set vPresentation = vPowerPoint.Presentations.Open("\\network_folder\presentation.pptm")
' Set slide to be worked on
Set vSlide = vPresentation.Slides(2)
' Set shape to (for this example) "Picture 3"
vShapeName = "Picture 3"
Set vShape = vSlide.Shapes(vShapeName)
' Copy and paste new shape (picture) of range specified
ThisWorkbook.Sheets("Sheet1").Range("A6:B9").CopyPicture
Set vNewShape = vSlide.Shapes.Paste
' Align size and position of new shape to that of old shape
With vNewShape
.Width = vShape.Width
.Height = vShape.Height
.Left = vShape.Left
.Top = vShape.Top
End With
' Delete original shape, rename new shape to original so code works next replace cycle
vSlide.Shapes(vShapeName).Delete
vNewShape.Name = vShapeName
End Sub

Related

Inserting .png files from a folder into PowerPoint with a Labeled Object

I had a question earlier about how to import data into PowerPoint using PowerPoint VBA and ended up finding a solution from Excel VBA. That being said I haven't found a solution that works for Excel because it gives me errors despite copying and pasting the exact code.
strPic ="Picture Name"
Set shp = ws.Shapes(strPic)
'Capture properties of exisitng picture such as location and size
With shp
t = .Top
l = .Left
h = .Height
w = .Width
End With
ws.Shapes(strPic).Delete
Set shp = ws.Shapes.AddPicture("Y:\our\Picture\Path\And\File.Name", msoFalse, msoTrue, l, t, w, h)
shp.Name = strPic
shp.ScaleHeight Factor:=1, RelativeToOriginalSize:=msoTrue
shp.ScaleWidth Factor:=1, RelativeToOriginalSize:=msoTrue
This did NOT work for me because it said the method is not found.
The solution I am looking for can be on either PowerPoint VBA or Excel VBA, I am not picky. Even if it is 1 line of code that does it one time, and doesn't use fancy looping I am ok with that as well because I would rather iterate 100 lines of code and change the slide number than beat my head against the wall trying to find answers to this issue!
TL:DR
VBA Code in Excel or PowerPoint that will allow me to select a named shape in a PowerPoint Slide and then insert a .png image from a folder.
Literally I want this line of code to work, but it doesn't
Set shp = ws.Shapes("Named Shape").AddPicture("Y:\our\Picture\Path\And\File.Name", msoFalse, msoTrue, l, t, w, h)
I have tried this For Excel VBA:
oPPTFile.Slides(100).Shapes("Name").Fill.UserPicture("D:\Pictures\Picture.png")
but it didn't work even though that was the exact same code style I used to replace text in differently named shapes...
oPPTFile.Slides(100).Shapes("Different Name").TextFrame.TextRange.Text = "Some Text"
Maybe instead of oPPTFile it's something else and oPPTFile is meant for text? I don't know, this is where I am stuck!!!
I finally found my answer:
Sub main()
Dim objPresentaion As Presentation
Dim objSlide As Slide
Dim objImageBox As Shape
Set objPresentaion = ActivePresentation
Set objSlide = objPresentaion.Slides.Item(1)
Set objImageBox = objSlide.Shapes.AddPicture("D:\Folder\Picture.png", msoCTrue, msoCTrue, 100, 100)
End Sub
The 100, 100 as near as I can tell ensures that whatever picture you import to the slide object fills the entire object. Key take aways: The shape has to be able to recieve images, and it has to be present on the slide already. Beyond that Item(#) refers to the Slide #, not the shape #. If I am wrong as to why this code works the way it does please correct me, but I have seen a LOT of questions about this subject and very few answers that were straightforward.
Now that being said this is NOT a loop, and I have no idea how to make this a loop process. You can iterate those Set lines as often as you like and it WILL work pretty quickly as well. 100 images maybe took a minute, probably less.
So hopefully this helps someone in the future!
Maybe that explains the problem I am having. I have the shape in the exact place I want the picture to show up. That being said, when it imports, and it does import correctly, when I try to use
With shp.Fill.PictureEffects
Dim eff As PictureEffect
Set eff = .Insert(msoEffectSharpenSoften)
eff.EffectParameters(1).Value = 1
End With
Only some of the pictures work???
Use this instead of the existing code in your example file. I changed the Value to -9 to make it more obvious; you can change that back to whatever you want. This now works with both pictures and pictures in placeholders.
Sub EsoteraCardInitialFormatting()
Dim currentSlide As Slide
Dim shp As Shape
Dim eff As PictureEffect
For Each currentSlide In ActivePresentation.Slides
For Each shp In currentSlide.Shapes
Select Case shp.Type
Case msoPicture
With shp.Fill.PictureEffects
Set eff = .Insert(msoEffectSharpenSoften)
eff.EffectParameters(1).Value = -9
End With
Case msoPlaceholder
If shp.PlaceholderFormat.ContainedType = msoPicture Then
With shp.Fill.PictureEffects
Set eff = .Insert(msoEffectSharpenSoften)
eff.EffectParameters(1).Value = -9
End With
End If
End Select
Next
Next
End Sub

Moving an image in microsoft word using VBA in excel

I am trying to make an automated invoice maker and I currently am having difficulty positioning an image in a word doc using excel VBA
The image loads fine into the word document, yet it is the position that is causing me issues.
edit:
The specific issue that is causing is that the image is intended to be at the top of the page, but appears below the inserted text.
Thanks in advance for any help
Set wordObject = CreateObject("Word.Application")
'create the document object and add documents to it
Set documentObject = wordObject.documents.Add
'setting logo
Set logoObject = documentObject.inlineshapes
'make it visible
wordObject.Visible = True
'create selection object
Set selectionObjectHeader = wordObject.Selection
'place the logo
logoObject.AddPicture ("C:\Users\conno\Documents\logo.png")
With logoObject
.ConvertToShape
End With
With caniprelogoObject.Shapes(1)
.Top = 100
.Left = 100
End With

Use Word Content Control Values for chart object in same Word doc

Using MS Word (in my case 2010 version), I have constructed a form with Content Control elements to be filled out by the user. Now I want certain entries (that I already gave titles to) be shown in a chart inside the same Word document (not in a separate Excel document).
This should be an automated process, so that if the user changes one of the Content Control entries, the chart updates itself automatically; I would also be OK if the user had to press a button in order to update the chart (but the user shouldn't have to click around a lot, since I must assume the user to have little skills.)
So I inserted an Excel chart object in my Word form document. I also wrote some VBA code inside this Excel object to read the Content Control values from the Word document as source for the chart. But I think what I really need is the VBA code to be in my Word document itself (for example to be executed upon click on a button by the user), yet I don't know how to address the Excel chart object and the cells within.
My VBA code inside the Excel object is:
Sub ChartDataAcquirer()
Dim wdApp As Object
Dim wdDoc As Object
Dim DocName As String
Dim ccX As String
Dim ccY As String
Dim datapairs As Integer
'''''''''' Variables '''''''''
DocName = "wordform.docm"
ccX = "titleX"
ccY = "titleY"
datapairs = 5
''''''''''''''''''''''''''''''
Set wdApp = GetObject(, "Word.Application")
Set wdDoc = wdApp.Documents(DocName)
Dim i As Integer
For i = 1 To datapairs
With ActiveSheet.Cells(i + 1, 1) ' The first row contains headline, therefore i+1
.Value = wdDoc.SelectContentControlsByTitle(ccX & i).Item(1).Range.Text ' The CC objects containing the x values have titles "titleX1", "titleX2" ..., therefore "ccX & i"
On Error Resume Next
.Value = CSng(wdDoc.SelectContentControlsByTitle(ccX & i).Item(1).Range.Text) ' To transform text into numbers, if user filled the CC object with numbers (which he should do)
End With
With ActiveSheet.Cells(i + 1, 2)
.Value = wdDoc.SelectContentControlsByTitle(ccY & i).Item(1).Range.Text
On Error Resume Next
.Value = CSng(wdDoc.SelectContentControlsByTitle(ccY & i).Item(1).Range.Text)
End With
Next
End Sub
I guess I need a similar code that is placed in and operates from the Word form document itself, but that is where I am stuck...
The following is demo code that shows how to access an embedded Excel chart.
Note that the Name (Shapes([indexValue])) of your chart Shape is probably different than in this code. You'll need to check and change that assignment. Also, your chart may be an InlineShape rather than a Shape, so you may need to adjust that bit, as well.
This code checks whether the Shape is actually a chart. If it is, the Chart object is accessed as well as its data sheet. Via that, it's possible to get the actual workbook, the worksheets, even the Excel application if you should need it.
Sub EditChartData()
Dim doc As Word.Document
Dim shp As Word.Shape
Dim cht As Word.Chart
Dim wb As Excel.Workbook, ws As Excel.Worksheet, xlApp As Excel.Application
Set doc = ActiveDocument
Set shp = doc.Shapes("MyChart")
If shp.HasChart Then
Set cht = shp.Chart
cht.ChartData.Activate
Set wb = cht.ChartData.Workbook
Set xlApp = wb.Application
Set ws = wb.ActiveSheet
Debug.Print ws.Cells(1, 2).Value2
End If
Set ws = Nothing
Set wb = Nothing
Set cht = Nothing
Set xlApp = Nothing
End Sub

Changing the data-range of an existing PowerPoint-Chart using vba

I'm trying to do the following using vba:
I automatically gather data in Excel and want to paste it in an existing PowerPoint-Chart.
It is working fine, that's the way i do it (the paste-to-powerpoint-part):
Dim myChart As PowerPoint.Chart
Dim myChart As PowerPoint.Chart
Dim myData As PowerPoint.ChartData
Dim myWkb As Excel.Workbook
Dim myWks As Excel.Worksheet
Dim wbcd As Workbook
For chnmb = 1 To 1000
On Error Resume Next
Set myChart = ppSlide.Shapes(chnmb).Chart
'test_name = myChart.Name
If myChart.Name = "" Then Else Exit For
Next
I am doing this above (surely not the perfect way) because I don't know the Chart-Name (it is supposed to work for different Charts in different ppt-Files). After that:
Set myData = myChart.ChartData
Set myWkb = myData.Workbook
Set myWks = myWkb.Worksheets(1)
dat_area = "A1:" & Cells(1 + rowct, 1 + colct).Address(RowAbsolute:=False, ColumnAbsolute:=False)
myWks.ListObjects(1).Resize myWks.Range(dat_area)
That is the part not working.
I manage to fill data into the Chart using:
myWks.Cells(j, i).Value = Workbooks("ppt-tool.xlsm").Sheets("Acc_Data").Cells(Row + j, 1 + i).Value
(via for-next; I don't want to paste the data but fill in every field) and to later edit the Chart, but it won´t change the data-area (with the blue border around it) of the ppt-Chart.
Strangely, if I create a new Chart using
Set myChart = ppSlide.Shapes.AddChart2(297, xlBarStacked100).Chart
I manage to resize the data area (with the same resize-code), but it's not working with existing Charts. Incidentally, I don't want to link the ppt-Chart to Excel (because the Excel-Tool is used over and over again with no data saved and the ppt-Charts may have to be edited later again).

add new data series to an existing excel chart object in powerpoint using vba 2010 - type mismatch

I am outputting an excel chart into powerpoint and I need to add a new data series to the Chart, I have recorded the macro to see how to do it, which it requires a range to a series collection but no luck.
Here is the full working example using access 2010 vba, in which at the end I try to add a new data series:
Option Compare Database
Public Sub CreateChart()
Dim myChart As Chart
Dim gChartData As ChartData
Dim gWorkBook As Excel.Workbook
Dim gWorkSheet As Excel.Worksheet
Dim pptApp As Object
Dim pptobj As Object
Set pptApp = CreateObject("Powerpoint.Application")
Set pptobj = pptApp.Presentation.Add
pptobj.Slides.Add 1, ppLayoutBlank
' Create the chart and set a reference to the chart data.
Set myChart = pptobj.Slides(1).Shapes.AddChart.Chart
Set gChartData = myChart.ChartData
' Set the Workbook and Worksheet references.
Set gWorkBook = gChartData.Workbook
Set gWorkSheet = gWorkBook.Worksheets(1)
' Add the data to the workbook.
gWorkSheet.ListObjects("Table1").Resize gWorkSheet.Range("A1:B5")
gWorkSheet.Range("Table1[[#Headers],[Series 1]]").Value = "Items"
gWorkSheet.Range("a2").Value = "Coffee"
gWorkSheet.Range("a3").Value = "Soda"
gWorkSheet.Range("a4").Value = "Tea"
gWorkSheet.Range("a5").Value = "Water"
gWorkSheet.Range("b2").Value = "1000"
gWorkSheet.Range("b3").Value = "2500"
gWorkSheet.Range("b4").Value = "4000"
gWorkSheet.Range("b5").Value = "3000"
' Apply styles to the chart.
With myChart
.ChartStyle = 4
.ApplyLayout 4
.ClearToMatchStyle
End With
' Add the axis title.
With myChart.Axes(xlValue)
.HasTitle = True
.AxisTitle.Text = "Units"
End With
'Add a new data series - TYPE MISMATCH ERROR!!!
myChart.SeriesCollection.NewSeries
myChart.SeriesCollection(2).Name = "New_Series"
myChart.SeriesCollection(2).Values = gWorkSheet.Range("C2:C5") 'Range that is in the worksheet
'myChart.ApplyDataLabels
' Clean up the references.
Set gWorkSheet = Nothing
' gWorkBook.Application.Quit
Set gWorkBook = Nothing
Set gChartData = Nothing
Set myChart = Nothing
End Sub
This example is in the following link: https://msdn.microsoft.com/en-us/library/office/ff973127(v=office.14).aspx
In order to run this code is necessary to import: Visual Basic For Applications, Microsoft access Object Library, OLE Automation, Microsoft office access database engine object, Microsoft Office Object Library, Microsoft Powerpoint Object Library and Microsoft Excel Object Library. (Powerpoint object library should be imported first and then Microsoft Excel Object Library or there are reference problems)
Do you have any idea of how to add a new data series or what could be wrong with the code?
Thanks a lot in advance.
Try it this way:
With myChart.SeriesCollection.NewSeries
.name = "New_Series"
.Values = gWorkSheet.Range("C2:C5").Value2
End With
First you need to reference the newly added series and you cannot just assume that it will have index 2. This method is safer.
Second take the .Value property of the said range, to get an array of values. This fixed it. The default .Value property of the Range object is not guaranteed to work in all circumstances, i.e. when the context or container is not Excel. So it is better to always be explicit when referencing the value of a range.

Resources