14 Replies Latest reply on Aug 11, 2017 11:12 AM by Matt Bieringer

    Save label caption on userform

    Matt Bieringer

      Hey guys,

       

      I have a macro that changes the label caption on my userform to match a folder that I am browsing to. Is it possible to have that caption change be permanent? I know this can be done in Excel VBA by enabling form designer with VBA, but I cannot figure out how to get that to work in Solidworks API.

       

      I tried to adapt the code from here: vBulletin Database Error , but I cannot get that to work.

        • Re: Save label caption on userform
          Deepak Gupta

          When you say permanent, then why can't you hard code that? And would you mind sharing the macro for someone to check the codes and suggest something?

            • Re: Save label caption on userform
              Matt Bieringer

              Deepak please see my reply to Simon for the code.

               

              Edit:

              There is a textbox on another form that changes this label, but if the other form is not called up then I want the label to remain the same as the last time a change was made to the form. This would be regardless of whether or not the initial userform and solidworks have been closed.

            • Re: Save label caption on userform
              Simon Turner

              I have edited that code for you:

               

              Sub main()

                  Dim i As Long, n As Long

                 

                  With Application.VBE.ActiveVBProject.VBComponents("Userform1").Designer

                      For i = 1 To .Controls.Count

                          If TypeName(.Controls(i - 1)) = "Label" Then

                              n = n + 1

                              .Controls(i - 1).Caption = "Some Label " & n

                          End If

                      Next

                  End With

              End Sub

                • Re: Save label caption on userform
                  Matt Bieringer

                  Simon When I use that I get a Run Time Error 91.

                   

                  Here is the sub where I try to save the label.

                  Private Sub CommandButton3_Click()
                  Dim strFold        As String
                  Set swApp = Application.SldWorks
                  With Browse
                      .Show vbModeless
                      .StartUpPosition = 0
                      .Top = swApp.FrameTop + 300
                      .Left = swApp.FrameLeft + swApp.FrameWidth - InitForm.Width - 1200
                      .TextBox1.SetFocus
                      .Repaint
                  End With
                  strFold = Browse.TextBox1.Text
                  Label1.Caption = strFold
                  Application.VBE.ActiveVBProject.VBComponents("Initform").Designer.Controls("label1").Caption = strFold
                  ListGen
                  End Sub
                  
                  • Re: Save label caption on userform
                    Matt Bieringer

                    It doesn't seem to like the .controls code for some reason. That is where I am getting the Run-Time Error '91'.

                      • Re: Save label caption on userform
                        Simon Turner

                        I'm not sure if you can use .Controls with the name of the control. The example I gave used the index.

                        Try looping through the controls using the index to find the control with the name "label1":

                        With Application.VBE.ActiveVBProject.VBComponents("Userform1").Designer

                                For i = 1 To .Controls.Count

                                    If .Controls(i - 1).Name = "Label1" Then

                                        .Controls(i - 1).Caption = strFold

                                    End If

                                Next

                            End With

                         

                        Having said this, it is a very unusual way to achieve what you want. There is almost certainly a more standard way of doing it, such as storing the label name in a global variable in a module.

                          • Re: Save label caption on userform
                            Matt Bieringer

                            Simon

                            Basically the label displays a folder that the user browses to. I want the label to preserve the last entry even if Solidworks has closed.

                             

                            That being said I keep getting the Run-Time Error '91' on the .controls line, which leads me to believe that I cannot use this function. I have the VBE UI 7.1 Object Library and the Visual Basic for Applications Extensibility 5.3 as references.

                             

                            I think I might need to do what Ivana recommended and use a txt file or something like that to save it.

                              • Re: Save label caption on userform
                                Deepak Gupta

                                You can use an INI file or simple text file to read back the data in case you're using VB and if you're using .NET then you can store the value in the program itself..

                                  • Re: Save label caption on userform
                                    Simon Turner

                                    Or in VBA, you can use the Windows Script Host Object Model to read and write values in the registry.

                                      • Re: Save label caption on userform
                                        Deepak Gupta

                                        Agree but messing with registry is not recommended always, so didn't added that as an option

                                          • Re: Save label caption on userform
                                            Matt Bieringer

                                            Ok so I am using a simple txt file and have the writing to the file down. The issue I am having is when I go do read the data in the file and convert that to a string so I can set the caption I get a space between each character. I have tried to use a replace function but that doesn't remove the spaces.

                                             

                                            Sub ReadFile()
                                            Dim fn As String
                                            Dim txt As String
                                            Dim mtch As Object
                                            Dim m As Object
                                            fn = "B:\Files\Solidworks Files\VBA\AutoLoader\Label.txt"
                                            txt = CreateObject("scripting.filesystemobject").OpenTextFile(fn).ReadAll
                                            With CreateObject("vbscript.regexp")
                                                .Global = True
                                                .Pattern = "[^\n]+"
                                                Set mtch = .Execute(txt)
                                                For Each m In mtch
                                                    .Pattern = "[^\t]+"
                                                Next
                                            End With
                                            txt = Replace(txt, "ÿþ", "")
                                            Debug.Print txt
                                            End Sub
                                            

                                             

                                            This is what it is outputting:

                                            B : \ F i l e s \ S o l i d w o r k s   F i l e s \ V B A \

                                            It also was putting "ÿþ" at that start of the string but I was able to get that to go away.

                                              • Re: Save label caption on userform
                                                Simon Turner

                                                Seems a little complicated. I would do it more like this:

                                                Sub ReadFile()
                                                Dim fn As String
                                                Dim txt As String
                                                Dim fso As New Scripting.FileSystemObject 'Make sure you add a reference to Microsoft Scripting Runtime
                                                Dim myStream As TextStream

                                                fn = "B:\Files\Solidworks Files\VBA\AutoLoader\Label.txt"
                                                Set myStream = fso.OpenTextFile(fn, ForReading)
                                                txt = myStream.ReadLine
                                                myStream.Close

                                                Debug.Print txt
                                                End Sub

                                                 

                                                If you still get the spaces, it could be because the file is in Unicode.

                                                In that case, open it with this line:

                                                Set myStream = fso.OpenTextFile(fn, ForReading, False, 1)

                                • Re: Save label caption on userform
                                  Ivana Kolin

                                  is it because of you want to know last used folder? I would save it in another file and read it by starting macro.

                                  • Re: Save label caption on userform
                                    Matt Bieringer

                                    Thank you everyone for the help here is the code that I have working for both the setting and the reading.

                                     

                                    Read sub:

                                    Sub ReadFile()
                                    Dim fn As String
                                    Dim txt As String
                                    Dim fso As Object
                                    Dim oFile As Object
                                    fn = "B:\Files\Solidworks Files\VBA\AutoLoader\Label.txt"
                                    Set fso = CreateObject("Scripting.FileSystemObject")
                                    Set oFile = fso.OpenTextFile(fn, ForReading)
                                    txt = oFile.ReadLine
                                    oFile.Close
                                    'Debug.Print "1: " & txt
                                    If txt = "" Then
                                        Label1.Caption = "Folder Location"
                                    Else
                                        Label1.Caption = txt
                                    End If
                                    'Debug.Print "2: " & Label1.Caption
                                    ListGen
                                    End Sub
                                    

                                    Set sub:

                                    Private Sub CommandButton1_Click()
                                    Dim Path            As String
                                    Dim FileTxt         As String
                                    Dim fn              As String
                                    Dim fso             As Object
                                    Dim oFile           As Object
                                    Path = BrowseFolder(Caption:="Select A Folder/Path")
                                    If Path = "" Then
                                        MsgBox "Please select the path and try again"
                                    Else
                                        Path = Path & "\"
                                        TextBox1.Text = Path
                                        InitForm.Label1.Caption = TextBox1.Text
                                        'Debug.Print "3: " & TextBox1.Text
                                        
                                        fn = "B:\Files\Solidworks Files\VBA\AutoLoader\Label.txt"
                                        Set fso = CreateObject("Scripting.FileSystemObject")
                                        Set oFile = fso.CreateTextFile(fn, True, False)
                                        oFile.WriteLine Path
                                        oFile.Close
                                    End If
                                    End Sub