Add Hyperlinks to Powerpoint from Matlab using ActiveX

13 views (last 30 days)
Can someone explain how I can use matlab and activeX to add hyperlinks to powerpoint files?
This question explains how to create a powerpoint file using matlab:
This question 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. I see that "Slide" objects have the ".Hyperlinks" attribute but there is no ".Add" method for Hyperlinks.
Here is the code I have so far:
ppt = actxserver('PowerPoint.Application'); % create a powerpoint object
template = 'link\to\template.pptx'; % path to template
op = invoke(ppt.Presentations,'Open',template,[],[],false); % open the template file
slide = invoke(op.Slides,'Item',1); % create handle for 1st slide
slide.Hyperlinks .... ? % not sure
Please share another example. I would like the links to appear in a table.
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.mathworks.com';
table.Table.Cell(1,1).Shape.TextFrame.TextRange. ... add hyperlink to text
Thanks!

Answers (3)

Brandon
Brandon on 25 Oct 2016
I know this is an old question, but this might help someone in the future.
In Visual Basic you use the ActionSettings member of the TextRange. However the ActionSettings variable doesn't make it through to MATLAB cleanly. It has a count, but doesn't have anything else needed to set the Hyperlinks. I worked around this shortfall the following way:
1. I Added all the text that was to become links using normal MATLAB calls. I used different lines for each link. sprintf is important because it converts '\n' into a real return character.
txt = sprintf("Link 1\nLink 2");
linkFromSlide.Shapes.Placeholders.Item(2).TextFrame.TextRange.Text = txt;
2. I Wrote a Visual Basic Script (makeLink.vbs that takes command-line parameters to set the link
Set args = WScript.Arguments
Set ppt = CreateObject( "PowerPoint.Application" )
Set slide = ppt.ActivePresentation.Slides(CInt(args(0)))
Set line = slide.Shapes.Placeholders.Item(2).TextFrame.TextRange.Lines(args(1))
'WScript.Echo line.Text
With line.ActionSettings(1)
.Action = ppActionHyperlink
.Hyperlink.SubAddress = args(2)
End With
3. Made a system call from my MATLAB script
system(sprintf('cscript makeLink.vbs %d %d %d',linkFromSlide.SlideNumber,lineNumber,linkToSlide.SlideNumber));

Laura Nichifor
Laura Nichifor on 29 Aug 2018
if true
action = textBox.TextFrame.TextRange.ActionSettings(1);
item = action.Item(1);
item.Hyperlink.Address = 'C:\picture1.jpg';
end

Pit Hoffmann
Pit Hoffmann on 28 Aug 2020
As Brandon already said is it an old question. However, there is another solution which may helps others. I've changed the code that you don't need to use a template.
ppt = actxserver('PowerPoint.Application'); % create a powerpoint object
openPPT = ppt.Presentations.Add(); % open powerpoint
randomLayoutObj = openPPT.SlideMaster.CustomLayouts.Item(1); % get layout to create slide
slide = openPPT.Slides.AddSlide(1, randomLayoutObj); % create slide
sH = openPPT.PageSetup.SlideHeight; % slide height
sW = openPPT.PageSetup.SlideWidth; % silde width
table = invoke(slide.Shapes, 'AddTable', 1, 3, 0.05*sW,...
sH*.2, 0.9*sW, sH*.60); % create table
table.Table.Cell(1,1).Shape.TextFrame.TextRange.Text = ...
'An optional answer can be find here'; % add text to table
% ### ADD HYPERLINK ### %
table.Table.Cell(1,1).Shape.TextFrame.TextRange.Find('here')...
.ActionSettings.Item(1).Action = 'ppActionHyperlink'; % find symbols in text
table.Table.Cell(1,1).Shape.TextFrame.TextRange.Find('here')...
.ActionSettings.Item(1).Hyperlink.Address = ['https://de'...
'.mathworks.com/matlabcentral/answers/139986-add-hyperlinks'...
'-to-powerpoint-from-matlab-using-activex']; % add hyperlink-address

Categories

Find more on MATLAB Report Generator in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!