OVH Cloud OVH Cloud

Portabilité de macro utilisant le solveur

14 réponses
Avatar
Eric S
Bonjour,

Il y a un certain temps, j'ai fait une feuille excel avec une macro VBA
utilisant le solveur. J'ai récemment changé de PC en conservant les
mêmes versions de windows et de office. Mais la macro ne marche plus
directement. Pourtant, j'ai bien installé le Solveur dans les Macro
complémentaires. Ce qui se passe:
- je clique sur le fichier
- excel s'ouvre
- sur la fenêtre de sécurité, je clique sur Activer les macros
- Microsoft Visual Basic s'ouvre et m'annonce une erreur de compilation
(Librairie manquante)
- je vais dans Outils|Références... là, il y a MANQUANT : SOLVER.XLA
- je vois en dessous qu'il le cherche dans
C:\Program Files\Microsoft Office\Office\MacroLib\Solveur\
Manque de chance, dans ma nouvelle installation, solver.xla est dans
C:\Program Files\Microsoft Office\Office10\MacroLib\Solver\
- si j'indique le nouveau chemin, ça marche.

Comment faire pour qu'il trouve automatiquement SOLVER.XLA quelque soit
l'installation de Office sur le PC où c'est déployé?

Eric

4 réponses

1 2
Avatar
michdenis
Désolé, mais c'est ce fichier que vous deviez tester :

http://cjoint.com/?bsvjASMRAM
Avatar
Eric S
A l'ouverture, j'ai un message d'erreur : "Solver not found. This
workbook will not work".

Ce qui ne colle pas :

If Val(Application.Version) > 11 Then
CheckSolver ("Complément Solver")
CheckSolverIntl ("Solver.xlam")
Else
CheckSolver ("Complément Solveur")
CheckSolverIntl ("Solver.xla")
End If

J'ai une version 10 (Excel 2002). Dans les Macro complémentaires, j'ai
"Complément Solver". Alors que d'après ton code, je devrais avoir
"Complément Solveur". En fait, je pense que les CheckSolver devraient
être des fonctions qui nous renvoient si elles ont réussi ou non de
façon silencieuse. Il faudrait à chaque fois virer la ligne avec MsgBox
puis avoir un code d'ouverture du style

if not CheckSolver ("Complément Solver")
Then
if not CheckSolver ("Complément Solveur")
Then
If Val(Application.Version) > 11 Then
if not CheckSolverIntl ("Solver.xlam")
Then
MsgBox "Solver not found. This workbook will not
work.", vbCritical
End if
Else
if not CheckSolverIntl ("Solver.xla")
Then
MsgBox "Solver not found. This workbook will not
work.", vbCritical
End if
End if
End if
End if

Non testé.

Eric
Avatar
Modeste
Bonsour® Eric S
Bonne année !!! Meilleurs voeux à tous !!!

A l'ouverture, j'ai un message d'erreur : "Solver not found. This
workbook will not work".

Ce qui ne colle pas :

If Val(Application.Version) > 11 Then
CheckSolver ("Complément Solver")
CheckSolverIntl ("Solver.xlam")
Else
CheckSolver ("Complément Solveur")
CheckSolverIntl ("Solver.xla")
End If

J'ai une version 10 (Excel 2002). Dans les Macro complémentaires, j'ai
"Complément Solver". Alors que d'après ton code, je devrais avoir
"Complément Solveur".



utilisateur de Excel 2002
cette macro fonctionne correctement avec Complément solver

Sub testsolver()
Dim a As AddIn
Set a = AddIns("Complément solver")
If a.Installed = True Then
MsgBox "The Solver add-in is installed"
Else
MsgBox "The Solver add-in is not installed"
End If
End Sub
Avatar
michdenis
Théoriquement, cette seule ligne de code devrait être suffisant :
| AddIns("Complément Solver").Installed = True

"Complément Solver" représente l'expression de la fenêtre des
macros complémentaires désignant la macro complémentaire : Solver.xla

En fait, lorsque l'on coche manuellement la case à côté de cette macro
complémentaire, Excel reçoit l'ordre d'ouvrir le fichier correspondant
Solver.xla situé dans un répertoire spécifique. Ce répertoire est connu et
varie selon le système d'exploitation en vigueur sur la machine.

La problématique survient lorsque l'on a besoin de cette macro complémentaire
dans un fichier donné et que ce fichier doit être ouvert sous différents systèmes
d'exploitation et différentes versions d'excel. Excel a sauvegardé en mémoire
l'emplacement du fichier source. Si un autre usager ouvre le fichier, comment
s'assurer qu'à l'ouverture du fichier la référence à la location du fichier source
soit corrigée quelle que soit le système d'exploitation, le cas échéant.

Je crois que ce qui suit devrait fonctionner pour toutes les versions d'excel
de 1977 à 2007. Reste à trouver des volontaires pour tester les procédures.

