Powerpoint Chart Data Update From Excel Instance - Data disappears when Edit Data - excel

I am editing a PPT template with data from Excel using the following code:
Set shapeObject = oPPT.ActivePresentation.Slides(slideIndex).Shapes("Chart 9")
With shapeObject.Chart.ChartData
.Workbook.Worksheets(1).Range("B2").Value = Sheets(slideSheetName).Range("D3").Text
.Workbook.Worksheets(1).Range("B3").Value = Sheets(slideSheetName).Range("D4").Text
.Workbook.Worksheets(1).Range("B4").Value = Sheets(slideSheetName).Range("D5").Text
.Workbook.Close
End With
When I click to Edit Data on the chart in PPT I see the data as it was copied over from the above code. But some users see no data in the Edit Data sheet but the chart itself still has data in it.
Has anyone ever seen that before? What is the fix?

.Workbook.Worksheets(1).
This reference doesn't seem sound.
I'd try something like this:
Workbooks('NameofWB').Sheets('Sheet1')
So the code is explicit as to what workbook is referenced.

Related

Inserting and Linking a Picture into a Shape Fill using VBA

I am trying to link and insert a picture (*.png) into a shapes fill in powerpoint using vba in Excel. In the end I will loop through this on 100+ pages and the pictures will be updated frequently so automating this will be a huge time saver. Currently I have figured out how to loop through the pages and insert the pictures into the shapes, but I have been unable to figure out how to link the pictures too.
I'm using the below code to fill the shape with the picture but I can't find the syntax to both insert and link it:
Pres.Slides(1).Shapes(ShapeName).Fill.UserPicture PictureFilePath
Ultimately this should behave like clicking on a shape, going format > shape fill > picture > insert and link (On the drop down next to insert in the dialog box).
Not all user interface actions are in the VBA object model. One of those exceptions is creating a link to a shape fill. The closest you can get is to link pictures that are inserted as pictures rather than as fills. Here's the syntax to add a linked picture. It assumes the picture is in the same folder as the presentation:
Sub Macro1()
ActiveWindow.Selection.SlideRange.Shapes.AddPicture(FileName:="Picture1.png", LinkToFile:=msoTrue, SaveWithDocument:=msoFalse, Left:=300, Top:=251, Width:=121, Height:=38).Select
End Sub
Welcome to SO.
For answering your question, I'll give some credit to this similar SO question based on Excel and this similar SO question based on PP. Some additional information was gathered from the microsoft documentation on the subject.
What you seem to be looking for is the ActionSetting object in the shapes class, which seems to be availible to shapes in PowerPoint. Below is a code snippet created directly in PowerPoint VBA
Sub insertPictureIntoShapeWithLinkToPicture()
Dim PP_Slide As Slide, PP_Shape As Shape, imagePath As String
Set PP_Slide = ActivePresentation.Slides(1) 'Set slide (change as necessary)
Set PP_Shape = PP_Slide.Shapes(1) 'Set shape (change as necessary)
imagePath = "Path to image"
With PP_Shape
'add picutre
.Fill.UserPicture imagePath
'Set an action on click
With .ActionSettings(ppMouseClick)
'Set action to hyperlink
.Action = ppActionHyperlink
'Specify address
.Hyperlink.Address = imagePath
End With
End With
End Sub
The same approach should be available via Excel, either adding a reference to the PowerPoint object library, or using Late-Binding methods. Note that the 'click' method with hyperlink defaults to standard hyperlink clicking (ctrl + left mouse for windows with a Danish keyboard).
Please find attached code.
First create a shape in PPT and run the code.

VBA: Automating Loading of Data from Excel into PowerPoint Internal Chart Data

I am looking to script the automated loading of data from an Excel Worksheet directly into the embedded Chart Data Source within PowerPoint - as opposed to copying the chart in Excel and pasting the linked chart into PowerPoint.
I have attached a link to an image which demonstrates (I hope) my intention.
I tried to implement some of the code on :
www.think-cell.com/en/support/manual/exceldataautomation.shtml#x28-21500022.1
But not entirely sure if this is the right approach, plus I am having issues debugging the code for:
Dim tcaddin As Object
Set tcaddin = _
Application.COMAddIns("thinkcell.addin").Object
And genuinely confused as to where in my code to implement what it calls the signature:
tcaddin.UpdateChart( _
pres As PowerPoint.Presentation, _
strName As String, _
rgData As Excel.Range, _
bTransposed As Boolean _
)
Genuinely not sure if I've gone about this the wrong way or if it's even possible, but some new and exciting approaches would be massively appreciated!!
If you want to add parts of an excel document to a powerpoint, you can paste the data so that it embeds and links to that document.
https://support.office.com/en-ie/article/insert-excel-data-in-powerpoint-0690708a-5ce6-41b4-923f-11d57554138d
Look at the above link and try the section Link a section of data in Excel to PowerPoint.
Then everytime you open the powerpoint it will ask if you want to update the links that are attached to the excel document.

