How to update a PowerPoint slide with an Excel Chart - openxml

I have one Excel file that has a Chart (Bar Chart). I also have a Powerpoint presentation with 3 slides. Please note that there is no chart "Title" either on the Excel sheet ot on the Powerpoint slides. I need to update the chart in slide #2 with the the chart in the excel file.
Please note that I have searched this forum and what I found was ways to create a new slide as well as updating a chart using the tittle.
Any help will be greatly appreciated. Thanks for your time
EDIT: Your solution is good but does not help me with how to replace a chart in a particular slide. In my question I have stated that there are 3 slides. I want to specifically update the chart in Slide 2. There is no chart title so I will need to find the Chart to replace based on the Slide number. How is this done ? Thanks again.

This can be done by following these steps: (Note: the powerpoint is the target, and the excel is the source)
Open the target and locate the ChartPart you want to replace. Keep a reference to the parent SlidePart where the ChartPart is and copy the ChartPart's RelationshipId to the SlidePart and then delete the ChartPart from the target.
Save the source and leave the file open.
foreach (var slidePart in targetPPT.PresentationPart.SlideParts)
{
if (slidePart.ChartParts.Any())
{
slidePartBookMark = slidePart;
var chartPart = slidePart.ChartParts.First();
chartPartIdBookMark = slidePart.GetIdOfPart(chartPart);
slidePart.DeletePart(chartPart);
slidePart.Slide.Save();
return;
}
}
Open the source and locate the ChartPart that you want to copy to the target. It will be in a DrawingsPart of a WorksheetPart somewhere in your source. Keep a reference to the ChartPart and leave the file open.
foreach (var worksheetPart in sourceXls.WorkbookPart.WorksheetParts)
{
if (worksheetPart.DrawingsPart != null)
if (worksheetPart.DrawingsPart.ChartParts.Any())
{
saveXlsChart = worksheetPart.DrawingsPart.ChartParts.First();
return;
}
}
In the SlidePart reference from step 1, create a new ChartPart with the AddNewPart call using the RelationshipId
var newChartPart = slidePartBookMark.AddNewPart<ChartPart>(chartPartIdBookMark);
Copy the contents of the source chart to the new chartpart you created in step 4 with a FeedData/GetStream idea found here. Save the Slide, and close your files.
newChartPart.FeedData(saveXlsChart.GetStream());
slidePartBookMark.Slide.Save();
sourceXls.Close();
targetPPT.Close();
I was able to get it to work with a simple source/target that only contained one chart each. If your files are more complex, your locate logic will be more than a First().
Hope this helps.

Related

XLConnect disabling vlookup

I am using XLConnect to copy monthly data to a template. The template has vlookups and I can get the new data to the new template file no problem. THe problem is in excel the new template now with the new data the vlookups don't recognize the data until I manually click each cell and the formula bar then it recognizes the data.
I have tried changing the formula to automatic but that still doesn't work. Anyone seen this problem?
I finally figured it out
setForceFormulaRecalculation(wb, sheet = 1, TRUE)
here is an example
https://rdrr.io/cran/XLConnect/man/setForceFormulaRecalculation-methods.html

Add Hyperlinks to Powerpoint from Matlab using ActiveX

