Chart is not copying correctly from Excel to Access - excel

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.

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.

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.

How to position a chart copied from excel into powerpoint using CommandBars?

I am trying to copy some charts (embedded) in different worksheets of an excel workbook using vbscript into different slides of a powerpoint. I would like to keep the link between the excel sheet and the powerpoint while doing so and therefore I used the below piece of code that allows me to keep formatting and link (instead of a simple paste or PasteSpeical. Is there any other way?) :
For i = 1 to TotalNumWorkSheets 'I iterated with indices.
Set pptSlide = pptPres.Slides.Add(i, 11) 'There is one opening slide before this.
set ws = wb.Worksheets(i)
ws.ChartObjects(1).Chart.ChartArea.Copy
pptApp.CommandBars.ExecuteMso("PasteExcelChartSourceFormatting")
pptApp.CommandBars.ReleaseFocus
With pptSlide
.Shapes.Title.TextFrame.TextRange.Text = objCurSheet.Name
'Adding some more textboxes here. Working fine. Position checks out in PPT.
.Shapes(.Shapes.Count).Left = 20 'Doesn't work for all slides.
End With
Next
The code works and copies all the charts, creates titles, adds new text as expected, but I am not able to position the charts on individual slides because after the ExecuteMso command, I don't know how to access the reference to the chart. I read in one of the SO posts that pasting using this method looses the chart selection but you can access the last .Shapes object since pasting always adds the object to the end of the list. Is that always the case? I tried accessing and positioning my chart by accessing the last object but it only works for the first slide of the loop (i.e. the first chart pasted is shifted to Left=20). The rest all charts in other slides are centered. Can someone explain where and how to add the formatting chart options? I ran into an even weirder problem. If I increase the number of worksheets, even the first plot looses it's Left formatting. The above code is the only place where I add formatting so I don't know what is happening. I am sure I am not formatting it correctly.
Many thanks for your suggestions.
Edit: One additional thing which I tested. I am using
WScript.Sleep 500
code before the For loop ends since it gives enough time for earlier operations to finish (at least that's what I understood from many other google searches).
So after some more searching and testing, I found a solution (for my case at least).
Moved the WScript.Sleep 1000 (500 didn't work for me) statement just below CommandBars.ReleaseFocus.
It makes some sense now since it is the Chart copying, pasting and linking from excel that needs time. Especially with source formatting. After that there is only text generation which I believe is not so heavy.
Cleared all the set Object variables when not used. Especially the ones associated with the "With" keyword.
set obj = CreateObject()
With obj
'Do something here.
End With
set obj = Nothing
Not clearing them, apparently, can also prevent you from closing the applications even after you use the .Close and .Quit method. At least that's what I observed. I found PowerPoint.exe running in the task manager when nothing was opened.
After doing the above, I am able to copy with format the charts and also able to set the position of the charts. The weird problem of larger number of worksheets also disappeared. Hopefully, it might help others. If someone thinks the observations are incorrect or troublesome, please correct.

Embed excel into word using vba does not always display the top of the embedded sheet

I have a Word document with VBA code to embed multiple excel sheets using the following process i.e. addOLEObject
Set IllSheet = _
ActiveDocument.Bookmarks("example1").Range.InlineShapes.AddOLEObject(ClassType:="Excel.Sheet.12", filename:=filename1, LinkToFile:=False)
however on occasion the embedded spreadsheet does not display at the top of the sheet but seemingly randomly scrolled part way down the sheet. I have checked and the excel sheet is saved with it scrolled to the top.
How can I ensure the sheet embedded is always scrolled to the top?
I can access the embedded sheet and input values as per below but can not seem to find a suitable line of code to ensure that the sheet displays at the top of active sheet.
Set objOLE = ActiveDocument.InlineShapes(3).OLEFormat
objOLE.Activate
objOLE.Object.ActiveSheet.Cells(1, 3).Value = Format(Funds, "#,###0")
Selection.MoveRight
objOLE.Object.Application.Quit
DoEvents
I have tried scroll functions etc and also want to avoid using SendKeys because it is so unreliable.
Any help greatly appreciated.
M

Information on VBA Chart Exporting

I usually export Excel charts with code like:
Sub SaveChart()
Dim ch As Chart
Set ch = ActiveChart
ch.Export Filename:="C:\TestFolder\tempxx.jpg", FilterName:="JPG"
End Sub
I would like to explore using other picture formats. I would like to know what are the valid values for FilterName. However VBA Help has nothing useful. Can anyone point me an online resource that gives this information??
GIF and PNG are the commonly used picture format which should work With Chart.Export. FilterName is The language-independent name of the graphic filter as it appears in the registry under HKEY_LOCAL_MACHINE\Software\Microsoft\Shared Tools\Graphics Filters.
For many years (beginning with Excel 2003) I used GIF but at some stage Excel became incapable of exporting my charts properly, giving the saved chart a black background that rendered in grey in some applications. Switching to PNG has restored the functionality

Resources