Samadhan Kshirsagar
Samadhan Kshirsagar

Reputation: 21

Getting a specific page from Word

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..

Upvotes: 2

Views: 2525

Answers (4)

Azaz ul haq
Azaz ul haq

Reputation: 53

I know its old but I didnt get a proper answer so my solution is here it works fine.

object missing = System.Reflection.Missing.Value; 
var document = application.ActiveDocument;
Word.WdStatistic stat = Word.WdStatistic.wdStatisticPages; 
int num =  aDoc.ComputeStatistics(stat, ref missing);    // Get number of pages

for(int i=0; i<num; i++)
{
    document.ActiveWindow.Selection           // Go to page "i"
            .GoTo(WdGoToItem.wdGoToPage, missing, missing, i.ToString());
    document.ActiveWindow.Selection           // Select whole page
            .GoTo(WdGoToItem.wdGoToBookmark, missing, missing, "\\page");
    document.ActiveWindow.Selection.Copy();   // Copy to clipboard

    // Do whatever you want with the selection
}

Upvotes: 0

rememberjack
rememberjack

Reputation: 911

Are you using Office 2013? We ran into an issue with the same error message when running our interop code against a freshly installed Office 2013. It seems to be due to Office 2013's default "Reading Mode" as mentioned here.

Try turning off the reading mode by setting Application.ActiveWindow.View.ReadingLayout to false (as mentioned in the comments of the article). This call must be performed after you have opened a document. Otherwise, the call would fail with the message: System.Runtime.InteropServices.COMException : This command is not available because no document is open.

Upvotes: 5

neodim
neodim

Reputation: 487

After first call Word application still blocks document. Therefore document is read only. Kill WINWORD.EXE process and then change code to:

Document document = 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);

After work close document:

document.Close();
wordApplication.Quit();

After modifications working 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();

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

Upvotes: 0

Paul C
Paul C

Reputation: 4776

If you can I would use Open XML SDK 2.5 for Microsoft Office

This gives you full access to the document and in my opinion is the most likely to work and not have any memory problems.

Upvotes: 0

Related Questions