Does anyone know how I can use matlab and activeX to add hyperlinks to powerpoint files?
There are two helpful posts on MatlabCentral, but they don't give me everything I need. The first explains how to create a powerpoint file using matlab: "Create Powerpoint Files with Matlab"
and the second shows how to use ActiveX to insert hyperlinks into Excel:"Add Hyperlink in Excel from Matlab" (See the second answer by Kaustubha)
I tried to merge the two answers. In powerpoint the slide objects have the .Hyperlinks attribute, but there is no .Add method for .Hyperlinks as there is in Excel.
Here is the code I have so far. I would like the link to appear in a table:
ppt = actxserver('PowerPoint.Application');
op = invoke(ppt.Presentations,'Add');
slide = invoke(op.Slides,'Add',1,1);
sH = op.PageSetup.SlideHeight; % slide height
sW = op.PageSetup.SlideWidth; % silde width
table = invoke(slide.Shapes, 'AddTable', 1, 3, 0.05*sW, sH*.2, 0.9*sW, sH*.60);
table.Table.Cell(1,1).Shape.TextFrame.TextRange.Text = 'www.stackoverflow.com';
% Add hyperlink to text in table using ActiveX
% slide.Hyperlinks - this exists but there is no add feature
invoke(op,'Save');
invoke(op,'Close');
invoke(ppt,'Quit');
delete(ppt);
Slide objects have a .Hyperlinks collection that you can examine to learn how many hyperlinks there are, where they point and so forth. To add a hyperlink you have to work with individual shapes or text ranges.
Sub AddAHyperlink()
Dim oSh As Shape
' As an example we're going to add hyperlinks to the
' currently selected shape.
' You could use any other method of getting a reference to
' a shape that you like, however:
Set oSh = ActiveWindow.Selection.ShapeRange(1)
' Add a hyperlink to the shape itself:
With oSh
.ActionSettings(1).Hyperlink.Address = "http://www.pptfaq.com"
' you can also add a subaddress if required
End With
' Or add the hyperlink to the text within the shape:
With oSh.TextFrame.TextRange
.Text = "Hyperlink me, daddy, 8 to the click"
.ActionSettings(1).Hyperlink.Address = "http://www.pptools.com"
End With
End Sub
To access the text within a table cell you'd do as you're already doing:
table.Table.Cell(1,1).Shape.TextFrame.TextRange
or
table.Table.Cell(1,1).Shape
or
Set oSh = table.Table.Cell(1,1).Shape
then use the same code as I've shown above
Not sure if this is still an active request from anyone and potentially the program capability has changed; but for anyone else that might be interested it does appear to be possible to add links.
Here is an example from some code I wrote...The slide number/item references will need to get updated for your task but I think it covers the key points. In this example the goal was to add a hyperlink to another slide within the presentation.
hyperlink_text = sprintf('%0.0f, %0.0f, %s', Presentation.Slides.Range.Item(3+i).SlideID, Presentation.Slides.Range.Item(3+i).SlideIndex,Presentation.Slides.Range.Item(3+i).Shapes.Item(2).TextFrame.TextRange.Text);
The hyperlink text will look something like this, as a text string. '250, 4, Slide Title'
Presentation.Slides.Range.Item(3).Shapes.Item(2).Table.Cell(1+i,1).Shape.TextFrame.TextRange.ActionSettings.Item(1).Hyperlink.SubAddress = hyperlink_text;
For internal links the Hyperlink.Address field can be left blank.
It appears that the only thing that was missing from the prior answers was that when using Matlab to execute the powerpoint VBA you need to use ActionSettings.Item(1) to refer to the mouseclick action instead of ActionSettings(1) that was shown from basic powerpoint VBA.
Hopefully this can be helpful for anyone else still looking.
Note that I am currently using Matlab R2017A and Powerpoint in Microsoft 365 ProPlus

Is fpdi shrinking template and/or adding whitespace?

Using FPDI, I first use a template to create a table of contents. I then import additional pages, which are linked to by the table of contents. What I'm experiencing is that FPDI is shrinking the templates and possible adding white space. I believe I have eliminated any browser added whitespace/shrinkage by merging the same documents into one via the passthru() command.
I have pasted code here: http://pastebin.com/VeLEN8nz. Line 45 - 57 is where the Table of Contents gets included as a template file.
The original file is here: http://truckingshow.com/TOC.pdf
The post-FPDI file is here: http://truckingshow.com/TOC-afterFPDI.pdf
The most noticeable difference is in the right and bottom margins.
Thank you for taking a look, please let me know if I can provide more info.
The document that was given to me was "Letter", not "A4" (the default for FPDF())
Instead of $pdf = new FPDI('P', 'pt') Simply using $pdf = new FPDI('P', 'pt', 'Letter') solved the problem.
Thanks!

tt_news: use news title as download title in list view

