none
Can Word 2007 VBA use CreateObject("Excel.Application") to start Excel in SafeMode? RRS feed

  • Question

  • I have a VBA module in a Word 2007 .docm document that uses CreateObject("Excel.Application") when it needs to use Excel but finds that it is not currently running (i.e. GetObject("Excel.Application") has errored).

    This works fine except that whenever Excel starts up on my PC it loads with it a massive commercial application Add-in that I do not actually need at this time.

    In order to greatly reduce the load-up time for Excel I would like to emulate the "/s" switch (for Safe Mode - i.e. no Ad-ins) on starting it.

    Is this possible?  And, if so, how?

    Any and all offers of advice or help will, as ever, be gratefully recieved.

    Paul J

    Friday, March 18, 2011 11:50 AM

All replies

  • You can try something like this:

    Private Declare Sub Sleep Lib "kernel32" _
      (ByVal dwMilliseconds As Long)
    
    
    Sub StartExcel()
    
      Dim objExcel As Object
    
      On Error Resume Next
      
      Set objExcel = GetObject("Excel.Application")
      
      If Err.Number <> 0 Then
        
        Set objWshShell = CreateObject("WScript.Shell")
        objWshShell.Run "Excel.exe /s"
        
        Do While Not objExcel Is Nothing
          Sleep 250
          Set objExcel = GetObject("Excel.Application")
        Loop
        
      End If
      
      On Error GoTo 0
      
      objExcel.Visible = True
    
    End Sub
    
    

     

    You can change the sleep period and also limit the loop iterations trying to get Excel.Application object. For example, if GetObject doesn't work in the fifth iteration, restore normal error handling and use CreateObject.


    Uros Calakovic
    Saturday, March 19, 2011 4:11 PM
  • Hi Uros,

    Thank you for your suggestion (above).

    Whilst this macro does, indeed, load Excel up much faster than before it does have two major drawbacks:

    - When Excel is loaded it is visible (even when the /e switch is added) and takes focus as the active application;

    - Because Excel has been started independently by Windows (by the shell script not Word) my macros cannot make it quit.

    Thanks again anyway.

    Paul J

     

    DOES ANYONE ELSE HAVE ANY SUGGESTIONS, PLEASE?

    Sunday, March 20, 2011 8:16 AM
  • I have a VBA module in a Word 2007 .docm document that uses CreateObject("Excel.Application") when it needs to use Excel but finds that it is not currently running (i.e. GetObject("Excel.Application") has errored).

    This works fine except that whenever Excel starts up on my PC it loads with it a massive commercial application Add-in that I do not actually need at this time.

    In order to greatly reduce the load-up time for Excel I would like to emulate the "/s" switch (for Safe Mode - i.e. no Ad-ins) on starting it.

    Is this possible?  And, if so, how?

    Any and all offers of advice or help will, as ever, be gratefully recieved.

    Paul J

    Monday, March 21, 2011 11:33 AM
  • Just a thought: If it's just on your machine, how about modifying the registry so that when Excel is started via automation, it starts with the /e flag?

    I guess you could either dynamically alter the registry before you start Excel, then change it back again, or maybe you could even copy the existing CLSID (create a new one), modify it, and create a corresponding ProgID (e.g. Excel.SafeApplication) that points to that CLSID to use in your VBA code.

    Haven't tried it so no idea if it would work.


    Peter Jamieson
    Wednesday, March 23, 2011 10:07 AM