7 Replies Latest reply on May 9, 2017 6:36 AM by Raymond Yau

    How to write to text file - Macro

    Raymond Yau

      Hi,

       

      This isn't really SW related, but rather a VBA question. I have some code which tests a number of conditions using several Elseif statements, and it defaults to an Else statement if none of the pre-existing conditions match. The intention of the Else statement is to create a text file and then add the file name of the Solidworks File that the macro is running on using Writeline.

       

      Currently, the existing code is programmed in a way that everytime the macro runs, it overwrites the text file that already exists. Instead I would want to have it add to an existing text file without overwriting the file names that already existing within the file.

       

      ...

      Else

       

      Dim fso As Object

      Set fso = CreateObject("Scripting.FileSystemObject")

      Dim oFile As Object

      Set oFile = fso.CreateTextFile("C:\Users\Anon\Desktop\test.txt") 'Tried booleans, can't seem to get this to work.

      oFile.WriteLine "File name - " & swModel.GetTitle

      oFile.Close

      Set fso = Nothing

      Set oFile = Nothing

       

      End If

       

      Thoughts?

        • Re: How to write to text file - Macro
          VIGNESH NAGARAJAN

          Hi Raymond,

           

          If you want to create a Text file separately based on your output, write it as a function to create text file and append it instead of using it in the elseif statement.

           

          Regards,

          Vignesh

          • Re: How to write to text file - Macro
            Habib Ghalamkari

            this will read the content of the text file then add  "This is a new test" to the bottom line.

            and then open the file for preview.

            It won't recognize empty lines. I'm in a hurry now. I'll try to correct it later to be able to recognize the empty lines too later.

             

            Public Sub CreateTextFile()

                Dim f

                Dim i As Integer

                Dim FilePath

                Dim strLine As String

                Dim strPreviousText As String

              

                FilePath = "D:\temp.txt"

                      

                Open FilePath For Input As #1

                i = 1

                While EOF(1) = False

                    'read the next line of data in the text file

                    Line Input #1, strLine

                    'print the data in the current row

                    strPreviousText = strPreviousText & vbCrLf & strLine

                    'increment the row counter

                    i = i + 1

                Wend

                Close #1

             

                'Open the so you can write to it.

                Open FilePath For Output As #1

                'Write the Variable to the file

                Print #1, strPreviousText & vbCrLf & "this is a new test"

                'Close (and save) the file

                Close #1

              

              

              

             

                f = "notepad.exe " & FilePath

                'Open the file and view the variable

                Shell "notepad.exe " & FilePath, vbNormalFocus

              

                End Sub

            • Re: How to add to write to text file - Macro
              Nick Gvk

              the reason why that does not work is because s u are telling it to create a new text file called test.txt  every single time u run the macro

              macro`s just overwrite data without asking

               

              u need to add

               

               

                  Dim FilePath As String

                  Dim CheckFile As String

               

               

                  FilePath = "C:\Users\Anon\Desktop\test.txt"

                  CheckFile = ""

                

                

                  On Error Resume Next

                  CheckFile = Dir(FilePath)

                  On Error GoTo 0

                  If CheckFile = "" Then

                  Set oFile = fso.CreateTextFile("C:\Users\Anon\Desktop\test.txt")

                      MsgBox "File din't exist File Created ! "

                  Else

                  Set oFile = fso.OpenTextFile("C:\Users\Anon\Desktop\test.txt")

                

                  End If

              • Re: How to write to text file - Macro
                Simon Turner

                To append to a file, you need to use OpenTextFile rather than CreateTextFile:

                Set oFile = fso.OpenTextFile("C:\Users\Anon\Desktop\test.txt" ,ForAppending , True)

                The last "True" means that if the file does not exist, then it will create it.