Bonjour à tous,
J'aurai besoin d'aide, je ne comprends pas ce qui se passe dans mon code. Je cherche à lancer une macro excel depuis un fichier batch. J'ai trouvé le code sur internet, la macro se lance bien mais il y a un problème à la fin:
- le fichier excel se ferme (comme demandé dans le code)
- un autre fichier excel appelé Classeur1 vide avec un onglet nommé Macro1 s'ouvre (je ne sais pas d'où il sort ni à quoi correspond Macro1)
Je ne sais pas si c'est lié à l'histoire des getobject/createobject ou bien à l'histoire où il faut nommer un onglet avec le nom du classeur le précédent ou bien à complètement autre chose. Tout ce que je sais c'est que quand je mets en commentaires la ligne wbExcel.Close, ça marche. Mais moi, je voudrai que excel sauvegarde tout seul le fichier sans que j'ai à cliquer sur la croix.
Auriez-vous une idée d'où sortent Macro1 et Classeur1 ?
Merci de votre aide !
Voici le code:
Fichier batch:
Fichier excel, module ThisWorkbook
Code : Sélectionner tout - Visualiser dans une fenêtre à part "C:\Program Files\Microsoft Office\Office14\EXCEL.EXE" /cmd /MAJ "P:\BilanMensuel.xlsm"
Fichier excel, module Module1
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 Private Declare Function GetCommandLine Lib "kernel32" Alias "GetCommandLineA" () As Long Private Declare Function lstrlen Lib "kernel32" Alias "lstrlenA" (lpString As Any) As Long Private Declare Function lstrcpy Lib "kernel32" Alias "lstrcpyA" (lpString1 As Any, lpString2 As Any) As Long Private Function GetCmd() As String Dim lpCmd As Long lpCmd = GetCommandLine() GetCmd = Space$(lstrlen(ByVal lpCmd)) lstrcpy ByVal GetCmd, ByVal lpCmd End Function Private Sub Workbook_Open() Dim macmdline As Variant Dim monparam As Variant 'déclare une variable macmdline = GetCmd 'affecte la valeur de la ligne de commande If Not IsNull(macmdline) Then 'si la variable est nulle If Len(macmdline) > 0 Then 'on s'assure qu'il y a eu une ligne de commande passée If InStr(macmdline, "/cmd") > 0 Then macmdline = Replace(macmdline, ThisWorkbook.FullName, "", , , vbTextCompare) monparam = Split(macmdline, " /cmd ") Application.Run Mid(monparam(1), 2, Len(monparam(1)) - 3) End If End If End If End Sub
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 Sub MAJ() Dim ExcelWasRunning As Boolean Dim xlApp As Object Dim wbExcel As Object Set xlApp = Nothing On Error Resume Next Set xlApp = GetObject(, "Excel.Application") On Error GoTo 0 If xlApp Is Nothing Then Set xlApp = CreateObject("Excel.Application") ExcelWasRunning = False Else ExcelWasRunning = True End If Set wbExcel = ActiveWorkbook Dim oAccesS As Access.Application Dim db As DAO.Database Set oAccesS = GetObject(ML_PATH) 'ML_PATH est une constant declarée Public oAccesS.Visible = False Set db = oAccesS.CurrentDb Call LancementRequêteAccess("MaRequeteAccess", "MonOngletExcelOuExporterLesDonnees", "NomDuRangeSousExcelOuExporterLesDonnees", "A1") db.Close oAccesS.Quit Set db = Nothing Set oAccesS = Nothing If Not ExcelWasRunning Then wbExcel.Close True xlApp.Quit Set wbExcel = Nothing Set xlApp = Nothing Else wbExcel.Close True Set wbExcel = Nothing End If End Sub Sub LancementRequêteAccess(MyQuery As String, MyWorksheet As String, MyDynamicRange As String, MyRangeStart As String) On Error GoTo err Dim qdf As QueryDef Dim rst As DAO.Recordset Set qdf = CurrentDb.QueryDefs(MyQuery) Set rst = qdf.OpenRecordset(dbOpenSnapshot) If IsEmpty(Worksheets(MyWorksheet).Range(MyRangeStart)) <> True Then Worksheets(MyWorksheet).Range(MyDynamicRange).ClearContents End If Worksheets(MyWorksheet).Range(MyRangeStart).CopyFromRecordset rst Exit_Err: rst.Close: qdf.Close Set rst = Nothing Set qdf = Nothing Exit Sub err: MsgBox "Une erreur est survenue" & vbCrLf & err.Description, vbCritical, "LancementRequêteAccess" End Sub
Partager