Enregistrer paramètres

Sirkel Messages postés 2 Date d'inscription mardi 4 mars 2008 Statut Membre Dernière intervention 15 juillet 2008 - 15 juil. 2008 à 18:43
Sirkel Messages postés 2 Date d'inscription mardi 4 mars 2008 Statut Membre Dernière intervention 15 juillet 2008 - 15 juil. 2008 à 19:46
Bonjour,

Je programme depuis pas mal de temps, mais pas vraiment en VBA, c'est une demande spécifique que l'on m'a faite, et je colle sur un soucis, je souhaiterai enregistrer des paramètres pour l'exécution de la macro, des paramètres que je pourrais réutiliser à chaque exécution de la macro sans avoir à les redemander à l'utilisateur. C'est possible ou pas ?

3 réponses

gillardg Messages postés 3275 Date d'inscription jeudi 3 avril 2008 Statut Membre Dernière intervention 14 septembre 2014 2
15 juil. 2008 à 18:49
Storing Values When a Macro Ends



See Also
Specifics


When a macro ends, the values stored in its variables aren't automatically saved to disk. If a macro needs to preserve a value, it must store that value outside itself before the macro execution is completed. This topic describes five locations where macro values can be easily stored and retrieved.


Document variables

Document variables allow you to store values as part of a document or a template. For example, you might store macro values in the document or template where the macro resides. You can add variables to a document or template using the [mk:@MSITStore:C:\Documents%20and%20Settings\Georges\Mes%20documents\vbaxl%20help\vbawd10.chm::/html/womthAdd1.htm Add] method of the [mk:@MSITStore:C:\Documents%20and%20Settings\Georges\Mes%20documents\vbaxl%20help\vbawd10.chm::/html/woobjVariables1.htm Variables] collection. The following example saves a document variable in the same location as the macro that is running (document or template) using the ThisDocument property.



Sub AddDocumentVariable()
    ThisDocument.Variables.Add Name:=  "Age", Value:=12
End Sub



The following example uses the [mk:@MSITStore:C:\Documents%20and%20Settings\Georges\Mes%20documents\vbaxl%20help\vbawd10.chm::/html/woproValue1.htm Value ] property with a [mk:@MSITStore:C:\Documents%20and%20Settings\Georges\Mes%20documents\vbaxl%20help\vbawd10.chm::/html/woobjVariable1.htm Variable] object to return the value of a document variable.



Sub UseDocumentVariable()
    Dim intAge As Integer
    intAge  = ThisDocument.Variables("Age").Value
End Sub



Remarks

You can use the DOCVARIABLE field to insert a document variable into a document.


Document properties

Like document variables, document properties allow you to store values as part of a document or a template. Document properties can be viewed in the Properties dialog box (File menu).


The Word object model breaks document properties into two groups: built-in and custom. Custom document properties include the properties shown on the Custom tab in the Properties dialog box. Built-in document properties include the properties on all the tabs in the Properties dialog box except the Custom tab.


To access built-in properties, use the [mk:@MSITStore:C:\Documents%20and%20Settings\Georges\Mes%20documents\vbaxl%20help\vbawd10.chm::/html/woproBuiltInDocumentProperties1.htm BuiltInDocumentProperties] property to return a [mk:@MSITStore:vbaof11.chm::/html/ofobjDocumentProperties1.htm DocumentProperties] collection that includes the built-in document properties. Use the [mk:@MSITStore:C:\Documents%20and%20Settings\Georges\Mes%20documents\vbaxl%20help\vbawd10.chm::/html/woproCustomDocumentProperties1.htm CustomDocumentProperties] property to return a DocumentProperties collection that includes the custom document properties. The following example creates a custom document property named "YourName" in the same location as the macro that is running (document or template).



Sub AddCustomDocumentProperties()
    ThisDocument.CustomDocumentProperties.Add Name:= "YourName", _
        LinkToContent:=False, Value:="Joe", Type:=msoPropertyTypeString
End Sub



Built-in document properties cannot be added to the DocumentProperties collection returned by the BuiltInDocumentProperties property. You can, however, retrieve the contents of a built-in document property or change the value of a read/write built-in document property.


Remarks

You can use the DOCPROPERTY field to insert document properties into a document.


AutoText entries

AutoText entries can be used to store information in a template. Unlike a document variable or property, AutoText entries can include items beyond macro variables such as formatted text or a graphic. Use the [mk:@MSITStore:C:\Documents%20and%20Settings\Georges\Mes%20documents\vbaxl%20help\vbawd10.chm::/html/womthAdd1.htm Add] method with the [mk:@MSITStore:C:\Documents%20and%20Settings\Georges\Mes%20documents\vbaxl%20help\vbawd10.chm::/html/woobjAutoTextEntries1.htm AutoTextEntries] collection to create a new AutoText entry. The following example creates an AutoText entry named "MyText" that contains the contents of the selection. If the following instruction is part of a template macro, the new AutoText entry is stored in the template, otherwise, the AutoText entry is stored in the template attached to the document where the instruction resides.