My news should not contain any text, except for the title. Instead, in the list view the title should directly point to the first attached file (a PDF).
Now I thought the simplest solution for that would be to hide the title, show only the attached files and give them the title of the related news entry via TypoScript.
For the detail view this code works fine:
plugin.tt_news.newsFiles {
labelStdWrap.cObject = TEXT
labelStdWrap.cObject.dataWrap = DB:tt_news:{GPvar:tx_ttnews|tt_news}:title
labelStdWrap.cObject.wrap3 = {|}
labelStdWrap.cObject.insertData = 1
}
...but it uses the GPvar for the currently displayed news, which of course is not present in list view.
So my question is: how do I get the ID of the currently iterated news entry, if possible at all?
I am open to other solutions as well.
You will need to use custom itemMarkerArrayFunc - fastest by copying the sample into your own extension: typo3conf/ext/tt_news/res/example_itemMarkerArrayFunc.php and adding custom marker to $markerArray.
The mentioned sample even demonstrates access to files so I assume that should not be a problem to modify it for your needs.
Note: if you haven't any own ext to copy the func into it, and you don't want to create such, try to copy it somewhere under fileadmin folder, modifying files in original destination is wrong idea, cause you'll lost all changes after next tt_news' update.
Easiest solution I found is using the labelStdWrap:
plugin.tt_news.newsFiles {
labelStdWrap = TEXT
labelStdWrap.field= title
}
More options can be found here: http://typo3.org/documentation/document-library/core-documentation/doc_core_tsref/4.0.0/view/5/13/

How to add content control in a Word 2007 document using OpenXML

I want to create a word 2007 document without using object model. So I would prefer to create it using open xml format. So far I have been able to create the document. Now I want to add a content control in it and map it to xml. Can anybody guide me regarding the same???
Anoop,
You said that you are able to creat the document using OpenXmlSdk. With that assumption, you can use the following code to create the content control to add to the Wordprocessing.Body element of your Document.
//praragraph to be added to the rich text content control
Run run = new Run(new Text("Insert any text Here") { Space = StaticTextConstants.Preserve });
Paragraph paragraph = new Paragraph(run);
SdtProperties sdtPr = new SdtProperties(
new Alias { Val = "MyContentCotrol" },
new Tag { Val = "_myContentControl" });
SdtContentBlock sdtCBlock = new SdtContentBlock(paragraph);
SdtBlock sdtBlock = new SdtBlock(sdtPr, sdtCBlock);
//add this content control to the body of the word document
WordprocessingDocument wDoc = WordprocessingDocument.Open(path, true); //path is where your word 2007 file is
Body mBody = wDoc.MainDocumentPart.Document.Body;
mBody.AppendChild(sdtBlock);
wDoc.MainDocumentPart.Document.Save();
wDoc.Dispose();
I hope this answers a part of your question. I did not understand what you ment by "Map it to XML". Did you mean to say you want to create CustomXmlBlock and add the ContentControl to it?
Have a look for the Word Content Control Toolkit on www.codeplex.com.
Here is a very brief explanation on how to do what you are attempting.
You need to have access to the developer tab on the Word ribbon. To get this working click on the Office (Round thingy) in the top left hand corner and Select Word Options at the bottom of the menu. On the first options page there is a checkbox to show the developer toolbar.
Use the developer toolbar to add the Content controls you want on the page. Click the properties button in the Content controls section of the developer bar and set the name and tag properties (I stick to naming the name and tag fields with the same name).
Save and close the word document.
Open the Content control toolkit and then open your document with the toolkit. Use the left hand pain to create some custom xml to link to your controls.
Now use the bind view to drag and drop the mappings between your custom xml and the custom controls that are displayed in the right panel of the toolkit.
You can use the openxml sdk 1.0 or 2.0 (still in ctp) to open your word document in code and access the custom xml file that is contained as part of the word document.
If you want to have a look at how your word document looks as xml. Make a copy of your word document and then rename it to say "a.zip". Double click on the zip file and then navigate the folder structure. The main content of the word document is held under the word folder in a file called "document.xml". The custom xml part of the document is held under the customXml folder and is generally found in the file named "item1.xml".
I hope this brief explanation get you up and running.