Witam
Chcę mieć możliwość utworzenia pliku xml z solidworks, a także odczytania go i pobrania wartości z tego pliku.
Celem jest możliwość zapisania różnych opcji CheckBox w zależności od komputera, z którego uruchamiane jest makro, i pobrania ich przy następnym uruchomieniu z ewentualną modyfikacją.
Znalazłem funkcjonalną metodę zapisu tego pliku xml, ale niemożliwe do pobrania wartości (wstawia mnie jako spację między każdą literą w rzutni makra, a mimo to xml jest bez spacji między literami.
Oto plik xml:
<OPTIONS>
<CheckBoxCacherPièce="Vrai">
<CheckBoxCacherPlan="Vrai">
<CheckBoxDesignation="Vrai">
<CheckBoxCouleur="Vrai">
<CheckBoxRevision="Vrai">
</OPTIONS>
Makro do napisania:
' Add a reference to Microsoft Scripting Runtime (click Tools > References > Browse > C:\windows\system32\scrrun.dll.
Sub ecrire()
'Exemple XML attribute: <Counter name="Incrementing0" Version="1" id="Incrementing0" Auto="0" AutoName="" type="1" RangeCounter="False" RangeDate="23/12/2019">
Dim swApp As SldWorks.SldWorks
Dim sPath As String
Dim sPathName As String
Dim fso As Scripting.FileSystemObject
Dim XMLfile As Scripting.TextStream
Set swApp = Application.SldWorks
sPath = Environ("USERPROFILE") & "\.SaveMacroSldworks\"
sPathName = sPath & "SaveMacroIndice.xml"
Debug.Print sPathName
'On vérifie si le dossier existe sinon on le créé
'If Dir(sPath) = "" Then
If Dir(sPath, vbDirectory + vbHidden) = "" Then
Debug.Print "Création du dossier: " & sPath
MkDir sPath
End If
Set fso = CreateObject("Scripting.FileSystemObject")
Set XMLfile = fso.CreateTextFile(sPathName, True, True)
XMLfile.WriteLine "<OPTIONS>"
'On vérifie tout les objets commençant par CheckBox et on sauvegarde la valeur de l'option dans le fichier xml
Dim ole1 As Control
For Each ole1 In UserformOptionsMacro.Controls
If Left$(ole1.Name, 8) = "CheckBox" Then XMLfile.WriteLine " <" & ole1.Name & "=" & Chr(34) & ole1.Value & Chr(34) & ">"
Next
'XMLfile.WriteLine " <Option 1=" & Chr(34) & True & Chr(34) & ">"
XMLfile.WriteLine "</OPTIONS>"
XMLfile.Close
End Sub
Makro do odczytania:
Sub lire()
Dim swApp As SldWorks.SldWorks
Dim sPath As String
Dim sPathName As String
Dim fso As Scripting.FileSystemObject
Dim XMLfile As Scripting.TextStream
Dim stChaine As String
Set swApp = Application.SldWorks
sPath = Environ("USERPROFILE") & "\.SaveMacroSldworks\"
sPathName = sPath & "SaveMacroIndice.xml"
Debug.Print sPathName
Set fso = CreateObject("Scripting.FileSystemObject")
'Ajouter la gestion d'erreur si fichier introuvable
Set XMLfile = fso.OpenTextFile(sPathName, ForReading, True, TristateFalse)
'Read till the end
Do Until XMLfile.AtEndOfStream
'Debug.Print "Printing line " & XMLfile.Line
'Debug.Print XMLfile.ReadLine 'Print a line from the file
stChaine = XMLfile.ReadLine
stChaine = Replace(stChaine, " ", vbNullString)
Debug.Print stChaine
If stChaine Like "*Checkbox*" Then MsgBox "CheckBox ok"
Loop
XMLfile.Close
End Sub
Jeśli ktoś ma inną, bardziej funkcjonalną metodę, przykład lub po prostu lead, który chce mi przekazać, pozwoli mi to ruszyć do przodu z moim projektem.
Z góry dziękuję.