none
Open an existing word document and access to an especific location in the document through a windows forms application in c# RRS feed

  • Question

  • First of all, i would like you to know that im not a programmer so spare me the bad terminology or the lack of knowledge about the matter. Taking the above into account, i would to request some help about working with c# and word documents, the situation is the following:

    I have managed to developed a windows forms application on VS using C#. Not a big deal, im using a local database (sql server) , and the application serves as the user interface to insert, update, delete and query info to visualize trend charts of the data being stored. Im almost done, but i still need to do the help part, so i need some advice about how would be the easiest way to do this, cause my programming knowledge is limited to c++ and c#. One solution i have thought of , is using a help document, that i already have written, and with the help of word hyperlinks or references, get to the part of the document where the info requested by the user is located. The problem is that i dont know how to call the hyperlinks or references in the help doument from the application, i only know how to open the document (using the office interop library?, component, complement, what is it??) . Any advice, help, suggestion, etc, will be very useful for me. Thank you in advance

     

    • Moved by Cindy Meister MVPModerator Tuesday, April 12, 2011 6:10 AM not using VSTO technology (From:Visual Studio Tools for Office)
    Sunday, April 10, 2011 5:26 PM

Answers

  • Hello alejol,

    I do it using Document Bookmarks (Select the Word, Line or Region, Menu Insert, Bookmark).

    private void button_Click(object sender, EventArgs e) 
    {
      OpenDocumentAndGotoBookmark("c:\\test.docx", "Test3");
    }
    
    
      
    
    void OpenDocumentAndGotoBookmark(object filename, string bookmarkname)
    {
      // CReate the
      Word.Application wordApp = StartWordApplication();
      wordApp.Visible = true;
    
      Word.Document document = wordApp.Documents.Open(ref filename);
      document.Activate();
    
      Dictionary<string, Word.Bookmark> bookmarks = GetDocumentBookmarks(document);
    
      if(bookmarks.ContainsKey(bookmarkname))
      {
        Word.Bookmark bookmark = bookmarks[bookmarkname];
        bookmark.Select();
      }
      else
      {
        MessageBox.Show(string.Format("Bookmark: {0} does not exists in this document!", bookmarkname));
      }
    
      bookmarks.Clear();
      document = null;
      wordApp = null;
      
      GC.Collect();
      GC.WaitForPendingFinalizers();
    }
    
    const string WORD_PROCESSNAME = "WINWORD";
    const string WORD_APPLICATIONNAME = "Word.Application";
    
    Word.Application StartWordApplication()
    {
      return StartApplication(WORD_PROCESSNAME, WORD_APPLICATIONNAME) as Word.Application;
    }
    
    private object StartApplication(string processName, string applicationName) 
    {
     // Application object
      object app = null;
      try 
      {
    
        // is there an existing application object ?
        if (Process.GetProcessesByName(processName).Length > 0)
        {
          // use the GetActiveObject method to attach an existing application object
          app = Marshal.GetActiveObject(applicationName);
        }
        if (app == null)
        {
    
          // create a new instance
          Type t = Type.GetTypeFromProgID(applicationName);
          app = Activator.CreateInstance(t);
        }
      } 
      catch (System.Exception ex)
      {
        // Some Logging
        Trace.WriteLine(string.Format("Error while starting teh Application: {0}", applicationName));
      }
      return app;
    }
    
    Dictionary<string, Word.Bookmark> GetDocumentBookmarks(Word.Document document)
    {
      // A dictionary that stores all Bookmarks with their names
      Dictionary<string, Word.Bookmark> documentBookmarks = new Dictionary<string, Word.Bookmark>();
      
      // Get the document bookmarks
      Word.Bookmarks bookmarks = document.Bookmarks;
    
      // Loop over all Bookmarks in the Document
      for (int i = bookmarks.Count; i > 0; i--)
      {
        object accessor = i;
    
        // Store them into the Dictionary
        Word.Bookmark bookmark = bookmarks.get_Item(accessor);
        documentBookmarks[bookmark.Name] = bookmark;
      }
    
      // cleanup
      bookmarks = null;
      return documentBookmarks;
    }

    Hope this helps,

    Greets - Helmut

     

     

     
    Monday, April 11, 2011 6:22 AM

