Insert Text Into a Word .Doc From a C# TextBox

Traitée Insert Text Into a Word .Doc From a C# TextBox

  • Thursday, August 16, 2012 9:35 AM
     
      Has Code

    Microsoft.Office.Interop.Word.Document oDoc = new Document(); oDoc = oWordApplic.Documents.Open(@"C:\Documents and Settings\tsiphambo\Desktop\Edgars.doc"); if (oDoc.Bookmarks.Exists("DateOfScan")) { object oBookMark = "DateOfScan"; oDoc.Bookmarks.get_Item(ref oBookMark).Range.Text = txtSelectDate.Text; } oDoc.SaveAs(@"C:\Documents and Settings\tsiphambo\Desktop\Edgars.doc"); this.Close(); MessageBox.Show("The file is successfully saved!");

    oWordApplic
    i have an error on line 2 of this current Code, i Have added Ms word interop

    ERROR(Doesnt exist in current Context)

    I am trying to insert the date displayed in my C# textBox into the bookmark i have set in my .Doc Document

    Thanx Plz Help

    Regards

All Replies

  • Thursday, August 16, 2012 9:39 AM
     
      Has Code

    Add this line at the top

    Word.ApplicationClass oWordApplic = new Word.ApplicationClass();


    Karthikeya

  • Thursday, August 16, 2012 9:59 AM
     
      Has Code

     added that  Like you said:

    Word.ApplicationClass oWordApplic = new Word.ApplicationClass();

    Error 2 The type or namespace name 'Word' could not be found (are you missing a using directive or an assembly reference?) 

    this is what im getting:

    This Code is Under: this Button

     private void btnReport_Click(object sender, EventArgs e)   {

     

    Microsoft.Office.Interop.Word.Document oDoc = new Document();
                    oDoc
    = oWordApplic.Documents.Open(@"C:\Documents and Settings\tsiphambo\Desktop\Edgars.doc");
                       
                   
    if (oDoc.Bookmarks.Exists("DateOfScan"))
                           
                       
    {                    
                           
    object oBookMark = "DateOfScan";
                            oDoc
    .Bookmarks.get_Item(ref oBookMark).Range.Text = txtSelectDate.Text;
                       
    }
                    oDoc
    .SaveAs(@"C:\Documents and Settings\tsiphambo\Desktop\Edgars.doc");
                   
    this.Close();
                   
    MessageBox.Show("The file is successfully saved!");

  • Thursday, August 16, 2012 10:37 AM
     
      Has Code
    Try this
    ApplicationClass oWordApplic = new ApplicationClass();
                Document oDoc = new Document(); 
                    oDoc = oWordApplic.Documents.Open(@"C:\Documents and Settings\tsiphambo\Desktop\Edgars.doc");
                        
                    if (oDoc.Bookmarks.Exists("DateOfScan"))
                            
                        {                     
                            object oBookMark = "DateOfScan";
                            oDoc.Bookmarks.get_Item(ref oBookMark).Range.Text = txtSelectDate.Text;
                        }
                    oDoc.SaveAs(@"C:\Documents and Settings\tsiphambo\Desktop\Edgars.doc");


    Karthikeya

  • Thursday, August 16, 2012 11:16 AM
     
     

    Error 1 Interop type 'Microsoft.Office.Interop.Word.ApplicationClass' cannot be embedded. Use the applicable interface instead.

    am i Not missing some reference to this class?

  • Thursday, August 16, 2012 1:18 PM
     
     Answered

    Try this

    • Right click the offending assembly that you added in the solution explorer under your project References. (In my case WIA)
    • Click properties.
    • And there should be the option there for Embed Interop Assembly.
    • Set it to False

    From this thread Interop type cannot be embedded


    Karthikeya

    • Marked As Answer by Hefff Thursday, August 16, 2012 3:23 PM
    •