tags:

views:

57

answers:

2

how can we open a word file with specific page number. can anybody plz help me..??

this is the the code i used to open the file:

public static Application Open(string fileName)
        {
            object fileNameAsObject = (object)fileName;
            Application wordApplication;
            try
            {
                wordApplication = new Application();
                object readnly = false;
                object missing = System.Reflection.Missing.Value;
                wordApplication.Documents.Open(ref fileNameAsObject, ref missing, ref readnly, 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);

                return wordApplication;
            }
            catch (Exception ex)
            {
                LogEntry log = new LogEntry();
                log.Categories.Add("Trace");
                log.Message = ex.ToString();
                Logger.Write(log, "Trace");
                throw new System.IO.FileLoadException("File cannot be opened");
            }
            finally
            {
                wordApplication = null;
            }
        }

how can i use the vba code (Selection.GoTo What:=wdGoToPage, Which:=wdGoToFirst, Count:=3, Name:="" ) equivalent in c sharp to get the page that i want..??? or any other suggestions??

+3  A: 

The equivalent C# interop would be:

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

wordApplication.Selection.GoTo(ref what, ref which, ref count, ref missing);
Mark
A: 

Hi Mark, i tried this in my application.... but the word page is still not getting opened with the specific page number by using the code that u suggested.

could u plz tell me wht other changes i hv to make in this code...

string fileName = @"c:\userGuide.doc";
            object fileNameAsObject = (object)fileName;

            Microsoft.Office.Interop.Word.Application wordApplication;
            try
            {
                wordApplication = new Microsoft.Office.Interop.Word.Application();
                object readnly = false;
                object missing = System.Reflection.Missing.Value;
                wordApplication.Documents.Open(ref fileNameAsObject, ref missing, ref readnly, 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);
                object what = Microsoft.Office.Interop.Word.WdGoToItem.wdGoToPage;
                object which = Microsoft.Office.Interop.Word.WdGoToDirection.wdGoToFirst;
                object count = 3;
                wordApplication.ActiveDocument.GoTo(ref what, ref which, ref count, ref missing);
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.ToString());
            }
            finally
            {
                wordApplication = null;
            }

Thanks in advance...

bjoy
@bjoy, it should be wordApplicaiton.Selection.GoTo and NOT wordApplicaiton.ActiveDocument.GoTo.
Mark
Thanks Mark, It works fine.....
bjoy