Excel 2010 Changing Pivot Chart Line Format On Workbook.SaveAs

I've written a bunch of VBA for Excel 2010 to retrieve data from an Access database, place the pivot table into a new workbook and create a pivot chart based on that pivot table with two data series. One series is a bar and the second series is a line on which I have set the .MarkerStyle to xlMarkerStyleNone. The macro also saves the new workbook.
When I open that saved workbook, I find that the line now has markers on it.
In trying to find out what's going on here, I have hit [CTRL]-[BREAK] to stop the macro after the chart is formatted and before it's saved, so I can see what's in the chart format. I can see that the marker style has been set to none when I right-click the series and see the properties, and also by verifying that by seeing that .MarkerStyle on the relevant series is still set to xlMarkerStyleNone.
When I go to save the new workbook manually with [CTRL]-[S], it saves just fine and loads up without the line markers. But when I save the file with save-as ([ALT]-[F], [A]), I see the markers appear after the file is saved.
So I don't think it's code that's doing this, but the Workbook.SaveAs method. But I'll show relevant code just in case.
I have used both xlExcel12 and 50 as the FileFormat when saving as a .xlsb file, and both xlOpenXMLWorkbook and 51 when saving as a regular .xlsx file. This has made no difference, either.
Update: I have also tried the Workbook.Save method, which just saves the new workbook I create as Book1.xlsx, and it has the same problem.
' Chart Formatting
reportSheet.Shapes(10).Chart.SeriesCollection(2).ChartType = xlLine
reportSheet.Shapes(10).Chart.SeriesCollection(2).Format.Line.Visible = msoTrue
reportSheet.Shapes(10).Chart.SeriesCollection(2).Format.Line.ForeColor.RGB = RGB(186, 7, 67)
reportSheet.Shapes(10).Chart.SeriesCollection(2).Format.Line.Transparency = 0
reportSheet.Shapes(10).Chart.SeriesCollection(2).Format.Line.Weight = 1.5
reportSheet.Shapes(10).Chart.SeriesCollection(2).MarkerStyle = xlMarkerStyleNone
' Saving the chart
reportbook.SaveAs fileName:=saveFileSpec, FileFormat:=xlOpenXMLWorkbook
I'm expecting the workbook to save without any changes being made in the save process when using the Workbook.SaveAs method.
OK, this is silly, but I've gotten around it by drum roll formatting-and-saving twice. This will do for now, but I'll try Jon Peltier's suggestions at some point and report back.

Chart is not copying correctly from Excel to Access

Using data from Access, I have created a line chart in Excel. When I go to copy/paste the chart into a report my DB, the image is not the same. But, if I paste the same image into a work doc or outlook email, it looks fine. The same distorted version appears if I paste it into msPaint. Please see the attached screenshots of the charts. The big issue is the x-axis labels. Here is my copy/paste code.
Dim objChart As Chart
objChart.CopyPicture xlScreen, xlBitmap, xlScreen
DoCmd.OpenReport ReportName:="rptDeptWindowChart", View:=acViewDesign
ctlC = Reports!rptDeptWindowChart.Controls.Count
Do
For Each ctl In Reports!rptDeptWindowChart
ctl.Name = "ctlDelPic"
DeleteReportControl "rptDeptWindowChart", "ctlDelPic"
Next
ctlC = Reports!rptDeptWindowChart.Controls.Count
Loop Until ctlC = 0
DoCmd.RunCommand acCmdPaste
Access is not well-known for it's charting capabilities. If I were you, I'd stick to using Access for managing large data sets, and export final results of queries to Excel, and do your charting/plotting/graphing in Excel. Word is great for creating documents, PowerPoint is great for presentations, etc. You need to use the right tool for the right job.

Dynamic source data for powerpoint chart

The data on the chart on the powerpoint slide is dependent on the data encased by the blue lines. The data is on a worksheet that pops out when i click edit data after right clicking on the chart.
I am trying to write a vba code to set the source of the data to include all of the data but to no avail as of now. My code is as follows:
Melon.Chart.SetSourceData _
Source:=Melon.Chart.ChartData.Workbook.Sheets(1).Range("B3:C" & (28 + Weekno))
Melon is the name of the chart on the powerpoint slide. Weekno actually stands for the week number of the 2017 which is a variable depending on the current week. The above code keeps returning a Run-time error '13': Type mismatch error and does not set the source data to the intended range.
Anyone has any idea? All help will be appreciated! Thank you!
With Melon.Chart.ChartData
.Activate
.Workbook.Sheets(1).ListObjects("Table1").Resize Range("$A$1:$C$29")
End With
This Works!!!! Im so happy

Resources