Setting focus to excel and then back to the original app.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • AJ James

    #1

    Setting focus to excel and then back to the original app.

    I have an application that opens excel and uses excel in the back ground. The application also has a way to execute excel macros. The excel macro I have written checks some conditions and then sends a message via a msgbox to the screen. My problem is that since the excel is not in focus when I execute the macro, the macro waits until the excel is in focus, displays the msg, and when I exit excel status in focus.

    I would like to see excel remain in the background ad the message displayed and when the msgbox is closed and the macro ends, excel stays minimized the entire time.

    I will accept that excel is shown when teh macro executes, and is minimized when the msgbox is closed (focus reverting back to my main applicaiton).

    I have tried many different approaches. Removing the un-needed code, I have the following in my macro:

    sub msgmacro

    ...

    msgbox ("Message")

    ...

    AppActivate "Microsoft Excel"
    Applicaiton.Win dowState = xlMinimized

    ...

    end sub

    I have also tried adding logic at the end of the macro

    AppActivate "mymainapp"
    Applicaiton.Dis playFullScreen = True

    Applicaiton.Int eractive = True

    Applicaiton.Win dowState = xlNormal

    And the following at the beginning of the macro

    AppActivate "MicroSoft Excel"
    Application.Win dowState = xlNormal

    all wihtout success.
  • Guido Geurs
    Recognized Expert Contributor
    • Oct 2009
    • 767

    #2
    This works for me with Office 2003.
    It calls a macro in Excel from Word without opening Excel:

    1 - Add a module in Excel with Your macro (like for my test)=

    Code:
    Sub Message()
       MsgBox ("OK" & Range("A1") & Range("A2"))
    End Sub
    2 - add in word a module with the code to call the macro in Excel: (My XLS file is in D:\....)

    Code:
    Sub XLTest()
    Dim XL As Object
       Set XL = CreateObject("Excel.Application")
       XL.Workbooks.Open "D:\GGtools\## TESTEN VB6 OPLOSSINGEN ##\Bytes.com\VBA WORD\WORD Setting focus to excel and then back to the original app\Setting focus to excel and then back to the original app_v2.xls"
       XL.Run "Message"
       'ยง End process Excel
       Call Shell("taskkill /F /IM Excel.exe", vbHide)
    End Sub

    PS: read also "http://support.microso ft.com/kb/177760"

    Comment

    Working...