Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
All the code with definitions of each line.
private void button1_Click(object sender, EventArgs e)
        {
            object oMissing = System.Reflection.Missing.Value;
            object oEndOfDoc = "\\endofdoc"; /* \endofdoc is a predefined bookmark */

            //Start Word and create a new document.
            Word._Application oWord; //Define
            Word._Document oDoc; //Define
            oWord = new Word.Application();//start Word
            oWord.Visible = true;//show Word

            Object oTemplatePath = "C:\\social_assesment_templ.doc";//find the document
            oDoc = oWord.Documents.Add(ref oTemplatePath, ref oMissing, ref oMissing, ref oMissing); //load social_assesment_templ.doc into the document
            Object oBookMark = "Applicant_name"; //A bookmark in the doc file.
            oDoc.Bookmarks.Item(ref oBookMark).Range.Text = "John Doe";//change 'oBookMark''s text range to "John Doe"
            Object fileName = "C:\\TestFile.doc";//Set new file name
            oDoc.SaveAs(ref fileName,ref oMissing, ref oMissing,ref oMissing, ref oMissing,ref oMissing, ref oMissing,ref oMissing, ref oMissing,ref oMissing, ref oMissing, ref oMissing,ref oMissing, ref oMissing,ref oMissing, ref oMissing);//save as 'filename'
        }


here is the line with problems that I can't figure out:
oDoc.Bookmarks.Item(ref oBookMark).Range.Text = "John Doe";//change 'oBookMark''s text range to "John Doe"//The 'Item' is underlined: ...Word.Bookmarks does not contain a definition of 'Item'. However, I found this on one of microsoft's sites.


Thank You.
Posted
Updated 2-Apr-12 21:42pm
v3

I think perhaps you have mixed code from two sites: Word._Document.SaveAs takes 16 parameters: MSDN[^] (and so does the Word.Document.SaveAs which is the one I would expect to use).

Go back to the site you got the code from, and look more closely - I think you have missed something! If nothing else, without teh original site for reference, we can't really help you much!
 
Share this answer
 
Comments
vlad781 3-Apr-12 3:39am    
Alright thanks, the other site was wrong then...That fixes my Save As problem..(I edited my question)
vlad781 3-Apr-12 3:54am    
I solved my second problem, below. I'll try to add both as acceptable solutions.
Replace:
C#
oDoc.Bookmarks.Item(ref oBookMark).Range.Text = "John Doe";

with:
C#
oDoc.Bookmarks.get_Item(ref oBookMark).Range.Text = "John Smith";
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900