Results 1 to 32 of 32

Thread: Problem Killing excel Object...Used to be Access and Excel VBA intermingling

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jun 2004
    Posts
    28

    Problem Killing excel Object...Used to be Access and Excel VBA intermingling

    Hey everyone,

    I got this button in access that exports data to a existing excel spreadsheet on my desktop. After it does this I need to run a macro to format the newly exported data. I was wondering if I could perhaps refine this process so that after I push the button in access it

    1) Will autocreate a blank excel file
    2) Export the data to this file [ I have this code already]
    3) Open the newly created file
    4) Run a Excel "Macro" to format the data [I have the code to format the data, just need to know if and how its possible to execute this code from access cause if we are creating a new excel sheet then there can be no predefinded macros, right?]

    So in essence i need to figure out if i can play around with excel functions from within access VBA, If anyone can help me out with this id highly appreciate it.


    Thanks

    Doc
    Last edited by doctor; Jul 9th, 2004 at 11:10 AM.

  2. #2
    PowerPoster Dave Sell's Avatar
    Join Date
    Mar 2004
    Location
    /dev/null
    Posts
    2,961
    If you are suggesting to begin this process from Access:

    Can you run an .exe from a macro in Access? If so, the you could easily do all this by writing a VB app to manipulate the finished excel spreadsheet.

    The Access macro could do its export thing and then run the VB .exe second.

    You would need Visual Studio for this.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jun 2004
    Posts
    28
    Yea, thats what im doing now, i got a Access form button that exports the data, and then a macro in excel that formats the data. It takes two steps and i need to formulate it down to one.

  4. #4
    PowerPoster Dave Sell's Avatar
    Join Date
    Mar 2004
    Location
    /dev/null
    Posts
    2,961
    What I'm saying is you can take the functionality from your Excel macro and put it in an executable created in VB.

    Access could do the export in a macro, and also run the VB executable in the same macro.

    Does this help?

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Jun 2004
    Posts
    28
    Yeah .. Now i see what your saying... sounds like a good plan, how can i go about doing that.. ? Im looking for a 'make exe' in the vba editor but cant find it.. and how would access exec the export macro and the exe i create..

    THX

  6. #6
    PowerPoster Dave Sell's Avatar
    Join Date
    Mar 2004
    Location
    /dev/null
    Posts
    2,961
    Originally posted by doctor
    Yeah .. Now i see what your saying... sounds like a good plan, how can i go about doing that.. ? Im looking for a 'make exe' in the vba editor but cant find it.. and how would access exec the export macro and the exe i create..

    THX
    As far as I know, there is no way to make a .exe from the VBA editor. You need Visual Studio, and Visual Basic (I use version 6) to make executables.

    Dave

  7. #7
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343
    Cut n paste the Excel code into a new module in Access.
    Change the references and obtain the correct ones as required using automation (getobject etc).

    No need to go to exe... which yes you can run.


    Vince

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Jun 2004
    Posts
    28

    Angry

    app2.Quit
    Set app2 = Nothing


    Is this the proper way to close the application object that i set via


    Set app2 = New Excel.Application

    Cause after everything is all said and done EXCEL is still a process running in the back ground... why??

  9. #9
    PowerPoster Dave Sell's Avatar
    Join Date
    Mar 2004
    Location
    /dev/null
    Posts
    2,961
    First close the Workbook

    VB Code:
    1. m_wkbReportBook.Close True, OUTPUTEXCELFILE
    2.     Set m_wkbReportBook = Nothing
    3.     '
    4.     m_appExcel.Quit
    5.     Set m_appExcel = Nothing

    I never have a dangling process when this executes properly.

    You should be warned that if your program crashes it could leave a dangling Excel process out there. If that occurs, you must kill it before running it again. I have had alot of problems getting my code to work when a dangling Excel proc was out there.

    Just be sure to kill all Excel procs before running the code anew.

    Dave

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Jun 2004
    Posts
    28
    m_appExcel is your application object so

    m_appExcel.Quit quits the application, right?


    and What is OUTPUTEXCELFILE?

  11. #11
    PowerPoster Dave Sell's Avatar
    Join Date
    Mar 2004
    Location
    /dev/null
    Posts
    2,961
    Originally posted by doctor
    m_appExcel is your application object so

    m_appExcel.Quit quits the application, right?
    Yes.

    and What is OUTPUTEXCELFILE?
    A constant like "C:\Temp\report.xls"

  12. #12

    Thread Starter
    Junior Member
    Join Date
    Jun 2004
    Posts
    28
    How are you initializeing your workbook m_wkbReportBook?

  13. #13
    PowerPoster Dave Sell's Avatar
    Join Date
    Mar 2004
    Location
    /dev/null
    Posts
    2,961
    Its not a sheet - its a Workbook:

    VB Code:
    1. ' Create a new Excel application
    2.     Set m_appExcel = New Excel.Application
    3.     m_appExcel.SheetsInNewWorkbook = 24
    4.     Set m_wkbReportBook = m_appExcel.Workbooks.Add
    5.     m_appExcel.SheetsInNewWorkbook = 3

  14. #14

    Thread Starter
    Junior Member
    Join Date
    Jun 2004
    Posts
    28
    hmm.. i tried that .. didnt seem to work either... any other

  15. #15
    PowerPoster Dave Sell's Avatar
    Join Date
    Mar 2004
    Location
    /dev/null
    Posts
    2,961
    Not sure if this helps, but these are my defs:

    VB Code:
    1. Private m_appExcel As Excel.Application
    2. Private m_wkbReportBook As Excel.Workbook

  16. #16

    Thread Starter
    Junior Member
    Join Date
    Jun 2004
    Posts
    28

    Unhappy

    hmmm nope I thought that would help but still didnt kill the excel process... this is getting weired.. ive tried a few different things and none seem to be working .. im all ears to try somthing else if you can think of anything...

  17. #17

    Thread Starter
    Junior Member
    Join Date
    Jun 2004
    Posts
    28
    http://support.microsoft.com/default...;EN-US;Q317109


    Thats the link that describes my problem, basically im using vb in access and need to do some stuff in excel. I make an excel object and then have trouble exiting the app.. the app performs flawlessly BUT when i go to quit it doesnt terminate excel. I tried doing what they suggested but when i get a error "object variable or with block variable not set" for this line..
    wkBook = app.Workbooks

    what does that mean?

  18. #18
    PowerPoster Dave Sell's Avatar
    Join Date
    Mar 2004
    Location
    /dev/null
    Posts
    2,961
    Try:

    VB Code:
    1. Set wkBook = app.Workbooks

  19. #19

    Thread Starter
    Junior Member
    Join Date
    Jun 2004
    Posts
    28
    hmm, now i get a type mismatch??

  20. #20
    PowerPoster Dave Sell's Avatar
    Join Date
    Mar 2004
    Location
    /dev/null
    Posts
    2,961
    OK try my code:

    VB Code:
    1. Set m_wkbReportBook = m_appExcel.Workbooks.Add

  21. #21

    Thread Starter
    Junior Member
    Join Date
    Jun 2004
    Posts
    28
    nnnope... oh boy....

  22. #22

    Thread Starter
    Junior Member
    Join Date
    Jun 2004
    Posts
    28
    sorry should be more specific... it made the error go away but still a NoGo on the killing excel

  23. #23
    PowerPoster Dave Sell's Avatar
    Join Date
    Mar 2004
    Location
    /dev/null
    Posts
    2,961
    Well if you are running this code from an Access module there is a possibility that it won't work correctly. It may be your only recourse is to compile your VBA in a VB executable. Is that an option for you?

  24. #24

    Thread Starter
    Junior Member
    Join Date
    Jun 2004
    Posts
    28
    sure, an executable is def a option for me, how much do i have to modify my current code to pull this off? and do you think you could walk me through or send me a faq or tutorial that would show me how to do this?

    BTW, since the last post (sorry should have updated) i got the error to go away BUT now the excel process sits there after program completion. Im sure the sequence of killing the process is right because i checked a few different sources of code. Im hoping that making this a executable may solve my problem.



    Thanks!

  25. #25
    PowerPoster Dave Sell's Avatar
    Join Date
    Mar 2004
    Location
    /dev/null
    Posts
    2,961
    Im at work so I cant go into too much detail now, but ya the VB code is very similar to the VBA code. The main thing is getting the visual COM object added to your project, and then declareing the objects and so forth.

    I will see if I can come up with a breif tutorial tonight.

    Dave

  26. #26

    Thread Starter
    Junior Member
    Join Date
    Jun 2004
    Posts
    28
    OK dave, Thanks a lot for your help btw im playing around with this thing and i set excel to be visible... i see all the data get manipulated and in the end the excell app quits.. i can see it dissapear.. but the process is still there. Dont know if that helps, just an observation. I understand you are doing this on your own free time so whenever youre free to shoot some code my way go for it Ill keep you updated on things just so you know if i have any breakthroughs

    Doc

  27. #27

    Thread Starter
    Junior Member
    Join Date
    Jun 2004
    Posts
    28
    Heey I fixed my problem ... i wasnt appending the excel object to my methods and apparently thats why it was not terminating the process. I am still interested in learning how to make a exceutable of all this, if you are still up for it )

    Thanks for your time and effort

    -Doc

  28. #28
    PowerPoster Dave Sell's Avatar
    Join Date
    Mar 2004
    Location
    /dev/null
    Posts
    2,961
    I'm definately up for it. There was so few documentation on the topic when I started into it, it should be done.

  29. #29

  30. #30

    Thread Starter
    Junior Member
    Join Date
    Jun 2004
    Posts
    28
    good stuff!

  31. #31
    PowerPoster Dave Sell's Avatar
    Join Date
    Mar 2004
    Location
    /dev/null
    Posts
    2,961
    Oops looks like I overlooked the part about making the VB project into an executable.

    Do you know how to do that part? I guess I left that out of the tutorial.

  32. #32

    Thread Starter
    Junior Member
    Join Date
    Jun 2004
    Posts
    28
    Ah yes, I took it from where you left off, I do know how to make it into a exce, but I didnt once So you may want to include it for ppl who dont know how to do it..

    thanks again
    -Doc

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