Quantcast
Channel: Active questions tagged blazor - Stack Overflow
Viewing all articles
Browse latest Browse all 4839

Auto load task pane in a newly created Word document from existing word document in a Word add in blazor project?

$
0
0

I have created a new Word document from existing word document in a Word addin Blazor project. I want to add content in a new Word document and also load the task pane in a new word document similar to existing Word task pane. I have written a code but it does not display the content in a new document, it displays in existing word context and the task pane is also not auto loaded in a new word document. Basically I want to create a word document similar to an existing word document.

export async function createNewDocument() {    try {        // Create and open the new document        let newDocUrl;        await Word.run(async (context) => {            const newDoc = context.application.createDocument();            newDoc.open();            // Sync to ensure the new document is opened            await context.sync();            // Get the URL of the new document (if possible, or use another method)            newDocUrl = Office.context.document.url; // This helps confirm we're dealing with the new doc        });        // Adding a short delay to ensure the new document is fully initialized        await new Promise(resolve => setTimeout(resolve, 1000));        // Use another `Word.run` block to interact with the new document explicitly        await Word.run(async (newContext) => {            // Ensure the context is correctly tied to the newly opened document            if (Office.context.document.url === newDocUrl) {                const newDocument = newContext.document;                // Insert content into the new document                newDocument.body.insertText("This is the new content for the new document.",                    Word.InsertLocation.end                );                // Sync after content insertion                await newContext.sync();                // Set the task pane to auto-show with the new document                Office.context.document.settings.set('Office.AutoShowTaskpaneWithDocument',                    true                );                // Save the settings asynchronously                await new Promise((resolve, reject) => {                    Office.context.document.settings.saveAsync((result) => {                        if (result.status === Office.AsyncResultStatus.Succeeded) {                            resolve();                        } else {                            reject(result.error);                        }                    });                });                // Show the task pane explicitly in the new document                Office.addin.showAsTaskpane();                // Sync to confirm task pane is shown                await newContext.sync();            } else {                throw new Error("Failed to switch to the new document context.");            }        });    } catch (error) {        console.error("Error creating or modifying the new document: ", error);        // Detailed error logging        if (error instanceof OfficeExtension.Error) {            console.error("Debug info: ", JSON.stringify(error.debugInfo));        }        throw error; // Re-throw to handle in the calling function    }}

I have successfully created a new word document from existing word document. But I cannot add the content in a new Word document and auto load the task pane as it is similar to existing word task pane. I want to auto load the sideloader in Word. My condition is if the existing document is already saved and it already contains the content I need to create a new document and insert my content or data in a new word document and also load the taskpane(sideloader) same as existing word document? my c# code is below

private async Task<string> EditDocument(){    try    {              isDocumentSaved = await JSRuntime.InvokeAsync<bool>("wordInterop.checkIfDocumentIsSaved");        Console.WriteLine("status of saved docuemnt"+ isDocumentSaved);  if (isDocumentSaved == true)  {      try      {          // Create a new document and ensure all operations are in its context           await JSModule.InvokeVoidAsync("createNewDocument");               return "New document created and operations performed.";      }catch (Exception ex)        {    Console.WriteLine($"Error: {ex.Message}");                throw new Exception("Error in processing new document: " + ex.Message);            }        }  else  { //perform ooperation in existing word document }    }    catch (Exception ex)    {        throw new Exception("message:-"+ ex.Message);    }}

Viewing all articles
Browse latest Browse all 4839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>