Results 1 to 30 of 30

Thread: [2005] VB.net write to Excel?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2006
    Posts
    126

    [2005] VB.net write to Excel?

    I'm very new to VB.net so please excuse me if I'm posting this in the wrong location. I'm hoping someone can get me started on a very very basic example here to get me started.

    What I want to do is create a form that a user can fill in two simple combo boxes with some simple information hit OK and write that information to an excel file. I was able to do this in VBA but totally lost on what VB.net code looks like or is structured compared to VBA. So if anyone could post a very basic simple form with one or two combo boxes that write to a simple excel file would be great!!

    Thanks for any help, I really appreciate it!!

    Mark
    Last edited by Hack; Mar 31st, 2006 at 07:47 AM. Reason: Added version to thread title

  2. #2
    Member
    Join Date
    Mar 2006
    Posts
    45

    Re: VB.net write to Excel?

    Hi, this is actually quite easy to do... I think the code below should work, although I haven't tested it. You need to include a reference to Microsoft.Office.Core in the project as well.

    EDIT: To be more precise, you have to right click References in the solution explorer, then 'add reference.' Once you have done that click the 'COM' tab and scroll down until you see 'Microsoft Excel 11.0 Object Library' add this by double clicking it, and for good measure, scroll down and add 'Microsoft Office 11.0 Object Library' as well.

    VB Code:
    1. 'set up Excel variables
    2.         Dim xcApp As New Excel.Application
    3.         Dim xcWB As Excel.Workbook = xcApp.Workbooks.Open("c:/myExcelFile.xls")
    4.         Dim xcWS As Excel.Worksheet = CType(xcWB.Worksheets(1), Excel.Worksheet)
    5.         Dim xcRange As Excel.Range
    6.  
    7.         'set up other variables
    8.         Dim strRange As String  'the cell that is currently being looked at
    9.         Dim intRow As Integer = 1   'the row currently being looked at
    10.         Dim sValue as String = "Write me to file!"
    11.        
    12.         'Change the value at strRange
    13.         strRange = "A" & CStr(intRow)
    14.         xcRange = xcWS.Range(strRange)
    15.         xcRange.Value = sValue
    16.         xcWB.Save()
    17.  
    18.         'clean up
    19.         xcWB.Close()
    20.         xcApp.Quit()
    21.         xcApp = Nothing
    22.         xcWB = Nothing
    23.         xcWS = Nothing
    24.         xcRange = Nothing

    Please note that in this example "c:/myExcelFile.xls" must already exist.

    To modify another row, simply change the range (e.g. xcRange = "B1"). Note that this could be done with a for loop if you wanted to change a whole lot of values.

    I've been trying to find the tutorial where I originally learnt how to use excel in vb but I can't seem to locate it.
    Last edited by Mecharius; Mar 29th, 2006 at 02:17 AM.
    VB.2003

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Mar 2006
    Posts
    126

    Re: VB.net write to Excel?

    Ok that example seems pretty simple and some what similar to what I've done in VBA. I tried running the code and I'm getting some errors saying.

    1. Type 'Excel.Application' is not defined.
    2. Type 'Excel.Workbook' is not defined.

    I think this has to do with adding the reference to Microsoft Excel 11.0. So what I did was right click in the solutions center, then hit ADD reference. I went to COM and found the two references you said. I CTRL selected each of the two to highlight them, and then hit OK? Then went back into the Add resource and went to Recent and they were listed there are recent?

    Is this the correct way to do this? Note: I should of stated this before, I'm running the Microsoft Visual Basic 2005 Express Edition. Hopefully this is still possible.

    Thank you very much for your help!!

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: VB.net write to Excel?

    Here is one of my code examples. Its a bit more complete.

    What version of vb.net are you runnng?


    http://www.vbforums.com/showpost.php...07&postcount=5
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Mar 2006
    Posts
    126

    Re: VB.net write to Excel?

    I'm running the Microsoft Visual Basic 2005 Express Edition. (The Free one) Is this what you're asking? Or somewhere else to find the version of Vb.net?

  6. #6
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: VB.net write to Excel?

    Nope, thats what I wanted to know.
    As long as you have the references added then the code examples should be good for you.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  7. #7
    Member
    Join Date
    Mar 2006
    Posts
    45

    Re: VB.net write to Excel?

    My example (yes I know its basic RobDog888) was for VB.NET 2003. I don't know if it will transfer across... best to use RobDog's!
    VB.2003

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Mar 2006
    Posts
    126

    Re: VB.net write to Excel?

    Cool these examples got me going. Now in my form I'm writing to my excel file. Well when I keep running the program it keeps writing over the same line in the excel file.

    In one of my VBA apps I use this to count the used rows. Then add one to start a new line of information. Is there a similar function for vb.net?

    The.....mycount = wksht.UsedRange.Rows.Count.......I think is where vb.net doesn't understand it?


    VB Code:
    1. VB code
    2.  
    3. Dim mycount As Long
    4. Dim CurRow As Long
    5.  
    6. mycount = wksht.UsedRange.Rows.Count
    7. CurRow = mycount + 1
    8.  
    9.                 MySht.Cells(CurRow, 1) = MyJobNumbernName
    10.                 MySht.Cells(CurRow, 2) = MyJobPM

    Thanks for any help!!

  9. #9
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: VB.net write to Excel?

    Used rows could be giving the wrong cell address if you have some bogus data somewhere down the sheet.

    There is another function for this too that is a bit more flexiable.

    VB Code:
    1. wksht.Cells.SpecialCells(xlCellTypeLastCell).Row
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Mar 2006
    Posts
    126

    Re: VB.net write to Excel?

    Right on!!..Im gonna give this a try!!

    Thanks for the help!!

  11. #11
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: VB.net write to Excel?

    NP, also check out it other constant arguments that make it better to use. You can get the last row/column with a formula in it or several other types.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  12. #12

    Thread Starter
    Lively Member
    Join Date
    Mar 2006
    Posts
    126

    Re: VB.net write to Excel?

    hmm...Ok Im getting the

    Error 2 Name 'xlCellTypeLastCell' is not declared.

    Here is my code so far maybe you could identify my problem?

    VB Code:
    1. My Code
    2.  
    3. Dim xlApp As New Microsoft.Office.Interop.Excel.Application
    4.         xlApp.Visible = True
    5.         xlApp.UserControl = True
    6.         Dim oldCI As System.Globalization.CultureInfo = _
    7.             System.Threading.Thread.CurrentThread.CurrentCulture
    8.         System.Threading.Thread.CurrentThread.CurrentCulture = _
    9.             New System.Globalization.CultureInfo("en-US")
    10.         Dim xlBook As Microsoft.Office.Interop.Excel.Workbook
    11.  
    12.         xlBook = xlApp.Workbooks.Open("c:\JobInfo.xls")
    13.  
    14.         Dim xlSheet As New Microsoft.Office.Interop.Excel.Worksheet
    15.  
    16.         Dim MyName As String
    17.         Dim MyDate As String
    18.         Dim MyType As String
    19.         Dim MyJobNumber As String
    20.         Dim MyJobName As String
    21.         Dim MyScope As String
    22.         Dim MyClient As String
    23.         Dim MyPrincipal As String
    24.  
    25.         MyName = CStr(ListBox1.Text)
    26.         MyDate = CStr(DateTimePicker1.Text)
    27.         MyType = CStr(ComboBox4.Text)
    28.         MyJobNumber = CStr(ComboBox2.Text)
    29.         MyJobName = CStr(TextBox1.Text)
    30.         MyScope = CStr(ComboBox1.Text)
    31.         MyClient = CStr(ComboBox5.Text)
    32.         MyPrincipal = CStr(ComboBox6.Text)
    33.  
    34.         xlSheet = xlBook.Worksheets(1)
    35.  
    36.         'xlSheet.Cells.Item(1, 1) = MyName
    37.         xlSheet.Cells.Item(1, 2) = DateTimePicker1
    38.         xlSheet.Cells.Item(1, 3) = MyType
    39.         xlSheet.Cells.Item(1, 4) = MyJobNumber
    40.         xlSheet.Cells.Item(1, 5) = MyJobName
    41.         xlSheet.Cells.Item(1, 6) = MyScope
    42.         xlSheet.Cells.Item(1, 7) = MyClient
    43.         xlSheet.Cells.Item(1, 8) = MyPrincipal
    44.  
    45.  
    46.         xlBook.Save()
    47.         xlBook.Close()

    So here in this section.

    'xlSheet.Cells.Item(1, 1) = MyName
    xlSheet.Cells.Item(1, 2) = DateTimePicker1
    xlSheet.Cells.Item(1, 3) = MyType
    xlSheet.Cells.Item(1, 4) = MyJobNumber
    xlSheet.Cells.Item(1, 5) = MyJobName
    xlSheet.Cells.Item(1, 6) = MyScope
    xlSheet.Cells.Item(1, 7) = MyClient
    xlSheet.Cells.Item(1, 8) = MyPrincipal

    I need to somehow replace the (1, 1) = with something like the (Lastrow, 1). So I can keep running my code and appending to new lines in the code.

    So the code you provided I just don't know where it needs to go relative to my code. Sorry to keep bugging, this is all just a lot of reference type stuff and declared variables which I'm getting lost in.

    Thanks for any help!!

  13. #13
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: [2005] VB.net write to Excel?

    I've edited your thread title to include the version that you specified in your message body. This helps people that want to help you know what sort of coding advice to provide.

    Please use the radio buttons provided to specify what version you are using when creating new threads in the future.

    Thanks.

  14. #14
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: [2005] VB.net write to Excel?

    Did you add a reference to MS Excel?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  15. #15

    Thread Starter
    Lively Member
    Join Date
    Mar 2006
    Posts
    126

    Re: [2005] VB.net write to Excel?

    Sorry about the version. I'll do that next time!!

    As far as the reference. I'm pretty sure I have the reference to MS Excel set because it writes to an excel file correctly. Just keeps writing over the same line over and over again?

  16. #16
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: [2005] VB.net write to Excel?

    I dont see where your referencing the last row to write to?

    .Cells.SpecialCells(xlCellTypeLastCell).Row
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  17. #17
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: [2005] VB.net write to Excel?

    Try referencing the entire class structure for the constant.
    VB Code:
    1. .Cells.SpecialCells(Excel.XlCellType.xlCellTypeLastCell).Row
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  18. #18

    Thread Starter
    Lively Member
    Join Date
    Mar 2006
    Posts
    126

    Re: [2005] VB.net write to Excel?

    Ok, so try?

    'xlSheet.Cells.SpecialCells(Excel.XlCellType.xlCellTypeLastCell).Row = Myname

    ?

    Or replace my code with

    .Cells.SpecialCells(Excel.XlCellType.xlCellTypeLastCell).Row = Myname ?

    I don't really know where to place this portion?

    Sorry, I'm still really new to all of this..

    Mark

  19. #19

    Thread Starter
    Lively Member
    Join Date
    Mar 2006
    Posts
    126

    Re: [2005] VB.net write to Excel?

    All right!! I've gotten it to find the last row used, set a counter and increment it by one to find the next available line to write to.

    Now my final question I'm hoping someone can help me with and I'll be all done.

    In the last row of information I have a job number stored in the first column. What I want to do is increment the number in the first column by one? Is it possible to get the information contained in a designated cell. Such as cell A6? I know how to string together the row, and "A" but not sure how to pull this from the excel file and store it as a variable?

    Thanks for any help!!

  20. #20
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: [2005] VB.net write to Excel?

    This is a way to reference cell A + whatever your last row is.
    VB Code:
    1. MessageBox.Show(wksht.Cells(1,wksht.Cells.SpecialCells(xlCellTypeLastCell).Row).Value.ToString)
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  21. #21

    Thread Starter
    Lively Member
    Join Date
    Mar 2006
    Posts
    126

    Re: [2005] VB.net write to Excel?

    Is there some special way to get the (xlCellTypeLastCell) to work? Ive tried all the examples before that had this string of code and they all say "(xlCellTypeLastCell)" not declared? This time its doing it again? I've got excel object 11.0 referenced? And everything else seems to be declared all right but this?

    Thanks again!!

  22. #22
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: [2005] VB.net write to Excel?

    Sorry, forgot to fully qualify it.
    VB Code:
    1. MessageBox.Show(wksht.Cells(1,wksht.Cells.SpecialCells(Excel.XlCellType.xlCellTypeLastCell).Row).Value.ToString)
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  23. #23

    Thread Starter
    Lively Member
    Join Date
    Mar 2006
    Posts
    126

    Re: [2005] VB.net write to Excel?

    Thanks Rob for the help, but still not getting my cell contents?

    When I add the following code I get the return value of "System._ComObject" in the message box?

    Here is the code I'm at, maybe this will help?

    Thanks for helping!!

    VB Code:
    1. Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
    2.  
    3.         Dim Myexcel2 As Excel.Application
    4.         Myexcel2 = CType(CreateObject("Excel.Application"), Excel.Application)
    5.  
    6.             Myexcel2.Workbooks.Open("C:\JobInformationResidential.xls")
    7.  
    8.         Myexcel2.Visible = True
    9.  
    10.         Dim mycount As Long
    11.  
    12.             Dim CurRow As Long
    13.  
    14.             Dim MyExcel As Excel.Application
    15.             Dim MySht As Excel.Worksheet
    16.             MyExcel = CType(GetObject(, "Excel.Application"), Excel.Application)
    17.             MySht = CType(MyExcel.ActiveSheet, Excel.Worksheet)
    18.  
    19.             Dim wksht As Excel.Worksheet
    20.             wksht = CType(Myexcel2.ActiveSheet, Excel.Worksheet)
    21.  
    22. 'Gets last row
    23.         mycount = wksht.UsedRange.Rows.Count
    24.         CurRow = mycount + 1
    25.  
    26.         MessageBox.Show(wksht.Cells(1, wksht.Cells.SpecialCells(Excel.XlCellType.xlCellTypeLastCell).Row).ToString)
    27.  
    28.  
    29.         Dim MyJobNumber As String
    30.         Dim MyPrincipal As String
    31.  
    32.         MyJobNumber = CStr(ListBox1.Text)
    33.         MyPrincipal = CStr(ComboBox7.Text)
    34.        
    35.         wksht.Cells(CurRow, 1) = MyJobNumber
    36.         wksht.Cells(CurRow, 2) = MyPrincipal
    37.        
    38.         Myexcel2.ActiveWorkbook.Close(True)
    39.  
    40.         End
    41.     End Sub

  24. #24
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: [2005] VB.net write to Excel?

    Thats because you forgot to add the .Value.ToString part like I have in my posted code in #22.

    Why are you using 2 Excel.Applications? It looks like your using the same active sheet in two objects. The last cell will give you the next cell after the last used one.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  25. #25

    Thread Starter
    Lively Member
    Join Date
    Mar 2006
    Posts
    126

    Re: [2005] VB.net write to Excel?

    Ok, fixed up the two excel applications. Not sure what I was thinking there? This makes more sense.

    As far as the string of code. When I add in the line of code you posted I get an error shown..

    MessageBox.Show(wksht.Cells(1, wksht.Cells.SpecialCells(Excel.XlCellType.xlCellTypeLastCell).Row).Value.ToString) under the lines underlined and the error is "Option Strict On disallows late binding"? Any idea what this means? But once I chop out the Value part the error message goes away, but of course the code doesn't do anything?

    I know I'm doing something wrong?

  26. #26
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: [2005] VB.net write to Excel?

    Thats because of using "Option Strict On" but this is a good thing. We just need to CType it to a number.
    VB Code:
    1. MessageBox.Show(wksht.Cells(1, CType(wksht.Cells.SpecialCells(Excel.XlCellType.xlCellTypeLastCell).Row).Value.ToString, Integer).Value.ToString)
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  27. #27

    Thread Starter
    Lively Member
    Join Date
    Mar 2006
    Posts
    126

    Re: [2005] VB.net write to Excel?

    Hmmm... Ok new errors?

    MessageBox.Show(wksht.Cells(1, CType(wksht.Cells.SpecialCells(Excel.XlCellType.xlCellTypeLastCell).Row).Value.ToString, Integer).Value.ToString)

    At Integer). I get "." expected

    Then at Integer) I get "Integer" is a type and cannot be used as an expression.

    Then at .Row) . I get "Syntax error in cast operator; two arguments separated by a comma are requsted?

    ????

  28. #28
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: [2005] VB.net write to Excel?

    Ok, try this for getting the last cells contents under Option Strict On.
    VB Code:
    1. Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
    2.         Dim oWB As Excel.Workbook = moApp.Workbooks.Add
    3.         Dim oSht As Excel.Worksheet = DirectCast(oWB.Sheets("Sheet1"), Excel.Worksheet)
    4.         Dim rng As Excel.Range
    5.         Dim i As Integer
    6.         oSht.Cells(10, 1) = "Test"
    7.         i = oSht.Cells.SpecialCells(Excel.XlCellType.xlCellTypeLastCell).Row
    8.         rng = oSht.Cells.Range(oSht.Cells(i, 1), oSht.Cells(i, 1))
    9.         MessageBox.Show(rng.Value.ToString) 'Test
    10.     End Sub
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  29. #29

    Thread Starter
    Lively Member
    Join Date
    Mar 2006
    Posts
    126

    Re: [2005] VB.net write to Excel?

    Ok found some code that got it to work for me...

    Thanks Rob for all the help!! Sorry I was such a pain with trying to figure this out!!

    VB Code:
    1. Dim wksht As Excel.Worksheet
    2.         wksht = CType(Myexcel.ActiveSheet, Excel.Worksheet)
    3.         mycount = wksht.UsedRange.Rows.Count
    4.         CurRow = mycount + 1
    5.  
    6.  
    7.         Dim wkRange As Excel.Range
    8.         Dim lProjNum As String
    9.  
    10.         wkRange = CType(wksht.Cells(mycount, 1), Excel.Range)
    11.         lProjNum = CStr(CLng(wkRange.Value) + 1)

  30. #30
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: [2005] VB.net write to Excel?

    NP, yes its the Range object that we both used that was the key to getting around the Strict declaration.

    ps, dont forget to Resolve the thread if there is nothing more on this issue.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width