NetOfficeFw / NetOffice

🌌 Create add-ins and automation code for Microsoft Office applications.
MIT License
700 stars 145 forks source link

C# example of adding Hyperlink in PowerPoint #399

Closed Offbeatmammal closed 1 year ago

Offbeatmammal commented 1 year ago

I've been trying to migrate some code from VBA to NetOffice/PowerPoint and not been able to get Hyperlinks working.

What I want to do is to select a particular piece of text and add a hyperlink which when clicked will take the viewer to a subaddress in the current slideshow.

I've got no problems accessing the Hyperlinks collection on the page, but typing to create/add a new one has me stumped... if I use Hyperlink hl = new Hyperlink(); a number of the properties throw an exception eg 'hl.SubAddress' threw an exception of type 'NetOffice.Exceptions.PropertyGetCOMException' if I try to set them, and others, like Type, are read-only so I can't even tell it what I want it to do.

Seems like an common enough task, but unable to find an example so hoping for a pointer...

Offbeatmammal commented 1 year ago

looks like a slightly different approach is needed:

target_sl is the Slide that I want to link to sh is the shape that I want to add the hyperlink to

ActionSetting hlas = sh.ActionSettings[PpMouseActivation.ppMouseClick];
hlas.Action = PpActionType.ppActionNamedSlideShow;
hlas.Hyperlink.SubAddress = target_sl.SlideIndex.ToString();