Saving ComboBoxes and TextBoxes to an Excel book

  • (2 Pages)
  • +
  • 1
  • 2

24 Replies - 1348 Views - Last Post: 08 March 2013 - 06:36 AM Rate Topic: -----

#1 MafiaAndre  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 18-January 13

Saving ComboBoxes and TextBoxes to an Excel book

Posted 30 January 2013 - 08:46 AM

Hi,
Can someone please help me on how to save textboxes and comboboxes to an excel workboook?

The current Code that I have is:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        TextBox1.Text = objWorksheet.Cells(1, 1).text
        TextBox2.Text = objWorksheet.Cells(2, 1).text
        TextBox3.Text = objWorksheet.Cells(3, 1).text
        objExcel.Workbooks.Close()
        objExcel.Application.Quit()
    End Sub



But this code does not work...

Is This A Good Question/Topic? 0
  • +

Replies To: Saving ComboBoxes and TextBoxes to an Excel book

#2 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9204
  • View blog
  • Posts: 34,582
  • Joined: 12-June 08

Re: Saving ComboBoxes and TextBoxes to an Excel book

Posted 30 January 2013 - 08:53 AM

"this code does not work" - how? In what way? Is there an exception? If so what is the text and what line is the exception on? Is it not performing as expected? If so what is it doing/what results is it providing, and what were you expecting? The more you can tell us the better we are at helping you.
Was This Post Helpful? 0
  • +
  • -

#3 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: Saving ComboBoxes and TextBoxes to an Excel book

Posted 30 January 2013 - 09:47 AM

Saving TO excel would require you to reverse your assignments

The left is assigned by the right

So to set textbox1.text to something it would be
TextBox1.Text = something

To Set something to what the textbox holds it would be
Something = Textbox1.Text
Was This Post Helpful? 0
  • +
  • -

#4 MafiaAndre  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 18-January 13

Re: Saving ComboBoxes and TextBoxes to an Excel book

Posted 16 February 2013 - 11:00 AM

View Postmodi123_1, on 30 January 2013 - 08:53 AM, said:

"this code does not work" - how? In what way? Is there an exception? If so what is the text and what line is the exception on? Is it not performing as expected? If so what is it doing/what results is it providing, and what were you expecting? The more you can tell us the better we are at helping you.


What I mean with "does not work" is that when I click on the save button, just the textboxes save, but not the selected item of the combobox. I just need help with the saving of combobox items.
Was This Post Helpful? 0
  • +
  • -

#5 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3480
  • View blog
  • Posts: 11,834
  • Joined: 12-December 12

Re: Saving ComboBoxes and TextBoxes to an Excel book

Posted 16 February 2013 - 11:16 AM

To get the selected item's text:

MessageBox.Show("You selected " & ComboBox1.SelectedItem.ToString())

Did you attempt to find this information yourself?
Was This Post Helpful? 0
  • +
  • -

#6 MafiaAndre  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 18-January 13

Re: Saving ComboBoxes and TextBoxes to an Excel book

Posted 16 February 2013 - 11:23 AM

View Postandrewsw, on 16 February 2013 - 11:16 AM, said:

To get the selected item's text:

MessageBox.Show("You selected " & ComboBox1.SelectedItem.ToString())

Did you attempt to find this information yourself?


Yes I attempted it. No results.
Was This Post Helpful? 0
  • +
  • -

#7 MafiaAndre  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 18-January 13

Re: Saving ComboBoxes and TextBoxes to an Excel book

Posted 17 February 2013 - 02:55 AM

How can I set a SaveFileDialog's coding to save the comboboxes and textboxes to the rows in Excel?

And how can I code the button to make the columns a certain size??

This post has been edited by MafiaAndre: 17 February 2013 - 03:00 AM

Was This Post Helpful? 0
  • +
  • -

#8 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3480
  • View blog
  • Posts: 11,834
  • Joined: 12-December 12

Re: Saving ComboBoxes and TextBoxes to an Excel book

Posted 17 February 2013 - 03:41 AM

View PostMafiaAndre, on 17 February 2013 - 02:55 AM, said:

How can I set a SaveFileDialog's coding to save the comboboxes and textboxes to the rows in Excel?

What? The SaveFileDialog just enables the user to select a filename and location; you have to write the code that copies the combo/textbox content to the Excel file and saves the file.

View PostMafiaAndre, on 17 February 2013 - 02:55 AM, said:

And how can I code the button to make the columns a certain size??

objWorksheet.Columns(1).ColumnWidth = "4"    'or
objWorksheet.Columns(1).AutoFit

Record macros in Excel, which will help you to discover the statements that you need.
Was This Post Helpful? 0
  • +
  • -

#9 MafiaAndre  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 18-January 13

Re: Saving ComboBoxes and TextBoxes to an Excel book

Posted 17 February 2013 - 08:54 AM

View Postandrewsw, on 17 February 2013 - 03:41 AM, said:

