none
How to read word document story which is already open? RRS feed

  • Question

  • I have created a Word add-in. I want to read an active word document which is currently open(Content of word document story) by pressing a button which is on window form, and window form get open by click on the button from the ribbon. So let me be clear that how flow happens.

    Ribbon -> Button on Ribbon (click it) -> A window form open (Having a button to read active document content) -> Reads the document.

    I have tried to access document content by WordOpenXML property and the second method I've tried is to access paragraphs of the active document.

    Try 1: Accessing paragraphs

    private async Task<bool> SaveDocToCryptacomm1(string fileName)
    {    
        [...some code here...]    
        StringBuilder sb = new StringBuilder();    
        long paraCount = application.ActiveDocument.Paragraphs.Count;    
        while (i < paraCount) {    
            sb.Append(application.
              ActiveDocument.Paragraphs[i].Range.Text);    
        }    
        [...some code here...]
    }

    Try 2: Accessing content from WordOpenXML property.

    private async Task<bool> SaveDocToCryptacomm1(string fileName)
    {
        [...some code here...]   
        
        Encoding.Default.
           GetBytes(application.ActiveDocument.WordOpenXML);
    
        [...some code here...]
    }

    Result of Try 1:

    Document get saved successfully but no content is there.

    Result of Try 2:

    Document get saved successfully but when I try to open it, it is corrupted.

    In this case, what should i do, to read active document from Window form button click event.

    Does the Microsoft.Office.Interop.Word.Application.ActiveDocument will work? Am I doing something wrong or what?

    Edit: Need to do this, without saving open document on local drive and Provided code I have written on Window form's button click event. Will it work?

    Wednesday, March 27, 2019 11:12 AM

All replies

  • I have found the Solution for this problem. I was using  

    Microsoft.Office.Interop.Word.Application.ActiveDocument
    instead of 
    Globals.ThisAddIn.Application.ActiveDocument.Content.WordOpenXML
    By this line, I was able to have ActiveDocument content in Flat OPC Format.
    Thursday, April 4, 2019 6:12 AM