none
get specific page from word in c#.net ? RRS feed

  • Question


  • how to get specific page from word in c#.net console application?

    I have tried it ,

    But unfortunately I have got error from my application.

    Following is my code:

    {
    object what = WdGoToItem.wdGoToPage;
    
    object which = WdGoToDirection.wdGoToFirst;
    
    object count = 0;
    
    const string fileName = @"C:\..\..\test.doc";
    
    object fileNameAsObject = fileName;
    
    Application wordApplication = new Application();
    
    object readOnly = false;
    
    object missing = System.Reflection.Missing.Value;
    
    wordApplication.Documents.Open(ref fileNameAsObject, ref missing, ref readOnly, ref missing,
    ref missing, ref missing, ref missing, ref missing,
    ref missing, ref missing, ref missing, ref missing,
    ref missing, ref missing, ref missing, ref missing);
    
    // here on this following line I have got error "This method or property is not available because this command is not available for reading."
    Range startRange = wordApplication.Selection.GoTo(ref what, ref which, ref count, ref missing);
    
    
    object count2 = (int)count + 1;
    
    Range endRange = wordApplication.Selection.GoTo(ref what, ref which, ref count2, ref missing);
    
    
    
    endRange.SetRange(startRange.Start, endRange.End);
    endRange.Select();
    
    
    }

    So please provide me any solution on it.. Thanks In advance.

    Tuesday, February 11, 2014 9:04 AM

Answers

  • Hi Samadhan,

    The code works fine for the first time but not for the second time.

    Are you facing the same issue as below?

    The issue occurred because the document was locking for editing.

    Can you find a residual Word process from task manager?

    If yes, please end the task.

    And after using Word PIA, don’t forget to clear them:

    wordApplication.Documents.Close();
    wordApplication.Quit();
    

    Without any residual Word process, the code you provided works fine for me for every time.

    Here is the whole function:

    static void Wordgoto()
            {
                object what = WdGoToItem.wdGoToPage;
    
                object which = WdGoToDirection.wdGoToFirst;
    
                object count = 1;
    
                const string fileName = @"E:\2.docx";
    
                object fileNameAsObject = fileName;
    
                Microsoft.Office.Interop.Word.Application wordApplication = new Microsoft.Office.Interop.Word.Application();
                wordApplication.Visible = true;
                object readOnly = false;
    
                object missing = System.Reflection.Missing.Value;
    
                wordApplication.Documents.Open(ref fileNameAsObject, ref missing, ref readOnly, ref missing,
                ref missing, ref missing, ref missing, ref missing,
                ref missing, ref missing, ref missing, ref missing,
                ref missing, ref missing, ref missing, ref missing);
    
                // here on this following line I have got error "This method or property is not available because this command is not available for reading."
                Range startRange = wordApplication.Selection.GoTo(ref what, ref which, ref count, ref missing);
    
    
                object count2 = (int)count + 1;
    
                Range endRange = wordApplication.Selection.GoTo(ref what, ref which, ref count2, ref missing);
    
                endRange.SetRange(startRange.Start, endRange.End);
                endRange.Select();
    
                wordApplication.Documents.Close();
                wordApplication.Quit();
            }
    

    I can see the seletion of the document would be changed to the second page. (After setting count with 1 as Eugene mentioned)


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, February 12, 2014 8:27 AM
    Moderator

