Results 1 to 11 of 11

Thread: [RESOLVED] RESOLVED: Runtime error 1004 when exporting to excel

  1. #1

    Thread Starter
    Member
    Join Date
    Nov 2005
    Posts
    44

    Resolved [RESOLVED] RESOLVED: Runtime error 1004 when exporting to excel

    Hi, my application is getting the followed error

    Runtime error 1004: Application defined or Object defined error

    and this is the offending piece of code.

    myBook.Worksheets(1).Range(bCol).Value = vDate

    vDate is a string, and I have checked bCol as well to make sure that they have correct variables.

    Any help much appreciated...
    Last edited by Santos7772005; Dec 16th, 2005 at 12:48 AM. Reason: Resolved

  2. #2
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Runtime error 1004 when exporting to excel

    Post more code than that. It doesn't sound like Excel is being opened correctly.

  3. #3
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: Runtime error 1004 when exporting to excel

    What is the format of the column where that data is going?
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

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

    Re: Runtime error 1004 when exporting to excel

    Post the values of your variables too.

    Moved from Classic VB.
    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
    Member
    Join Date
    Nov 2005
    Posts
    44

    Red face Re: Runtime error 1004 when exporting to excel

    Yes, excel is doing something strange, it does not seem to be closing even after my application has quit.

    the runtime error problem I fixed by changing vDate to a type String but the problem seems to be with the spreadsheet either not opening or closing properly. If I close out of Vb and then try to open the spreadsheet I get a message that a user (me!) has it open and it can only be opened readonly so its like it has not closed properly.


    how I have declared my Excel workbook:

    Dim myBook As Excel.Workbook

    I open it like this:

    fname = exePath & "exportXL.xls"
    Set myBook = Workbooks.Open(FileName:=fname)

    When I have finished writing the data to the spreadsheet I close it like this:

    myBook.Save
    myBook.Close
    Set myBook = Nothing

    Hope this provides a few more clues.

  6. #6
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: Runtime error 1004 when exporting to excel

    Will you try it like this...

    VB Code:
    1. Public Sub Test()
    2. Dim exl         As Excel.Application
    3. Dim myBook      As Excel.Workbook
    4. Dim fName       As String
    5.  
    6.     Set exl = New Excel.Application
    7.     fName = exePath & "exportXL.xls"
    8.     Set myBook = exl.Workbooks.Open(fName)
    9.     'stuff
    10.     myBook.Save
    11.     myBook.Close
    12.     Set myBook = Nothing
    13.     exl.Quit
    14.     Set exl = Nothing
    15. End Sub
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

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

    Re: Runtime error 1004 when exporting to excel

    Using Dee-u's code will work or specifically, this line of code is causing excel to remain open as your using the default Workbooks object without referencing an object variable set to it first.
    VB Code:
    1. Set myBook = [b]Workbooks[/b].Open(FileName:=fname)
    2. 'Better to do...
    3. Dim oApp As Excel.Application
    4. Set oApp = New Excel.Application
    5. Set myBook = oApp.Workbooks.Open(FileName:=fname)
    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

  8. #8

    Thread Starter
    Member
    Join Date
    Nov 2005
    Posts
    44

    Re: Runtime error 1004 when exporting to excel

    Hey thanks for the help guys. Appreciate it, seems to have cleared up my problem

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

    Re: Runtime error 1004 when exporting to excel

    Glad it worked out.

    Ps, as a new member I'll let you know that when you have your thread answered its courteous to mark your thread as Resolved so other members will know its solved.
    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
    Member
    Join Date
    Nov 2005
    Posts
    44

    Re: RESOLVED: Runtime error 1004 when exporting to excel

    Thanks for the heads up

  11. #11
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: RESOLVED: Runtime error 1004 when exporting to excel

    We are glad to be of help.
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

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