View PostMafiaAndre, on 17 February 2013 - 02:55 AM, said:

How can I set a SaveFileDialog's coding to save the comboboxes and textboxes to the rows in Excel?

What? The SaveFileDialog just enables the user to select a filename and location; you have to write the code that copies the combo/textbox content to the Excel file and saves the file.

I have the coding to save the combobox and textbox, but I just want to know how can I code it with the savefiledialog?
My coding:
Dim oExcel As Object
        Dim oBook As Object
        Dim oSheet As Object

        'Start a new workbook in Excel
        oExcel = CreateObject("Excel.Application")
        oBook = oExcel.Workbooks.Add


        'Add data to cells of the first worksheet in the new workbook
        oSheet = oBook.Worksheets(1)
        oSheet.Range("A1").Value = "Account"
        oSheet.Range("C1").Value = "Name"
        oSheet.range("D1").Value = "Surname"
        oSheet.Range("A1:B1").Font.Bold = True
        oSheet.Range("C2").value = TextBox2.Text
        oSheet.Range("D2").Value = TextBox1.Text



        'Save the Workbook and Quit Excel
        oBook.SaveAs("C:\Users\Documents\Save.xlsx")
        oExcel.Quit()
        Marshal.FinalReleaseComObject(oExcel)


Was This Post Helpful? 0
  • +
  • -

#10 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3480
  • View blog
  • Posts: 11,834
  • Joined: 12-December 12

Re: Saving ComboBoxes and TextBoxes to an Excel book

Posted 17 February 2013 - 10:07 AM

My previous post contains an MSDN link for the SaveFileDialog.
Was This Post Helpful? 0
  • +
  • -

#11 MafiaAndre  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 18-January 13

Re: Saving ComboBoxes and TextBoxes to an Excel book

Posted 17 February 2013 - 10:15 AM

View Postandrewsw, on 17 February 2013 - 10:07 AM, said:

My previous post contains an MSDN link for the SaveFileDialog.


The example coding is:
Private Sub button1_Click(sender As Object, e As System.EventArgs)
        Dim myStream As Stream
        Dim saveFileDialog1 As New SaveFileDialog()

        saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
        saveFileDialog1.FilterIndex = 2
        saveFileDialog1.RestoreDirectory = True 

        If saveFileDialog1.ShowDialog() = DialogResult.OK Then
            myStream = saveFileDialog1.OpenFile()
            If (myStream IsNot Nothing) Then 
                ' Code to write the stream goes here.
                myStream.Close()
            End If 
        End If 
    End Sub



Now there where it says " Code to write the stream goes here. " Do I paste that Coding I gave you before your post?
Was This Post Helpful? 0
  • +
  • -

#12 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3480
  • View blog
  • Posts: 11,834
  • Joined: 12-December 12

Re: Saving ComboBoxes and TextBoxes to an Excel book

Posted 17 February 2013 - 10:21 AM

View PostMafiaAndre, on 17 February 2013 - 10:15 AM, said:

Now there where it says " Code to write the stream goes here. " Do I paste that Coding I gave you before your post?


No. If you just paste your code it is highly unlikely to work. You need to:

read the code, study and understand it, and adapt it.
Was This Post Helpful? 0
  • +
  • -

#13 MafiaAndre  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 18-January 13

Re: Saving ComboBoxes and TextBoxes to an Excel book

Posted 17 February 2013 - 10:33 AM

View Postandrewsw, on 17 February 2013 - 10:21 AM, said:

View PostMafiaAndre, on 17 February 2013 - 10:15 AM, said:

Now there where it says " Code to write the stream goes here. " Do I paste that Coding I gave you before your post?


No. If you just paste your code it is highly unlikely to work. You need to:

read the code, study and understand it, and adapt it.


I am very uncertain where to put my coding...?
Was This Post Helpful? 0
  • +
  • -

#14 MafiaAndre  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 18-January 13

Re: Saving ComboBoxes and TextBoxes to an Excel book

Posted 18 February 2013 - 11:21 AM

I have multiple comboboxes and some of them are not used at once sometimes, but other times they can be used.
How can I get rid of this exception:

NullReferenceException was unhandled
Object reference not set to an instance of an object

How can I get rid of this. The user has the choice of selecting an item from the combo box or not.
Was This Post Helpful? 0
  • +
  • -

#15 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3480
  • View blog
  • Posts: 11,834
  • Joined: 12-December 12

Re: Saving ComboBoxes and TextBoxes to an Excel book

Posted 18 February 2013 - 11:33 AM

I hope that modi123_1 won't mind me re-quoting his earlier post, as he states it very well:

Quote

"this code does not work" - how? In what way? Is there an exception? If so what is the text and what line is the exception on? Is it not performing as expected? If so what is it doing/what results is it providing, and what were you expecting? The more you can tell us the better we are at helping you.

and, of course, we would need to see relevant code.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2