
| auteur : SilkyRoad |
Pour activer une bibliothèque (référence), déplacez vous dans l'éditeur de macros (ALT+F11):
Menu Outils
Références
Cochez la ligne qui correspond à l'application que vous souhaitez piloter.
Cliquez sur le bouton OK pour Valider.
Vous avez désormais accès aux méthodes et propriétés VBA de l'application spécifiée.
Quelques exemples de bibliothèques (en fonction des applications installées sur votre poste):
Microsoft Word xx.x Object Library (xx.x dépend de la version installée sur le poste)
Microsoft ActiveX Data Object 2.x Library (modèle ADO)
Windows Media Player
Shockwave Flash
…etc…
Voici un exemple pour déclarer une variable qui utilise la bibliothèque Word:
Vba |
Dim wordApp As Word. Application
|
Remarque: L'outil de saisie semi automatique (intellisense) permet d'afficher les méthodes et propriétés de la bibliothèque.
Vous n'avez pas besoin de réactiver la référence si vous utilisez le classeur sur un autre poste de travail.
Quand plusieurs versions d'une librairie sont disponibles (exemple AD0 2.1, 2.5, 2.7 ...) et que vous devez utiliser le
classeur sur plusieurs Postes possédant des configurations différentes, sélectionnez la version la plus ancienne commune
pour assurer une compatibilité.
La configuration WindowsXP/Office97 peut provoquer des erreurs lors de la création d'objets. Le message qui
s'affiche est: "Erreur d'exécution -2147417851 (80010105)".
Dans ce cas, une solution consiste à remplacer l'instance Early Binding:
Vba |
Dim wordApp As Word. Application
Dim wordDoc As Word. Document
Set wordApp = New Word. Application
wordApp. Visible = True
Set wordDoc = wordApp. Documents . Add
|
par une instance Late Binding:
Vba |
Dim wordApp As Object
Dim wordDoc As Object
Set wordApp = CreateObject (" Word.Application " )
wordApp. Visible = True
Set wordDoc = wordApp. Documents . Add
|
Vous trouvez des informations complémentaires sur le site Microsoft
L'instance Late Binding permet principalement de gérer les problèmes de version
lorque le fichier est utilisé sur des postes disposant de configurations différentes.
Remarque:
Il est possible d'ajouter une référence par macro:
Vba |
Dim xRef As String
xRef = " C:\Program Files\Microsoft Office\Office10\MSOUTL.OLB "
ThisWorkbook. VBProject . References . AddFromFile xRef
|
|
| auteur : SilkyRoad |
L'archivage des visites depuis l'explorateur FireFox est nommé history.dat. Ce fichier est généralement dans le répertoire:
C:\Documents and Settings\nom_utilisateur\Application Data\Mozilla\Firefox\Profiles\11ndnaxb.default\history.dat
La procédure ci-dessous lit le contenu du fichier, extrait les url et les dates de dernière visite.
Vba |
Sub ExtraireURL_History_FireFox ()
Dim CibleLigne As String
Dim Fichier As String , Resultat As String
Dim Valeur As Double
Dim Place As Double, Fin As Double, Debut As Double
Dim x As Double, y As Double
Dim i As Integer
Fichier = " C:\Documents and Settings\nom_utilisateur\...\Firefox\...\history.dat "
Open Fichier For Input As #1
Valeur = FileLen (Fichier)
CibleLigne = Input (Valeur, 1 )
Close 1
Debut = 1
Do While InStr (Debut, CibleLigne, " http " ) < > 0
Place = InStr (Debut, CibleLigne, " http " )
Fin = InStr (Place, CibleLigne, " ) " )
Resultat = Replace (Replace (Mid (CibleLigne, Place, Fin - Place), _
vbCrLf , " " ), " \ " , " " )
If InStr (1 , Resultat, " pagead2.googlesyndication.com " ) = 0 Then
i = i + 1
Worksheets (" Feuil1 " ). Cells (i, 1 ) = Resultat
x = InStr (Fin, CibleLigne, " = " )
y = InStr (x, CibleLigne, " ) " )
If IsNumeric (Mid (CibleLigne, x + 1 , y - x - 1 )) Then _
Worksheets (" Feuil1 " ). Cells (i, 2 ) = _
Timestamp_To_Date (Left (Mid (CibleLigne, x + 1 , y - x - 1 ), 10 ), 1970 )
End If
Debut = Fin
Loop
Columns (" A:B " ). AutoFit
End Sub
Function Timestamp_To_Date (TimeStamp As Double, Annee As Double) As Date
Dim DebutAnnee As Date
DebutAnnee = CDate (" 01/01/ " + Str (Annee))
Timestamp_To_Date = DateAdd (" s " , TimeStamp, DebutAnnee)
End Function
|
|
| auteur : Kiki29 | Vba |
Sub Tst_Adobe_PDF_03 ()
Dim sNomFichierPS As String
Dim sNomFichierPDF As String
Dim sNomFichierLOG As String
Dim PDFDist As PdfDistiller
Dim PrinterDefault As String
PrinterDefault = Application. ActivePrinter
Application. ActivePrinter = Imprimante_AdobePDF
sNomFichierPS = ThisWorkbook. Path & " \ " & " Essai_AdobbePDF.ps "
sNomFichierPDF = ThisWorkbook. Path & " \ " & " Essai_AdobbePDF.pdf "
sNomFichierLOG = ThisWorkbook. Path & " \ " & " Essai_AdobbePDF.log "
ActiveSheet. Range (" Zone " ). PrintOut Copies:= 1 , Preview:= False , _
ActivePrinter:= " Acrobat PDF " , PrintToFile:= True , _
Collate:= True , PrToFilename:= sNomFichierPS
Set PDFDist = New PdfDistiller
PDFDist. FileToPDF sNomFichierPS, sNomFichierPDF, " "
Set PDFDist = Nothing
Kill sNomFichierPS
Kill sNomFichierLOG
Application. ActivePrinter = PrinterDefault
End Sub
Private Function Imprimante_AdobePDF () As String
Dim i As Integer
Dim NomPortReseau As String
For i = 0 To 10
If i < 10 Then
NomPortReseau = " Adobe PDF sur Ne0 " & i & " : "
Else
NomPortReseau = " Adobe PDF sur Ne " & i & " : "
End If
On Error Resume Next
Application. ActivePrinter = NomPortReseau
If ActivePrinter = NomPortReseau Then
Exit For
End If
Next i
Imprimante_AdobePDF = NomPortReseau
End Function
|
Dans une configuration d'Entreprise avec de multiples utilisateurs et les droits attenants, il peut être
nécessaire de connaitre son nom de login (qui n'a rien à voir avec Application.UserName de VBA ).
Vba |
Dim sUserName As String
sUserName = Environ (" USERNAME " )
|
Ou si l'on préfère connaître le chemin "C:\Documents and Settings\UserName" :
Vba |
Dim sUserProfile As String
sUserProfile = Environ (" USERPROFILE " )
|
Dans ce cas, l'exemple ci-dessus deviendra :
Vba |
Sub Tst_Adobe_PDF ()
Dim sNomFichierPS As String
Dim sNomFichierPDF As String
Dim sNomFichierLOG As String
Dim PDFDist As PdfDistiller
Dim PrinterDefault As String
Dim sUserProfile As String
sUserProfile = Environ (" USERPROFILE " )
PrinterDefault = Application. ActivePrinter
Application. ActivePrinter = Imprimante_AdobePDF
sNomFichierPS = sUserProfile & " \ " & " Essai_AdobbePDF.ps "
sNomFichierPDF = sUserProfile & " \ " & " Essai_AdobbePDF.pdf "
sNomFichierLOG = sUserProfile & " \ " & " Essai_AdobbePDF.log "
ActiveSheet. Range (" Zone " ). PrintOut Copies:= 1 , Preview:= False , _
ActivePrinter:= " Acrobat PDF " , PrintToFile:= True , _
Collate:= True , PrToFilename:= sNomFichierPS
Set PDFDist = New PdfDistiller
PDFDist. FileToPDF sNomFichierPS, sNomFichierPDF, " "
Set PDFDist = Nothing
Kill sNomFichierPS
Kill sNomFichierLOG
Application. ActivePrinter = PrinterDefault
End Sub
|
|
| auteur : SilkyRoad |
Cet exemple ferme tous les fichiers ouverts par le bloc notes ('notepad.exe').
Vba |
Sub FermerUneApplication ()
Dim objProcess As WbemScripting. SWbemObject
Dim colProcessList As WbemScripting. SWbemObjectSet
Dim objWMIService As WbemScripting. SWbemServices
Dim strComputer As String
strComputer = " . "
Set objWMIService = GetObject (" winmgmts: " _
& " {impersonationLevel=impersonate}!\\ " & strComputer & " \root\cimv2 " )
Set colProcessList = objWMIService. ExecQuery _
(" Select * from Win32_Process Where Name = 'notepad.exe' " )
For Each objProcess In colProcessList
objProcess. Terminate
Next
End Sub
|
|
| auteur : SilkyRoad |
Cet exemple vérifie si la calculatrice est déjà ouverte, l'affiche au premier plan et sinon déclenche
l'ouverture de l'application.
La procédure ne doit pas être déclenchée depuis l'éditeur de macros.
Vba |
Option Explicit
Private Declare Function BringWindowToTop Lib " user32 " _
(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 ShowWindow Lib " user32 " _
(ByVal Hwnd As Long, ByVal nCmdShow As Long) As Long
Sub ApplicationPremierPlan ()
Dim Hwnd As Long
Hwnd = FindWindow (vbNullString , " Calculatrice " )
If Hwnd > 0 Then
BringWindowToTop Hwnd
ShowWindow Hwnd, 1
Else
Shell " C:\WINDOWS\system32\calc.exe " , vbNormalFocus
End If
End Sub
|
|
| auteur : SilkyRoad |
Il est possible de piloter l'application Open Office par VBA, grâce au pont (bridge) UNO-Automation.
Vous allez tout d'abord créer une référence à l'objet "Gestionnaire de Service" d'OOo:
Vba |
Dim oServ As Object
Set oServ = CreateObject (" com.sun.star.ServiceManager " )
|
Et ensuite vous allez utiliser cette référence pour manipuler l'API d'OpenOffice.
Vba |
Dim Desktop As Object, Document As Object
Dim args ()
Set Desktop = oServ. createInstance (" com.sun.star.frame.Desktop " )
Set Document = Desktop. loadComponentFromURL (" private:factory/swriter " , " _blank " , 0 , args)
|
|
Consultez les autres F.A.Q's
Les sources présentés sur cette pages sont libre de droits,
et vous pouvez les utiliser à votre convenance. Par contre cette page de présentation de ces sources constitue une oeuvre intellectuelle protégée par les droits d'auteurs.
Copyright ©2008
Developpez LLC. Tout droits réservés Developpez LLC.
Aucune reproduction, même partielle, ne peut être faite de ce site et de
l'ensemble de son contenu : textes, documents et images sans l'autorisation
expresse de Developpez LLC. Sinon vous encourez selon la loi jusqu'à 3 ans
de prison et jusqu'à 300 000 E de dommages et intérets.
Cette page est déposée à la SACD.
|