1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
| '------------------------------------------------------------------------------------------------
' Permet de manipuler Excel dans une autre instance.
' Sources: https://docs.microsoft.com/fr-fr/office/vba/language/reference/user-interface-help/getobject-function
'------------------------------------------------------------------------------------------------
Option Explicit
' Declare necessary API routines:
Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, _
ByVal lpWindowName As Long) As Long
Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, _
ByVal wParam As Long, ByVal lParam As Long) As Long
'------------------------------------------------------------------------------------------------
Sub GetExcel()
'------------------------------------------------------------------------------------------------
Dim MyXL As Object ' Variable to hold reference to Microsoft Excel.
Dim ExcelWasNotRunning As Boolean ' Flag for final release.
' Test to see if there is a copy of Microsoft Excel already running.
' Getobject function called without the first argument returns a
' reference to an instance of the application. If the application isn't
' running, an error occurs.
On Error Resume Next
Set MyXL = GetObject(, "Excel.Application")
If Err.Number <> 0 Then ExcelWasNotRunning = True
Err.Clear
' Check for Microsoft Excel. If Microsoft Excel is running enter it into the Running Object table.
If DetectExcel = True Then
' Set the object variable to reference the file you want to see.
Set MyXL = GetObject("C:\Test\Monfichier.xlsm")
' Show Microsoft Excel through its Application property. Then show the actual window containing
' the file using the Windows collection of the MyXL object reference.
MyXL.Application.Visible = True
MyXL.Parent.Windows(1).Visible = True
' ....... Placez ici vos instructions :
' Active la feuille pour pouvoir utiliser ActiveSheet
MyXL.Worksheets(MyXL.ActiveSheet.Name).Activate
Debug.Print ActiveCell.Value
' Autre exemple si l'on connait le nom de la feuille et la cellule concernée:
Debug.Print MyXL.Sheets("Feuil2").Range("A1")
' .......
' If this copy of Microsoft Excel was not running when you started, close it using the
' Application property's Quit method. Note that when you try to quit Microsoft Excel, the
' title bar blinks and a message is displayed asking if you want to save any loaded files.
If ExcelWasNotRunning = True Then MyXL.Application.Quit
End If
Set MyXL = Nothing ' Release reference to the application and spreadsheet.
End Sub
'------------------------------------------------------------------------------------------------
Function DetectExcel() As Boolean
'------------------------------------------------------------------------------------------------
' Procedure dectects a running Excel and registers it.
Const WM_USER = 1024
Dim hWnd As Long
' If Excel is running this API call returns its handle, 0 means Excel not running.
hWnd = FindWindow("XLMAIN", 0)
If hWnd <> 0 Then
' Excel is running so use the SendMessage API function to enter it in the Running Object Table.
SendMessage hWnd, WM_USER + 18, 0, 0
DetectExcel = True
End If
End Function
'------------------------------------------------------------------------------------------------
'------------------------------------------------------------------------------------------------ |
Partager