Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get page number?

Tags:

c#

interop

I have this code:

Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.Application();
object nullobj = System.Reflection.Missing.Value;
object file = openFileDialog1.FileName;
Microsoft.Office.Interop.Word.Document doc = app.Documents.Open(
  ref file, ref nullobj, ref nullobj,
  ref nullobj, ref nullobj, ref nullobj,
  ref nullobj, ref nullobj, ref nullobj,
  ref nullobj, ref nullobj, ref nullobj,
  ref nullobj, ref nullobj, ref nullobj);
doc.ActiveWindow.Selection.WholeStory();
doc.ActiveWindow.Selection.Copy();
IDataObject data = Clipboard.GetDataObject();
string text = data.GetData(DataFormats.Text).ToString();
textBox2.Text = text;
doc.Close(ref nullobj, ref nullobj, ref nullobj);
app.Quit(ref nullobj, ref nullobj, ref nullobj);

But it does not return a page number. How can I get the page number?

like image 520
monkey_boys Avatar asked Feb 28 '23 02:02

monkey_boys


2 Answers

I'd say this is a better solution

Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.Application();
object nullobj = System.Reflection.Missing.Value;
object file = openFileDialog1.FileName;
Microsoft.Office.Interop.Word.Document doc = app.Documents.Open(
  ref file, ref nullobj, ref nullobj,
  ref nullobj, ref nullobj, ref nullobj,
  ref nullobj, ref nullobj, ref nullobj,
  ref nullobj, ref nullobj, ref nullobj,
  ref nullobj, ref nullobj, ref nullobj);
doc.ActiveWindow.Selection.WholeStory();
doc.ActiveWindow.Selection.Copy();
IDataObject data = Clipboard.GetDataObject();

// get number of pages
Microsoft.Office.Interop.Word.WdStatistic stat = Microsoft.Office.Interop.Word.WdStatistic.wdStatisticPages;
int pages = doc.ComputeStatistics(stat, Type.Missing);

string text = data.GetData(DataFormats.Text).ToString();
textBox2.Text = text;
doc.Close(ref nullobj, ref nullobj, ref nullobj);
app.Quit(ref nullobj, ref nullobj, ref nullobj);
like image 192
Eric Herlitz Avatar answered Mar 06 '23 18:03

Eric Herlitz


Look at this example:

http://www.c-sharpcorner.com/UploadFile/amrish_deep/WordAutomation05102007223934PM/WordAutomation.aspx

Specifically, look at Word.WdFieldType.wdFieldPage and Word.WdFieldType.wdFieldNumPages.

like image 33
Hans Olsson Avatar answered Mar 06 '23 17:03

Hans Olsson