none
Export VB form data to Excel RRS feed

  • Question

  • Hi,

    I am completely new to Visual Basic, is there a way how to export text from TextBox in Visual Basic form into Excel cell.

    Form done in VB on Visual Studio  (not in VBA)

    Thanks,

    Julie

    Wednesday, May 10, 2017 3:32 PM

All replies

  • If using Excel with the extension of .xlsx then look at SpreadSheetLight. In short you open an excel file using SLDocument class and use SetCellValue("B3", "Karen") which would place Karen in B3 of Sheet1. Finally use SaveAs of the SLDocument object to save changes.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Wednesday, May 10, 2017 5:28 PM
    Moderator
  • Hi Julz90,

    Firstly you could need to right-click Add Reference--Browse--Choose C:\Windows\assembly\GAC_MSIL, and then choose Microsoft.Office.Interop.Excel.dll.

    And you can refer to the code below.

     Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            Dim xlsWorkBook As Microsoft.Office.Interop.Excel.Workbook
            Dim xlsWorkSheet As Microsoft.Office.Interop.Excel.Worksheet
            Dim xls As New Microsoft.Office.Interop.Excel.Application
    
            Dim resourcesFolder = IO.Path.GetFullPath("D:\")
            Dim fileName = "Book1.xlsx"
    
            xlsWorkBook = xls.Workbooks.Open(resourcesFolder & fileName)
            xlsWorkSheet = xlsWorkBook.Sheets("Sheet1")
    
            xlsWorkSheet.Cells(1, 1) = TextBox1.Text
    
            xlsWorkBook.Close()
            xls.Quit()
    
            MsgBox("file saved to " & resourcesFolder)
    
        End Sub

    Hope it is helpful to you.

    Best Regards,

    Cherry Bu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, May 11, 2017 6:16 AM
    Moderator
  • Could you be more specific? Which cell are you attempting to export to?

    Paul ~~~~ Microsoft MVP (Visual Basic)

    Thursday, May 11, 2017 12:15 PM
  • As opposed to using the Primary Office Interop Assemblies, which can be limited to specific versions of Office applications, I would suggest using ExcelDNA.  Not only is it compatible with all versions, it also provides some of the best support for UDFs within Excel in addition to all of the expected API functionality.  Install it via NuGet according to the instructions in the link.

    -EDIT-

    I should note that this is for Add-Ins which execute your VB code from within Excel.  This provides a smoother user experience as they do not need to run a separate application and your custom code can be more intuitively found and executed through use of the Ribbon Bar.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"


    Thursday, May 11, 2017 2:42 PM
    Moderator
  • Hi Julz90,

    Have you solved your issue now? if yes, please remember to share your result and close your thread by marking the post, it is very beneficial to the other communities who face the same issue.

    Thanks for your understanding.

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, May 16, 2017 7:57 AM
    Moderator
  • Hi Julie, 

    I see in this thread beside Paul 3 moderators active trying to help you. 

    None of them are asking you to explain your problem a little bit better, because now it is the same likewise "Can I travel to Tokyo". We don't even know where you live. But you get answers like "Take the subway". 

    For your question is at least needed what kind of version of Excel you use. You can simply tell that by giving the extension. 

    Also is good to know if that is to a fresh excel sheet or to an existing one in a certain format. 


    Success
    Cor


    Tuesday, May 16, 2017 8:29 AM