Thread: Help with C# --> Word 2003 bookmarks

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    4

    Help with C# --> Word 2003 bookmarks

    Hi,

    This is my first post here and I hope someone can help me with my problem. I want to add text to existing bookmark in a word file from my C# program. The code I have now seems to work but I dont know how to add text to the bookmark.

    Take a look at this,

    Object fileName = "\\test.dot";
    Object readOnly = false;
    Object isVisible = true;
    Object missing = System.Reflection.Missing.Value;

    Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application();

    Microsoft.Office.Interop.Word.Document wordDoc = new Microsoft.Office.Interop.Word.Document();

    wordApp.Visible = true;
    wordDoc = wordApp.Documents.Open(ref fileName, ref missing, ref readOnly, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref isVisible, ref missing, ref missing, ref missing, ref missing);

    string Test = "Test";

    object bookmarkName = "Name";

    if (wordDoc.Bookmarks.Exists(bookmarkName.ToString()) )
    {
    'Code for adding text to bookmark
    }
    wordApp.Documents.Close();

    Okay, so the Test variable with the text "Test" should be placed in the bookmark named "Name".

    Im working with Microsoft Visual C# 2010 Express

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Surely the word "Test" needs to be in the document for you to create a Bookmark to it. If it's just a string variable in your C# app then word cannot create a bookmark to it

  3. #3
    Registered User
    Join Date
    Dec 2010
    Posts
    4
    Thanks for the awnser Fordy

    Maybe it's not even the right way to go for this. I'm gonna try to explain what I want to do.

    First of all, when a new costumer is placed in the database the program will get the information about the costumers name and address.

    This will be placed in two variables, when this is done the information should be sent to the existing template file (test.dot) and the bookmarks in the file (Name and address), after this the file should be print out with the correct information from the database.

    If my code is totally wrong I would appreciate if someone could help me.

  4. #4
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Cant this be done with a simple mail merge?

    How to automate Microsoft Word to perform Mail Merge from Visual C#

    Or just open the document and write the data in the document?

  5. #5
    Registered User
    Join Date
    Dec 2010
    Posts
    4
    Quote Originally Posted by Fordy View Post
    Cant this be done with a simple mail merge?

    How to automate Microsoft Word to perform Mail Merge from Visual C#

    Or just open the document and write the data in the document?
    I dont think that is the best way to go, although I can be wrong :P

    The thing is that the template have alot of "static" text in it, and should be the same at all time. The only thing that should be changed in the text file is the address and name so the letters will be sent to the correct costumer.

    And the letter must have the same layout everytime, thats why I thought bookmarks would be the best way to go?

  6. #6
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Quote Originally Posted by ozzon View Post
    I dont think that is the best way to go, although I can be wrong :P

    The thing is that the template have alot of "static" text in it, and should be the same at all time. The only thing that should be changed in the text file is the address and name so the letters will be sent to the correct costumer.

    And the letter must have the same layout everytime, thats why I thought bookmarks would be the best way to go?
    No a bookmark is like a shortcut to section of a document. If you want to use a standard letter and just change certain fields (name, address) then your best bet would be a mailmerge.

    Have a look at the link I posted earlier

  7. #7
    Registered User
    Join Date
    Dec 2010
    Posts
    4
    Quote Originally Posted by Fordy View Post
    No a bookmark is like a shortcut to section of a document. If you want to use a standard letter and just change certain fields (name, address) then your best bet would be a mailmerge.

    Have a look at the link I posted earlier
    I will take a look at it, thanks mate.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. please help with binary tree, urgent.
    By slickestting in forum C Programming
    Replies: 2
    Last Post: 07-22-2007, 07:55 PM
  2. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 05:49 PM
  3. Wrong Output
    By egomaster69 in forum C Programming
    Replies: 7
    Last Post: 01-28-2005, 06:44 PM
  4. FILES in WinAPI
    By Garfield in forum Windows Programming
    Replies: 46
    Last Post: 10-02-2003, 06:51 PM
  5. Using 'if' with char arrays or string objects
    By c++_n00b in forum C++ Programming
    Replies: 36
    Last Post: 06-06-2002, 09:04 PM