All replies

  • Hello Samandhan,

    You nee to pass values to the GoTo method of the Selection class starting from 1 (not 0):

    object what = WdGoToItem.wdGoToPage;
    
    object which = WdGoToDirection.wdGoToFirst;
    
    object count = 1;
    
    const string fileName = @"C:\..\..\test.doc";
    
    object fileNameAsObject = fileName;
    
    Application wordApplication = new Application();
    
    object readOnly = false;
    
    object missing = System.Reflection.Missing.Value;
    
    wordApplication.Documents.Open(ref fileNameAsObject, ref missing, ref readOnly, ref missing,
    ref missing, ref missing, ref missing, ref missing,
    ref missing, ref missing, ref missing, ref missing,
    ref missing, ref missing, ref missing, ref missing);
    
    // here on this following line I have got error "This method or property is not available because this command is not available for reading."
    Range startRange = wordApplication.Selection.GoTo(ref what, ref which, ref count, ref missing);
    
    
    object count2 = (int)count + 1;
    
    Range endRange = wordApplication.Selection.GoTo(ref what, ref which, ref count2, ref missing);
    
    
    
    endRange.SetRange(startRange.Start, endRange.End);
    endRange.Select();
    

    Does it help?

    Tuesday, February 11, 2014 1:24 PM
  • Hi ,

    Thanks for your reply...

    Also,Already I have tried whatever u replied me but,

    Still I have got same error from that.

    So anything new Changes in that.

    Thanks,
    Tuesday, February 11, 2014 2:01 PM
  • All parameters are optional. Did you try to omit the count parameter at all passing the missing object instead?
    Tuesday, February 11, 2014 2:22 PM
  • Okay I will try and let you know.

    Thanks,

    Samadhan.

    Tuesday, February 11, 2014 2:24 PM
  • Hi,

    I have removed this count parameter, still gives me error on this following line,

     Range startRange = wordApplication.Selection.GoTo(ref what, ref which, ref missing);     

    //"This method or property is not available because this command is not available for reading."     

    Thanks,

    Samadhan

    Tuesday, February 11, 2014 2:28 PM
  • Did you have a chance to check out the Selection object? Is it a valid object?

    Tuesday, February 11, 2014 3:11 PM
  • Ya,it is valid object..

    but, I think when I debug my application that time I have not get any value on object what, object which and also object count of following lines :

    object what = Microsoft.Office.Interop.Word.WdGoToItem.wdGoToPage;
                    object which = Microsoft.Office.Interop.Word.WdGoToDirection.wdGoToFirst;
                    object count = 1;

    So afterword ,when I move on this following line

    Range startRange = wordApplication.Selection.GoTo(ref what, ref which, ref missing); 

    that time I have got this error:

     //"This method or property is not available because this command is not available for reading."

    Tuesday, February 11, 2014 3:41 PM
  • Hi Samadhan,

    The code works fine for the first time but not for the second time.

    Are you facing the same issue as below?

    The issue occurred because the document was locking for editing.

    Can you find a residual Word process from task manager?

    If yes, please end the task.

    And after using Word PIA, don’t forget to clear them:

    wordApplication.Documents.Close();
    wordApplication.Quit();
    

    Without any residual Word process, the code you provided works fine for me for every time.

    Here is the whole function:

    static void Wordgoto()
            {
                object what = WdGoToItem.wdGoToPage;
    
                object which = WdGoToDirection.wdGoToFirst;
    
                object count = 1;
    
                const string fileName = @"E:\2.docx";
    
                object fileNameAsObject = fileName;
    
                Microsoft.Office.Interop.Word.Application wordApplication = new Microsoft.Office.Interop.Word.Application();
                wordApplication.Visible = true;
                object readOnly = false;
    
                object missing = System.Reflection.Missing.Value;
    
                wordApplication.Documents.Open(ref fileNameAsObject, ref missing, ref readOnly, ref missing,
                ref missing, ref missing, ref missing, ref missing,
                ref missing, ref missing, ref missing, ref missing,
                ref missing, ref missing, ref missing, ref missing);
    
                // here on this following line I have got error "This method or property is not available because this command is not available for reading."
                Range startRange = wordApplication.Selection.GoTo(ref what, ref which, ref count, ref missing);
    
    
                object count2 = (int)count + 1;
    
                Range endRange = wordApplication.Selection.GoTo(ref what, ref which, ref count2, ref missing);
    
                endRange.SetRange(startRange.Start, endRange.End);
                endRange.Select();
    
                wordApplication.Documents.Close();
                wordApplication.Quit();
            }
    

    I can see the seletion of the document would be changed to the second page. (After setting count with 1 as Eugene mentioned)


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, February 12, 2014 8:27 AM
    Moderator
  • Hi George,

    My Issue has been resolved.

    Thanks for that.

    But I have new another  Issue those I have posted in Forum.

    Could you please check it ?provide me any solutions on that?

    Thanks,
    Samadhan
    Thursday, February 13, 2014 7:24 AM
  • Hi Samadhan,

    I have noticed that you post another thread in the forum. So I deleted the reply you post.

    I gave a reply for your new thread.

    In addition, We don't recommend members to ask more than one question in a thread. It would be better to post a new thread so that other community members will involve and you would get more help.

    Thanks for your understanding.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, February 13, 2014 9:10 AM
    Moderator
  • Ya Sure..
    Thursday, February 13, 2014 10:48 AM