'Mettre cette section dans le ThisWorkbook du classeur

Private Sub Workbook_Open()
Dim Arr, Arr1, Ad As Variant, Elt As Variant
Dim X As String, Ok As Boolean, fin As String

Arr = Array("Complément Solver", "Complément Solveur", "Solver Add-In")
Arr1 = Array("Solver.xla", "Solver.xlam")
Ok = False
For Each Elt In Arr
If CheckSolver(Elt) Then
For Each Ad In Arr1
X = Ad
If CheckSolverIntl(X) = True Then
Ok = True
GoTo fin:
End If
Next
End If
Next
fin:
If Ok = True Then
MsgBox "Addin : Solver est bien installé."
Else
MsgBox "Addin : Solver n'a pas pu être installé."
End If
End Sub
'----------------------------------------------------

Mettre tout ce qui suit dans un module standard du classeur

'----------------------------------------------------
Function CheckSolver(Complement) As Boolean
'' Adjusted for Application.Run() to avoid Reference problems with Solver
'' Peltier Technical Services, Inc., Copyright © 2007. All rights reserved.
'' Returns True if Solver can be used, False if not.

Dim bSolverInstalled As Boolean

'' Assume true unless otherwise
CheckSolver = True

On Error Resume Next
' check whether Solver is installed
bSolverInstalled = Application.AddIns(Complement).Installed
Err.Clear

If bSolverInstalled Then
' uninstall temporarily
Application.AddIns(Complement).Installed = False
' check whether Solver is installed (should be false)
bSolverInstalled = Application.AddIns(Complement).Installed
End If

If Not bSolverInstalled Then
' (re)install Solver
Application.AddIns(Complement).Installed = True
' check whether Solver is installed (should be true)
bSolverInstalled = Application.AddIns(Complement).Installed
End If

If Not bSolverInstalled Then
'MsgBox "Solver not found. This workbook will not work.", vbCritical
CheckSolver = False
End If

If CheckSolver Then
' initialize Solver
Application.Run "Solver.xla!Solver.Solver2.Auto_open"
End If

On Error GoTo 0

End Function
'----------------------------------------------------

'The function above works fine for English versions of Excel,
'but in other languages, the name of the add-in may not
'be "Solver Add-In". We have to be a bit more clever, and introduce
'a loop to check the filenames of all add-ins. The CheckSolverIntl
'function below calls two additional functions which perform the loops.
'This function still relies on Solver being named "solver.xla".
'If this is not the case, change the value of the constant sAddIn in
'this procedure, and please email me about it.

'----------------------------------------------------
Function CheckSolverIntl(SolverName As String) As Boolean
'' Adjusted for Application.Run() to avoid Reference problems with Solver
'' Adjusted for international versions of Excel
'' Peltier Technical Services, Inc., Copyright © 2008. All rights reserved.
'' Returns True if Solver can be used, False if not.

Dim bSolverInstalled As Boolean
Dim bAddInFound As Boolean
Dim iAddIn As Long

'' Assume true unless otherwise
CheckSolverIntl = True

On Error Resume Next
' check whether Solver is installed
bSolverInstalled = IsInstalled(SolverName)
Err.Clear

If bSolverInstalled Then
' uninstall temporarily
bAddInFound = AddInInstall(SolverName, False)
' check whether Solver is installed (should be false)
bSolverInstalled = IsInstalled(SolverName)
End If

If Not bSolverInstalled Then
' (re)install Solver
bAddInFound = AddInInstall(SolverName, True)
' check whether Solver is installed (should be true)
bSolverInstalled = IsInstalled(SolverName)
End If

If Not bSolverInstalled Then
'MsgBox "Solver not found. This workbook will not work.", vbCritical
CheckSolverIntl = False
End If

If CheckSolverIntl Then
' initialize Solver
Application.Run "Solver.xla!Solver.Solver2.Auto_open"
End If

On Error GoTo 0
End Function
'----------------------------------------------------
Function IsInstalled(sAddInFileName As String) As Boolean
Dim iAddIn As Long

IsInstalled = False

For iAddIn = 1 To Application.AddIns.Count
With Application.AddIns(iAddIn)
If LCase$(.Name) = LCase$(sAddInFileName) Then
If .Installed Then
IsInstalled = True
End If
Exit For
End If
End With
Next
End Function
'----------------------------------------------------
Function AddInInstall(sAddInFileName As String, bInstall As Boolean) As Boolean
Dim iAddIn As Long

For iAddIn = 1 To Application.AddIns.Count
With Application.AddIns(iAddIn)
If LCase$(.Name) = LCase$(sAddInFileName) Then
If .Installed <> bInstall Then
.Installed = bInstall
End If
AddInInstall = True ' True = add-in is listed
Exit For
End If
End With
Next
End Function
'----------------------------------------------------
1 2