Results 1 to 10 of 10

Thread: 2 Instances of Excel Object using 2 VB Projects @ same time

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Mar 2005
    Location
    Toronto, Canada
    Posts
    28

    2 Instances of Excel Object using 2 VB Projects @ same time

    Hi,

    I am using VB 6.0 to automate some reports in excel. For every report there is a new VB project/VB executable.

    My Program does following;
    Creating a reference to Excel.Application object
    Opening an excel template
    Putting data in excel worksheets
    Saving excel workbook with different name
    Closing excel workbook

    Problem
    When two of my executables run at the same time, since they both are referencing excel object, they got failed, none of them is able to create excel report.

    Please let me know if there is any workaround otherwise we may use threads in VB 6 or we can also move on to the .NET. But before that we need to find some workaround in VB 6 other than threading.

    Thanks a lot guys,
    M.Ahmar

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: 2 Instances of Excel Object using 2 VB Projects @ same time

    Hi ahmarshi,

    Having two applications using Excel automation at the same time should not be a problem (I've had several running at the same time before), but I'm afraid without seeing your code I cant comment on why this would be happening.

    Several people have problems with Excel staying open after they think it should close, unfortunately it is usually due to a problem with their code (but this can be hard to spot if you don't know what you are looking for!); I presume that this would be a similar sort of issue.

    If you post your code I can hopefully provide the solution

    I have just one question from what you have posted so far.. do the different reports use the same template?

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

    Re: 2 Instances of Excel Object using 2 VB Projects @ same time

    Yes, we need to see code, but if you are using the GetObject function you could possibly be attaching to the same instance of Excel as your other program(s). Also, if your displaying a SaveAs dialog box and then another workbook tries to save at the same time, via code, it could potentially be the issue.

    Just something to think about
    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

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Mar 2005
    Location
    Toronto, Canada
    Posts
    28

    Re: 2 Instances of Excel Object using 2 VB Projects @ same time

    Please, see my code here under. As you have said that it is possible in VB, I will also try myself to write safe code.

    Answer To Your Question
    No, they all have different templates.

    If you can help, it will be appreciated.

    Thanks & Best Regards,
    M.Ahmar

    My Code

    Public xlAppTemp As Excel.Application

    Public Sub GenerateReport()
    On Error GoTo ErrHandler

    ' Creating Object for Excel File.....
    Set xlAppTemp = New Excel.Application

    ' Making it Invisible and non-Interactive.....
    xlAppTemp.Visible = False
    xlAppTemp.DisplayAlerts = False

    ' Opening Template Excel File.....
    xlAppTemp.Workbooks.Open strTemplatePath & "MyReport.xls", , False

    ' Making Active to Worksheet 1.....
    xlAppTemp.Sheets(1).Activate

    ' I am doing lot of things in it, but to provide you with example
    xlAppTemp.ActiveSheet.Cells(1,1) = "This is my report 1"

    ' Formating Date to attach with new file name.....
    strDate = Format(Date, "yyyy-mm-dd")

    ' Saving excel file with new name on different folder.....
    xlAppTemp.ActiveSheet.Parent.SaveAs strResultPath & "MyReport\MyReport" & strDate & ".xls"

    Workbooks(1).Close
    Cleanup:
    ' Destroying Objects.....
    Set xlAppTemp = Nothing
    End Sub
    Last edited by ahmarshi; Sep 9th, 2005 at 09:31 AM.

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

    Re: 2 Instances of Excel Object using 2 VB Projects @ same time

    To help clarify for Excel to know which workbook your referring to you should refer to it by name and the same with the sheets. Dont use ActiveSheet. But the problem is that if you run two instances of your program, you should have two instances of Excel application showing in your Taskmanager?
    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

  6. #6
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: 2 Instances of Excel Object using 2 VB Projects @ same time

    I totally agree, and have noticed another issue (which could be causing the problems) - you have Workbooks(1).Close without reference to the parent object (xlAppTemp). This can cause various issues, including the Excel application refusing to close when your program is finished.

    You also do not close your objects properly, simply setting the application object to Nothing does not close it - this just empties your variable. If you look in Task Manager you will probably see one copy of Excel.exe for each time that you have run your code.

    Here is a modified version of your code to correct these issues (you should make the same changes in the code you have not shown):
    VB Code:
    1. Public xlAppTemp As Excel.Application
    2. Public xlWorkBook as Excel.Workbook
    3. Public xlSheet as Excel.Worksheet
    4.  
    5. Public Sub GenerateReport()
    6.   On Error GoTo ErrHandler
    7.  
    8.   ' Creating Object for Excel File.....
    9.   Set xlAppTemp = New Excel.Application
    10.  
    11.   ' Making it Invisible and non-Interactive.....
    12.   xlAppTemp.Visible = False
    13.   xlAppTemp.DisplayAlerts = False
    14.  
    15.   ' Opening Template Excel File.....
    16.   Set xlWorkBook = xlAppTemp.Workbooks.Open (strTemplatePath & "MyReport.xls", , False)
    17.   Set xlSheet = xlWorkBook.Sheets(1)
    18.  
    19.   ' Making Active to Worksheet 1.....
    20.   xlSheet.Activate
    21.  
    22.   ' I am doing lot of things in it, but to provide you with example
    23.   xlSheet.Cells(1,1) = "This is my report 1"
    24.  
    25.   ' Formating Date to attach with new file name.....
    26.   strDate = Format(Date, "yyyy-mm-dd")
    27.  
    28.   ' Saving excel file with new name on different folder.....
    29.   xlWorkBook.SaveAs strResultPath & "MyReport\MyReport" & strDate & ".xls"
    30.  
    31. Cleanup:
    32.   ' Destroying Objects.....
    33.   Set xlSheet = Nothing
    34. 'I presume this section comes after ErrHandler, in which case you will want to close the workbook without changes.
    35. '(save happens just above if no error occurs)
    36.   xlWorkBook.Close SaveChanges:=False
    37.   Set xlWorkBook = Nothing
    38.  
    39. 'The Visible and DisplayAlerts settings should be reset, as they can affect 'manual' use of Excel too.
    40.   xlAppTemp.Visible = True
    41.   xlAppTemp.DisplayAlerts = True
    42.  
    43.   xlAppTemp.Close
    44.   Set xlAppTemp = Nothing
    45. End Sub

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Mar 2005
    Location
    Toronto, Canada
    Posts
    28

    Re: 2 Instances of Excel Object using 2 VB Projects @ same time

    Thanks guys,

    Actually, I figured this thing out few minutes ago.

    Thanks for your help.
    Mo

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

    Re: 2 Instances of Excel Object using 2 VB Projects @ same time

    This is incorrect:
    VB Code:
    1. xlAppTemp.Close

    should be
    VB Code:
    1. xlAppTemp.Quit

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

    Re: 2 Instances of Excel Object using 2 VB Projects @ same time

    But as you can see dglienna, it is a minor issue as ahmarshi posted he was able to figure it out.
    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
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: 2 Instances of Excel Object using 2 VB Projects @ same time

    Well, so did I
    Just in case others look at it.

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