none
Sending Data from Windows Form to Excel RRS feed

  • Question

  • I am trying to send Data from text boxes in my windows form to specific cells in excel. I am forgetting to do something because i keep getting this error...

    Error 1 'ExcelWorkbook1.ThisWorkbook' does not contain a definition for 'textBox1' and no extension method 'textBox1' accepting a first argument of type 'ExcelWorkbook1.ThisWorkbook' could be found (are you missing a using directive or an assembly reference?)

    This is the code I am trying to use in ThisWorkbook.cs.....

                           

            public void WriteStringToCell (string formData)

                Excel.Worksheet wks = this.ActiveSheet as Excel.Worksheet;
                wks.Range["F3"].Value2 = this.textBox1.Text;
                wks.Range["E8"].Value2 = this.textBox2.Text;
                wks.Range["F8"].Value2 = this.listBox1.Text; 

    When i direct reference it right to excel like this...

                wks.Range["F10"].Value2 = "Bla3";
                wks.Range["F11"].Value2 = "Bla3";
                wks.Range["F12"].Value2 = "Bla2";
                wks.Range["F13"].Value2 = "Bla3";

    It Works Fine but obviously that's not what I'm looking to do. I need the data to go from the text boxes and list boxes on the windows form to excel. 

    This is what i have on the even handler for the button... (I want all data to be transferred with one button click)

     private void button1_Click(object sender, EventArgs e)
            
                Globals.ThisWorkbook.WriteStringToCell(this.textBox1.Text);
                this.Dispose();

    I know this is wrong and i have no idea what needs to go in this event handler. I am very new to VSTO as I'm sure u know by reading this. After saying that I ask that your answer is very straight forward as i have searched forums for days not finding help. 



    Tuesday, March 12, 2013 11:50 AM

Answers

All replies

  • Your textbox control in Sheet1, but you use “this.textBox1.Text” to get text in ThisWorkbook.cs. you should use "Global.Sheet1.textBox1.Text"

    I test on my computer, the following codes work for me:

      // In ThisWorkbook.cs 
            public void writeCell(string data)
            {
                Excel.Worksheet wks = this.ActiveSheet as Excel.Worksheet;
                //Excel.Worksheet wks = (Excel.Worksheet)Globals.ThisWorkbook.ActiveSheet;
                wks.Range["A1"].Value2 = Globals.Sheet1.textBox1.Text;
            }
      // In Sheet1.cs
            private void button1_Click(object sender, EventArgs e)
            {
                Globals.ThisWorkbook.writeCell(this.textBox1.Text);
                
            }
    I think this can help you solve your question. Thanks

    Wednesday, March 13, 2013 2:43 AM
  • Hi Jordan8,

    Thank you for posting in the MSDN Forum.

    To begin, I recommend you read the article about this keyword.

    In your project, when you use the keyword this inside a Form class, the keyword this means the current instance of the Form. While in ThisWorkbook class, the keyword this means the current workbook you're working with.

    So, if the textboxes are placed in your Form, it makes no sense to write this.textBox1 inside ThisWorkbook.

    I've created a sample which might be helpful for your case. You can get it from https://skydrive.live.com/redir?resid=EDF9C5F7CB70A004!120&authkey=!AGkkbkY3FPL5xvQ

    Hope it helps.

    Best regards,


    Quist Zhang [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, March 13, 2013 4:34 AM
    Moderator
  • Hello Quist Zhang 

    I cant thank you enough for your post! My problem has been resolved :) Your sample was verry helpful 

    Thank You

    Wednesday, March 13, 2013 11:41 AM