All replies

  • If you have a word document containing text that is referenced via a table of contents or hyperlinks, i.e. you can access any part of the text by clicking on a link in the first page of the document, could i call those links from a c# windows forms application, so the document would be displayed at that part?. My main objective is to display a help document when the user presses a "?" button that i have located on every form.

     

    Sunday, April 10, 2011 7:31 PM
  • Hi Alejol,

    Two ways, and there may be others, is to have a unique style name or alternatively a bookmark as the heading title for each chunk of "help" content.  Then in your Windows form use either a Find command if you're using a style or a Goto command if you are using a bookmark to go to the appriate page of the help document.

    Hope this helps


    Regards
    Sunday, April 10, 2011 9:29 PM
  • Hello alejol,

    I do it using Document Bookmarks (Select the Word, Line or Region, Menu Insert, Bookmark).

    private void button_Click(object sender, EventArgs e) 
    {
      OpenDocumentAndGotoBookmark("c:\\test.docx", "Test3");
    }
    
    
      
    
    void OpenDocumentAndGotoBookmark(object filename, string bookmarkname)
    {
      // CReate the
      Word.Application wordApp = StartWordApplication();
      wordApp.Visible = true;
    
      Word.Document document = wordApp.Documents.Open(ref filename);
      document.Activate();
    
      Dictionary<string, Word.Bookmark> bookmarks = GetDocumentBookmarks(document);
    
      if(bookmarks.ContainsKey(bookmarkname))
      {
        Word.Bookmark bookmark = bookmarks[bookmarkname];
        bookmark.Select();
      }
      else
      {
        MessageBox.Show(string.Format("Bookmark: {0} does not exists in this document!", bookmarkname));
      }
    
      bookmarks.Clear();
      document = null;
      wordApp = null;
      
      GC.Collect();
      GC.WaitForPendingFinalizers();
    }
    
    const string WORD_PROCESSNAME = "WINWORD";
    const string WORD_APPLICATIONNAME = "Word.Application";
    
    Word.Application StartWordApplication()
    {
      return StartApplication(WORD_PROCESSNAME, WORD_APPLICATIONNAME) as Word.Application;
    }
    
    private object StartApplication(string processName, string applicationName) 
    {
     // Application object
      object app = null;
      try 
      {
    
        // is there an existing application object ?
        if (Process.GetProcessesByName(processName).Length > 0)
        {
          // use the GetActiveObject method to attach an existing application object
          app = Marshal.GetActiveObject(applicationName);
        }
        if (app == null)
        {
    
          // create a new instance
          Type t = Type.GetTypeFromProgID(applicationName);
          app = Activator.CreateInstance(t);
        }
      } 
      catch (System.Exception ex)
      {
        // Some Logging
        Trace.WriteLine(string.Format("Error while starting teh Application: {0}", applicationName));
      }
      return app;
    }
    
    Dictionary<string, Word.Bookmark> GetDocumentBookmarks(Word.Document document)
    {
      // A dictionary that stores all Bookmarks with their names
      Dictionary<string, Word.Bookmark> documentBookmarks = new Dictionary<string, Word.Bookmark>();
      
      // Get the document bookmarks
      Word.Bookmarks bookmarks = document.Bookmarks;
    
      // Loop over all Bookmarks in the Document
      for (int i = bookmarks.Count; i > 0; i--)
      {
        object accessor = i;
    
        // Store them into the Dictionary
        Word.Bookmark bookmark = bookmarks.get_Item(accessor);
        documentBookmarks[bookmark.Name] = bookmark;
      }
    
      // cleanup
      bookmarks = null;
      return documentBookmarks;
    }

    Hope this helps,

    Greets - Helmut

     

     

     
    Monday, April 11, 2011 6:22 AM