Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to insert html where is my cursor in word add-in using javascript office api?

using this I am Inserting 2 columns table with html. using "Word.InsertLocation.end" this is inserting html to end I want where my cursor clicked insert Html on that location.

async function Insert2ColumnTable() {
    try {
        await Word.run(async (context) => {
            const range = context.document.getSelection();
            const contentControl = range.insertContentControl();
            contentControl.tag = "my-custom-tag";
            // Set the appearance of the content control to "Hidden"
            contentControl.appearance = Word.ContentControlAppearance.hidden;
            contentControl.insertHtml(
                ` <table style="width: 100%;border-collapse:collapse;border:none;">
                    <tbody>
                        <tr>
                             <td style="width: 50%;border-top: 1pt solid black;border-right: 1pt solid black;border-bottom: 1pt solid black;border-image: initial;border-left: 1pt solid black;padding: 0in 5.4pt;vertical-align: top;">
                                <p style='margin:0in;text-align:center;font-size:16px;font-family:"Calibri",sans-serif;color:black;font-weight:bold;'>&nbsp;</p>
                            </td>
                            <td style="width: 50%;border-top: 1pt solid black;border-right: 1pt solid black;border-bottom: 1pt solid black;border-image: initial;1pt solid black;padding: 0in 5.4pt;vertical-align: top;">
                                <p style='margin:0in;text-align:center;font-size:16px;font-family:"Calibri",sans-serif;color:black;font-weight:bold;'>&nbsp;</p>
                            </td>
                        </tr>
                    </tbody>
                </table>
                `,
                Word.InsertLocation.end
            );
            await context.sync();
        });
        
    } catch (error) {
        console.error(error);
    }

};

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

only use this " Word.InsertLocation.replace" its working.

async function Insert2ColumnTable() {
    try {
        await Word.run(async (context) => {
            const range = context.document.getSelection();
            const contentControl = range.insertContentControl();
            contentControl.tag = "my-custom-tag";
            // Set the appearance of the content control to "Hidden"
            contentControl.appearance = Word.ContentControlAppearance.hidden;
            contentControl.insertHtml(
                ` <table style="width: 100%;border-collapse:collapse;border:none;">
                    <tbody>
                        <tr>
                             <td style="width: 50%;border-top: 1pt solid black;border-right: 1pt solid black;border-bottom: 1pt solid black;border-image: initial;border-left: 1pt solid black;padding: 0in 5.4pt;vertical-align: top;">
                                <p style='margin:0in;text-align:center;font-size:16px;font-family:"Calibri",sans-serif;color:black;font-weight:bold;'>&nbsp;</p>
                            </td>
                            <td style="width: 50%;border-top: 1pt solid black;border-right: 1pt solid black;border-bottom: 1pt solid black;border-image: initial;1pt solid black;padding: 0in 5.4pt;vertical-align: top;">
                                <p style='margin:0in;text-align:center;font-size:16px;font-family:"Calibri",sans-serif;color:black;font-weight:bold;'>&nbsp;</p>
                            </td>
                        </tr>
                    </tbody>
                </table>
                `,
                Word.InsertLocation.replace
            );
            await context.sync();
        });
        
    } catch (error) {
        console.error(error);
    }

};
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading