none
Get MsWord document page count using Microsoft.Office.Interop.Word RRS feed

  • Question

  •  

     

    I used Following code for get page Count of word document.

    Problem is If run this method after open the  document (Document that pass to method eg:DOC1) using MSoffice ,program will try to open that document(Doc1)and prompt dialog box  "File in use" and ask" do you want to:OPEN READ ONLY"

    using Word = Microsoft.Office.Interop.Word;
    using System.Runtime.InteropServices;
    
    
    
    
    public int GetNoOfPagesDOC(string FileName)
        {
          int num = 0;
    
          object fileName = FileName;
          object readOnly = false;
          object isVisible = false;
          object objDNS = Word.WdSaveOptions.wdPromptToSaveChanges;
          object missing = System.Reflection.Missing.Value;
    
          Word._Application WordApp = new Word.ApplicationClass();
    
          Word._Document aDoc = null;
    
          try
          {
            aDoc = WordApp.Documents.Open
                       (
                         ref fileName,ref missing, ref readOnly, 
                         ref missing,ref missing, ref missing, 
                         ref missing,ref missing, ref missing, 
                         ref missing,ref missing, ref isVisible,
                         ref missing,ref missing, ref missing,
                         ref missing
                       );
    
            Word.WdStatistic stat = Word.WdStatistic.wdStatisticPages;
            num = aDoc.ComputeStatistics(stat, ref missing);
          }
          catch (Exception ex)
          {
          }
          finally
          {
            if (aDoc != null)
            {
              aDoc.Close(ref objDNS, ref missing, ref missing);
              Marshal.ReleaseComObject(aDoc);
              aDoc = null;
            }
            if (WordApp != null)
            {
              WordApp.Quit(ref objDNS, ref missing, ref missing);
              Marshal.ReleaseComObject(WordApp);
              WordApp = null;
            }
            GC.Collect();
            GC.WaitForPendingFinalizers();
          }
          return num;
        }
    

    Greatly appreciate if any one can reply as soon as possible.

    Thanks

     

    Saturday, July 17, 2010 8:42 AM

Answers

  • Hi Amila

    If the document is open, you can only use COM automation (interop) to get information from it. If the document is closed, an alternative approach for *.docx and *.docm files would be to read the OpenXML (for more, see OpenXMLDeveloper.org).

    I think you should be able to use the windows API to determine whether Windows has put a file-lock on the file. If it has, the document should be open (or is otherwise in use and not accessible).

    If you detect a file lock (or if you don't want to do that test), you can check all Processes for winword.exe. If you get a "hit", use GetActiveObject("Word.Application") to assign the Word Application to an object of type Word.Application. (This instead of using new Word.Application, as you've been doing.) Then loop the Documents collection of the Application object to check whether the document in question is listed and, if it is, you can then use the ComputeStatistics method, as in your code. When you're done, set all your object to null, then perform garbage collection (I advise against using ReleaseComObject unless you have a really good reason for doing so).


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by Bessie Zhao Friday, July 23, 2010 8:33 AM
    Sunday, July 18, 2010 6:58 AM
    Moderator

All replies

  • Hi Amila

    You don't give us enough information about the application that's performing this. What kind of application is it? WinForms, Console app, something else?

    Why would you be trying to open the document again if it's already open? Could you please explain the circumstances surrounding when the GetNoofPagesDoc method would be called?

    Certainly, if the document is already open in a running instance of Word, Word can only open it as "Read-Only". Anytime a file is opened and being processed Windows puts a file lock on it... Why would you not be checking whether the file is already open in Word and get the information from that?


    Cindy Meister, VSTO/Word MVP
    Saturday, July 17, 2010 12:29 PM
    Moderator
  • Hi Cindy,

    Thank you very much for you reply.I called this method from console application(For testing).

    Reason for calling this method,

    I am going to write application to get total page count of documents  that updated by user.

    Could you please tell me How to check document already open and how to use that opened document to get page count in program.

    Is there are any other way to get page count of word document with out using com object.

     

    Great if you can reply ASAP.

    Thanks,

     

    Saturday, July 17, 2010 2:28 PM
  •  

     

    I used Following code for get page Count of word document.

    Problem is If run this method after open the  document (Document that pass to method eg:DOC1) using MSoffice ,program will try to open that document(Doc1)and prompt dialog box  "File in use" and ask" do you want to:OPEN READ ONLY"

     

    Reason for calling this method,

    I am going to write application to get total page count of documents  that updated by user.

    Could you please tell me How to check document already open and how to use that opened document to get page count in program.

    Is there are any other way to get page count of word document with out using com object.

     

    using
     Word = Microsoft.Office.Interop.Word;
    using
     System.Runtime.InteropServices;
    
    
    
    
    public
     int
     GetNoOfPagesDOC(string
     FileName)
      {
       int
     num = 0;
    
       object
     fileName = FileName;
       object
     readOnly = false
    ;
       object
     isVisible = false
    ;
       object
     objDNS = Word.WdSaveOptions.wdPromptToSaveChanges;
       object
     missing = System.Reflection.Missing.Value;
    
       Word._Application WordApp = new
     Word.ApplicationClass();
    
       Word._Document aDoc = null
    ;
    
       try
    
       {
        aDoc = WordApp.Documents.Open
              (
               ref
     fileName,ref
     missing, ref
     readOnly, 
               ref
     missing,ref
     missing, ref
     missing, 
               ref
     missing,ref
     missing, ref
     missing, 
               ref
     missing,ref
     missing, ref
     isVisible,
               ref
     missing,ref
     missing, ref
     missing,
               ref
     missing
              );
    
        Word.WdStatistic stat = Word.WdStatistic.wdStatisticPages;
        num = aDoc.ComputeStatistics(stat, ref
     missing);
       }
       catch
     (Exception ex)
       {
       }
       finally
    
       {
        if
     (aDoc != null
    )
        {
         aDoc.Close(ref
     objDNS, ref
     missing, ref
     missing);
         Marshal.ReleaseComObject(aDoc);
         aDoc = null
    ;
        }
        if
     (WordApp != null
    )
        {
         WordApp.Quit(ref
     objDNS, ref
     missing, ref
     missing);
         Marshal.ReleaseComObject(WordApp);
         WordApp = null
    ;
        }
        GC.Collect();
        GC.WaitForPendingFinalizers();
       }
       return
     num;
      }
    

    Greatly appreciate if any one can reply as soon as possible.

    Thanks

     


    Saturday, July 17, 2010 4:24 PM
  • Hi Amila

    If the document is open, you can only use COM automation (interop) to get information from it. If the document is closed, an alternative approach for *.docx and *.docm files would be to read the OpenXML (for more, see OpenXMLDeveloper.org).

    I think you should be able to use the windows API to determine whether Windows has put a file-lock on the file. If it has, the document should be open (or is otherwise in use and not accessible).

    If you detect a file lock (or if you don't want to do that test), you can check all Processes for winword.exe. If you get a "hit", use GetActiveObject("Word.Application") to assign the Word Application to an object of type Word.Application. (This instead of using new Word.Application, as you've been doing.) Then loop the Documents collection of the Application object to check whether the document in question is listed and, if it is, you can then use the ComputeStatistics method, as in your code. When you're done, set all your object to null, then perform garbage collection (I advise against using ReleaseComObject unless you have a really good reason for doing so).


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by Bessie Zhao Friday, July 23, 2010 8:33 AM
    Sunday, July 18, 2010 6:58 AM
    Moderator
  • Hi cindy

    Thank again reply for my post.

    Is there any way get page count using open xml.because i didn't  find way to get page count using open xml.

    thank you

    Monday, July 19, 2010 4:19 AM
  • Hi Amila

    I'm not familiar enough with OpenXML to be able to tell you that. I think that Word does store this information in the document Properties - it would reflect the page count for the last time the document was open in the Word application. But the best place to ask this would be a forum that supports OpenXML.


    Cindy Meister, VSTO/Word MVP
    Monday, July 19, 2010 9:06 AM
    Moderator
  • Hi,

    You can set readonly property as true to avoid any prompt if you just need to get statistics.

    Regards,

    Amir Ali.

    Wednesday, January 5, 2011 8:13 AM