Results 1 to 16 of 16

Thread: Call a macro in an opened excel file

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jun 2012
    Posts
    24

    Call a macro in an opened excel file

    So i want to be able to start an excel macro in a file that is already opened by a user. Like a user would open the excel file and look at it, but when there is new data, my vb.net program will run, call out the macro in the opened file and update the excel file in front of the user. Is this something that is possible?

    Every time I try to get this done it runs the macro of the opened file but doesn't update it. Instead it saves it over the excel file, so you have to close the excel file and reopen it to see the new data. But I don't want to have to do that.

    Here is my code used for this instance:

    Dim xl As New Excel.Application
    Dim oTargetWb As Excel.Workbook
    oTargetWb = xl.Workbooks.Open(fileTest)
    Try
    Dim F As Short = FreeFile()
    FileOpen(F, fileTest, OpenMode.Binary, OpenAccess.ReadWrite, OpenShare.LockReadWrite)
    FileClose(F)
    Catch
    xl.Run("test2")
    End Try


    Any suggestions?

  2. #2
    New Member
    Join Date
    Jan 2013
    Posts
    13

    Re: Call a macro in an opened excel file

    Hello,

    I am using something similar but it opens the file in a new instance of excel and runs the macro there. Once it finishes i have 2 files open.

    Also i cannot run a macro from other workbook and i guess it's related to this as well.

    i am using
    vb.net Code:
    1. Dim oXL As New Excel.Application
    2.         Dim oWB As Excel.Workbook
    3.         'Dim oRng As Excel.Range
    4.         oXL = New Excel.Application
    5.         oXL.Visible = True
    6.         oWB = oXL.Workbooks.Open("C:\Users\DealExport.xlsx")
    7.  
    8.  
    9.         oXL.Run("RunMeee")
    10.      
    11.         oXL = Nothing
    12.         oWB = Nothing

    If the macro is in a different file i get an error it is nonexistent even if i try with this:

    oXL.Run("C:\Users\xxxxx\AppData\Roaming\Microsoft\Excel\XLSTART\PERSONAL.XLSB!RunMeee")

    So some help would be nice to run it on an already open workbook.

    Sorry to post here but i think it's related.

    Thank you.

  3. #3
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Call a macro in an opened excel file

    Quote Originally Posted by mh1349 View Post
    So i want to be able to start an excel macro in a file that is already opened by a user. Like a user would open the excel file and look at it, but when there is new data, my vb.net program will run, call out the macro in the opened file and update the excel file in front of the user. Is this something that is possible?
    Which file format are you using?
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  4. #4
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Call a macro in an opened excel file

    Quote Originally Posted by danutzdobre View Post
    Hello,

    I am using something similar but it opens the file in a new instance of excel and runs the macro there. Once it finishes i have 2 files open.

    Also i cannot run a macro from other workbook and i guess it's related to this as well.
    As .xlsx is the macro-free format is it possible that the second application is being run to give you a version in which you could save the macro as part of the file?
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Jun 2012
    Posts
    24

    Re: Call a macro in an opened excel file

    I am using .xlsm if that's what you meant

  6. #6
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Call a macro in an opened excel file

    It is! I can have a little play now!
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  7. #7
    New Member
    Join Date
    Jan 2013
    Posts
    13

    Re: Call a macro in an opened excel file

    Quote Originally Posted by dunfiddlin View Post
    As .xlsx is the macro-free format is it possible that the second application is being run to give you a version in which you could save the macro as part of the file?
    I edited the code a little bit. the file i am using is .xlsb
    The original one is .xlsx and i need to run the macro from another file like PERSONAL.XLSB.

    To solve mh1349's problem we need to replace the "oTargetWb = xl.Workbooks.Open(fileTest)" with something else i guess, to just activate an open window instead of opening it again and saving it after running.

    Is there a way to just run a macro without having to open a file and then save it? Just open like it would run directly from excel.

    Danut

  8. #8
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Call a macro in an opened excel file

    Quote Originally Posted by mh1349 View Post
    I am using .xlsm if that's what you meant
    Ok, I've run a few tests and I'm afraid I'm just not seeing what you are. The macros run and update without problems. So unless there's something specific to the macro you're running I don't know what to suggest.
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  9. #9
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Call a macro in an opened excel file

    Quote Originally Posted by danutzdobre View Post

    Is there a way to just run a macro without having to open a file and then save it? Just open like it would run directly from excel.
    I'm not sure I really understand the question in that form but it's certainly possible to have the Excel Application persist simply by declaring it with form or global scope. So unless you actually need two workbooks open (and if you're going to use a macro from one in the other you probably do) you can certainly do everything in one application instance. As I've suggested in my latest answer, running macros doesn't require saving and reopening and should update in the present instance.
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Jun 2012
    Posts
    24

    Re: Call a macro in an opened excel file

    Quote Originally Posted by dunfiddlin View Post
    Ok, I've run a few tests and I'm afraid I'm just not seeing what you are. The macros run and update without problems. So unless there's something specific to the macro you're running I don't know what to suggest.
    Everything works fine when the excel file that has the macro in it is closed. But when I have it opened, it runs the macro in the background and doesn't update the file I have opened. Are you saying you had an excel file open then ran the vb program and it worked?

  11. #11
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Call a macro in an opened excel file

    I am. Sorry.
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  12. #12

    Thread Starter
    Junior Member
    Join Date
    Jun 2012
    Posts
    24

    Re: Call a macro in an opened excel file

    So it did work for you while you had the file open?

  13. #13
    New Member
    Join Date
    Jan 2013
    Posts
    13

    Re: Call a macro in an opened excel file

    Isn't this line oWB = oXL.Workbooks.Open("C:\Users\DealExport.xlsb") opening the file? Is there any way i can run the macro without this open command?
    Last edited by danutzdobre; Feb 28th, 2013 at 04:27 PM.

  14. #14

    Thread Starter
    Junior Member
    Join Date
    Jun 2012
    Posts
    24

    Re: Call a macro in an opened excel file

    Dim fileTest As String = "N:\Temp\template.xlsm"

    Try
    Dim F As Short = FreeFile()
    FileOpen(F, fileTest, OpenMode.Binary, OpenAccess.ReadWrite, OpenShare.LockReadWrite)
    FileClose(F)
    Catch
    Dim xl As Excel.Application = Marshal.GetActiveObject("Excel.Application")
    xl.Visible = True
    xl.Run("test2")
    Exit Sub
    End Try

    This is what finally worked for me. Because it finds the currently run excel object (being my already opened excel file) then runs its macro.

  15. #15
    New Member
    Join Date
    Jan 2013
    Posts
    13

    Re: Call a macro in an opened excel file

    Glad it worked for you

    For me is not working yet.
    I have the excel macro in a file "Macro.xlsb" and i want to run it in file "DealExport.xlsx".

    I open DealExport.xlsx and then i need to run the macro. How do i do that? Even if i have both files open it does not work.

  16. #16
    New Member
    Join Date
    Jan 2013
    Posts
    13

    Re: Call a macro in an opened excel file

    Any help here? Do i need to start a new thread?

Tags for this Thread

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