電子書籍の厳選無料作品が豊富!

アクセスからエクセルを開いた時に
エクセルがアクティブになったりならなかったり、
その時によって違うのですが
確実にエクセルをアクティブにする方法はありますか?

AppActivateステートメントを使うという事がわかったのですが
AppActivateをどうやって使えばいいでしょうか?


++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Sub test()
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim MyFileName
Dim MyFileFullName
Dim プロシージャー名

MyFileName = "book.xlsm"
MyFileFullName = CurrentProject.Path & "\" & MyFileName
プロシージャー名 = "開く"

Set xlApp = New Excel.Application
Set xlBook = xlApp.Workbooks.Open(MyFileFullName)

xlApp.Visible = True
xlApp.Run "'" & MyFileFullName & "'!" & プロシージャー名

Set xlBook = Nothing
Set xlApp = Nothing
End Sub
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

で問題なくエクセルファイルを立ち上げられますが
ここで確実にエクセルをアクティブ(手前に表示)にする方法を教えてください。

A 回答 (1件)

http://oshiete.goo.ne.jp/qa/7866966.html
を参考にして。
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function SetForegroundWindow Lib "user32.dll" (ByVal hwnd As Long) As Long

Sub test()
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim MyFileName
Dim MyFileFullName
Dim プロシージャー名

MyFileName = "book.xlsm"
MyFileFullName = CurrentProject.Path & "\" & MyFileName
プロシージャー名 = "開く"

Set xlApp = New Excel.Application
Set xlBook = xlApp.Workbooks.Open(MyFileFullName)

xlApp.Visible = True
SetForegroundWindow xlApp.hwnd 'としたらどうでしょ?
xlApp.Run "'" & MyFileFullName & "'!" & プロシージャー名

Set xlBook = Nothing
Set xlApp = Nothing
End Sub
    • good
    • 1
この回答へのお礼

APIを使うのですか。

xlApp.Visible = True
の下に
SetForegroundWindow xlApp.hwnd
に入れたらアクティブになりました。(今の所)

ちなみに
SetForegroundWindow xlApp.hwndは
Private Declare Function SetForegroundWindow Lib "user32.dll" (ByVal hwnd As Long) As Long
の方を使っているようですが
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
は何の意味があるのでしょうか?
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
をコメントアウトしても問題なく動きました。

お礼日時:2013/01/11 19:37

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!