Currently, I am working on a project that involves creating an addin for Office. The challenge I am facing is opening other pptx files from within the addin. After some research, I discovered that I need to use base64 for the PowerPoint.createPresentation() function. While hardcoding the base64 string into the function works, my goal is to dynamically generate this string from the file selected by the user, which is retrieved from a server.
Admittedly, I am still quite new to programming and have only managed to find information online without knowing how to properly implement it. Most of the resources I found were related to JavaScript, but I am aware that it should work in TypeScript as well. Nevertheless, I am unsure of how to proceed with either language.
At present, the Base64 string is sourced from an array. When a user chooses a specific option, the corresponding pptx file is opened with the hardcoded string.
HTML:
<div *ngFor="let template of templates">
<button (click)="onClickOpenTemplate(template.src)">
<img src="{{ template.img }}" alt="">
<h3>{{ template.name }}</h3>
</button>
</div>
TypeScript:
templates: any[] = [
{
src: 'base64string',
name: 'filename',
img: 'imgPath'
},
{
src: 'base64string',
name: 'filename',
img: 'imgPath'
},
];
async onCreateOpenPresentation(template) {
PowerPoint.createPresentation(template);
}
The desired outcome is to convert the selected pptx file to base64 when a button is clicked, then open it in a new PowerPoint window instead of relying on hardcoded values.
Edit: I attempted to use code snippets I came across elsewhere
// window.open(template);
// tslint:disable-next-line: prefer-const
let ActiveXObject: (type: string) => void;
try {
const fso = new ActiveXObject('Scripting.FileSystemObject');
const file = fso.OpenTextFile(template, 1);
const fileContent = file.ReadAll();
file.Close();
return fileContent;
} catch (e) {
if (e.number === -2146827859) {
alert('Unable to access local files due to browser security settings. ' +
'To overcome this, go to Tools->Internet Options->Security->Custom Level. ' +
// tslint:disable-next-line: max-line-length
'Find the setting for "Initialize and script ActiveX controls not marked as safe" and change it to "Enable" or "Prompt"');
}
}
const templateBase64 = window.btoa(fileContent);
console.log(templateBase64);
PowerPoint.createPresentation(templateBase64);
Unfortunately, the 'fileContent' variable in 'window.btao()' throws an error. Does anyone know how to resolve this issue?