The issue you’re experiencing with context.document.insertFileFromBase64 merging styles from multiple documents into the Styles Gallery is indeed a known problem. The JavaScript API for Office (Office.js) currently does not provide a built-in method to isolate styles when inserting documents using insertFileFromBase64. Your approach to delete custom styles and hide built-in ones before inserting a new document is a common workaround. Save the initial styles and document state in a global variable during Office.initialize
or Office.onReady
. Before inserting a new document, clear the current styles and apply the initial saved styles. Medical Insurance California
Office.onReady((info) => {
// Save the initial styles and document state
let initialState = {
styles: null,
document: null
``` };
Word.run((context) => {
```java
// Load the current styles and document
let currentStyles = context.document.getStyles();
currentStyles.load();
let currentDocument = context.document;
currentDocument.load();
return context.sync().then(() => {
// Save the initial state
initialState.styles = currentStyles.items;
initialState.document = currentDocument;
});
``` });
});
export const insertDocument = async (fileDataBase64) => {
return Word.run(async (context) => {
```typescript
// Clear current styles and apply initial styles
let currentStyles = context.document.getStyles();
currentStyles.load({ builtIn: true });
await context.sync().then(() => {
currentStyles.items.forEach((style) => {
if (!style.builtIn) {
style.delete();
}
});
// Apply initial styles (assuming initialState is accessible)
context.document.importStylesFromJson(JSON.stringify(initialState.styles));
});
// Insert new file
context.document.insertFileFromBase64(fileDataBase64, "Replace", {
importTheme: true,
importStyles: true,
importParagraphSpacing: true,
importPageColor: true,
importChangeTrackingMode: false,
});
return context.sync();
``` });
};