For those who have not had the pleasure to tangle with RDCOMClient
, it is both powerful and frustrating. Every time I revisit the beast I find myself mired in a search for documentation. The highest and best hope for this post is that it helps some poor unsuspecting soul who finds herself tangling with the same issue I confronted today.
The Problem
There is a process I automated which required me to access a saved, one page PowerPoint and re-save it after breaking the link to which an image in the slide refers.
Not coincidentally, this slide is itself the product of an R
process which outputs a .png file.
Below you’ll find the code to accomplish this link-breaking exercise. I’ll also post a few helpful documentation links at the bottom.
The Solution
Installation here is never a given. The package suffers from little support.
However, here are two methods to check to get started
# option 1 from source
install.packages("RDCOMClient", repos = "http://www.omegahat.org/R")
# from github
library(devtools)
install_github('omegahat/RDCOMClient')
Now that you have the package installed, here is the code needed to accomplish the task (with heavy commenting)
# load up the library
library(RDCOMClient)
# step 1: create the powerpoint application
app_ppt <- COMCreate("Powerpoint.Application")
# now we must open the file, setting the WithWindow argument to FALSE so the call doesn't open up PPT
# see here for documentation https://docs.microsoft.com/en-us/office/vba/api/powerpoint.presentations.open
# replace "c:/path/to/your/file.pptx" with your path
original_ppt <- app_ppt[["Presentations"]]$Open(FileName = "c:/path/to/your/file.pptx", WithWindow = FALSE)
# now that we have it open, we find the slide, the shape, call LinkFormat and BreakLink
# https://docs.microsoft.com/en-us/office/vba/api/powerpoint.linkformat
# you will have to adjust your Slides(#) for the right page and Shapes(#) to capture the correct image
break_link <- original_ppt$Slides(1)$Shapes(3)$LinkFormat()$BreakLink()
# now save file with no link
original_ppt$SaveAs("c:/new/path/to/file.pptx")
It took a few hours of trial and error to get this right. Specifically, it was a matter of determining correct syntax, and then realizing that I was guessing incorrectly that my ppt slide contained only the linked image. In fact, it contained two others, thus the “3” in “Shapes(3)”.
Now, a few notes on the break_link
call. If you visit this page you will see the “LinkFormat” object with one of its listed methods “BreakLink”.
I hope this post is useful for those in search of solution to this corner case.