Sub AddAutoTextEntry()
    ThisDocument.AttachedTemplate.AutoTextEntries.Add Name: ="MyText", _
        Range:= Selection.Range
End Sub



Use the [mk:@MSITStore:C:\Documents%20and%20Settings\Georges\Mes%20documents\vbaxl%20help\vbawd10.chm::/html/woproValue1.htm Value ] property with an [mk:@MSITStore:C:\Documents%20and%20Settings\Georges\Mes%20documents\vbaxl%20help\vbawd10.chm::/html/woobjAutoTextEntry1.htm AutoTextEntry] object to retrieve the contents of an AutoText entry object.


Settings files

You can set and retrieve information from a settings file using the [mk:@MSITStore:C:\Documents%20and%20Settings\Georges\Mes%20documents\vbaxl%20help\vbawd10.chm::/html/woproPrivateProfileString1.htm PrivateProfileString] property. The structure of a Windows settings file is the same as the Windows 3.1 WIN.INI file. The following example sets the DocNum key to 1 under the DocTracker section in the Macro.ini file.



Sub MacroSystemFile()
    System.PrivateProfileString( _
        FileName: ="C:\My Documents\Macro.ini", _
        Section:= "DocTracker", Key:="DocNum") = 1
End Sub



After running the above instruction, the Macro.ini file includes the following text.



[DocTracker]
DocNum=1



The PrivateProfileString property has three arguments: FileName , Section, and Key. The FileName argument is used to specify a settings file path and file name. The Section argument specifies the section name that appears between brackets before the associated keys (don't include the brackets with section name). The Key argument specifies the key name which is followed by an equal sign ( =) and the setting.


Use the same PrivateProfileString property to retrieve a setting from a settings file. The following example retrieves the DocNum setting under the DocTracker section in the Macro.ini file.



Sub GetSystemFileInfo()
    Dim intDocNum As Integer
    intDocNum =   System.PrivateProfileString( _
        FileName:="C:\My Documents\Macro.ini", _
        Section:="DocTracker", Key:="DocNum")
    MsgBox "DocNum is " & intDocNum
End Sub



Windows registry

You can set and retrieve information from the Windows registry using the [mk:@MSITStore:C:\Documents%20and%20Settings\Georges\Mes%20documents\vbaxl%20help\vbawd10.chm::/html/woproPrivateProfileString1.htm PrivateProfileString ] property. The following example retrieves the Microsoft Office Word 2003 program directory from the Windows registry.



Sub GetRegistryInfo()
    Dim strSection As String
    Dim strPgmDir As String
    strSection  = "HKEY_CURRENT_USER\Software\Microsoft" _
        & "\Office\10.0\Word\Options"
    strPgmDir =  System.PrivateProfileString(FileName:="", _
        Section:=strSection, Key:="PROGRAMDIR")
    MsgBox "The directory for Word is - " & strPgmDir
End Sub



The PrivateProfileString property has three arguments: FileName , Section, and Key. To return or set a value for a registry entry, specify an empty string ("") for the FileName argument. The Section argument should be the complete path to the registry subkey. The Key argument should be the name of an entry in the subkey specified by Section.


You can also set information in the Windows registry using the following PrivateProfileString syntax.



System.PrivateProfileString(FileName, Section, Key) =value


The following example sets the DOC-PATH entry to “C:\My Documents” in the Options subkey for Office Word 2003 in the Windows registry.



Sub SetDocumentDirectory()
    Dim strDocDirectory As String
    strDocDirectory = "HKEY_CURRENT_USER\Software\Microsoft" _
        & "\Office\10.0\Word\Options"
    System.PrivateProfileString(FileName:="", _
        Section:=strDocDirectory, Key:="DOC-PATH") = "C:\My Documents"
End Sub







<center>
[mk:@MSITStore:C:\Documents%20and%20Settings\Georges\Mes%20documents\vbaxl%20help\vbawd10.chm::/html/WordVBACopyright_HV01135790.htm ©2003 Microsoft Corporation. All rights reserved]




</center>




Les écologistes réclament une société propre les imbéciles nettoient
0
gillardg Messages postés 3275 Date d'inscription jeudi 3 avril 2008 Statut Membre Dernière intervention 14 septembre 2014 2
15 juil. 2008 à 18:51
 Microsoft Office Word 2003 Visual Basic for Applications (VBA) Language Reference.

Les écologistes réclament une société propre les imbéciles nettoient
0
Sirkel Messages postés 2 Date d'inscription mardi 4 mars 2008 Statut Membre Dernière intervention 15 juillet 2008
15 juil. 2008 à 19:46
merci c'est exactement ce qu'il me fallait.
0
Rejoignez-nous