×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Macro which save data a text file...
2

Macro which save data a text file...

Macro which save data a text file...

(OP)
hello.
I find macro which allow save data in excell file.But i want save data in text file.How make that?Do You have any ideas?I willbe graetfull for yours help...

RE: Macro which save data a text file...

2
One way to write to a text file is to use the File System Object and the WriteLine method.  A web search for these terms should turn up plenty of tutorials and examples.  You will need to include the Microsoft Scripting Runtime reference in your macro under Tools->References.

RE: Macro which save data a text file...

CODE

Sub WriteSomeStuffToATextFile()
Dim fso As Scripting.FileSystemObject
Dim sFilePath As String
Dim myOutFile As Scripting.TextStream
Dim i As Long

Set fso = CreateObject("Scripting.FileSystemObject")
sFilePath = "C:\TEMP\OutFile.txt"

Set myOutFile = fso.OpenTextFile(sFilePath, ForWriting, Create:=True)

myOutFile.WriteLine "Here is the first line"
myOutFile.WriteLine "Here is the second line"

For i = 3 To 9
    myOutFile.WriteLine "Here is line number " & i
Next i

myOutFile.Write "All of this text is on one line"
myOutFile.Write " because we used ""Write"" instead of ""WriteLine"""

myOutFile.WriteBlankLines 4

myOutFile.WriteLine "This last line comes after 4 blank lines."

myOutFile.Close

Set myOutFile = Nothing
Set fso = Nothing

End Sub

RE: Macro which save data a text file...

(OP)
thanks handleman that
is it!

RE: Macro which save data a text file...

(OP)
hello handleman. I have a problem. When I try run macro which You write i have a message: Type of definition is non define, and this line is lighted
Dim fso As Scripting.FileSystemObject
In my version SolidWorks i don't have something like: Scripting, so I cannot difine fso like that...
I use SolidWorks2005 and maybe tahts is& the problem..; What You think?

RE: Macro which save data a text file...

Scripting is a different library - not part of SolidWorks.  It should already be in your Windows installation.  You need to choose Tools->References in your VBA editor.  Look for "Microsoft Scripting Runtime" and check the box beside it.

RE: Macro which save data a text file...

(OP)
Exactlly this is it... I forgot check library in VBA... Thanks for YOu oncemore!

RE: Macro which save data a text file...

(OP)
Hello... I have oncemore problem... I want save in text file a data for example: center of selected surface... not only text...I Try combine your macro handleman but i don't know how... It is possible save in text file coordinates of center of surface (XYZ)?If You can and have a time please help.. This is example code sources:
Sub main()
    Dim swApp                   As SldWorks.SldWorks
    Dim swModel                 As SldWorks.ModelDoc2
    Dim swSelMgr                As SldWorks.SelectionMgr
    Dim swFace                  As SldWorks.Face2
    Dim swSurf                  As SldWorks.Surface
    Dim Feature                 As SldWorks.Feature
    Dim MathPoint               As SldWorks.MathPoint
    Dim RefPoint                As SldWorks.RefPoint
    Dim vRefPointFeatureArray   As Variant
    Dim XYZ                     As Variant
    Dim fso As Scripting.FileSystemObject
    Dim sFilePath As String
    Dim myOutFile As Scripting.TextStream
   
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    Set swSelMgr = swModel.SelectionManager
    Set swFace = swSelMgr.GetSelectedObject5(1)
    Set fso = CreateObject("Scripting.FileSystemObject")
    sFilePath = "C:\TEMP\OutFile.txt"
    Set swSurf = swFace.GetSurface
    
    vRefPointFeatureArray = swModel.FeatureManager.InsertReferencePoint(4, 0, 0.01, 1)
    Set Feature = vRefPointFeatureArray(0)
    Set RefPoint = Feature.GetSpecificFeature2
    Set MathPoint = RefPoint.GetRefPoint
    XYZ = MathPoint.ArrayData
    Set MathPoint = Nothing
    Set RefPoint = Nothing
    Set Feature = Nothing
    swApp.SendMsgToUser " Center of selected surface: " _
                        & vbCrLf _
                        & vbCrLf & " X = " & XYZ(0) * 1000 & " mm" _
                        & vbCrLf & " Y = " & XYZ(1) * 1000 & " mm" _
                        & vbCrLf & " Z = " & XYZ(2) * 1000 & " mm"
Set myOutFile = fso.OpenTextFile(sFilePath, ForWriting, Create:=True)
myOutFile.WriteLine "Here is the first line"
myOutFile.Close
Set myOutFile = Nothing
Set fso = Nothing
End Sub

RE: Macro which save data a text file...

You can write any text to the file.  All it needs is a string.  You can use the same string as you used for swApp.SendMsgToUser.  Like this:

CODE

Sub main()
    Dim swApp                   As SldWorks.SldWorks
    Dim swModel                 As SldWorks.ModelDoc2
    Dim swSelMgr                As SldWorks.SelectionMgr
    Dim swFace                  As SldWorks.Face2
    Dim swSurf                  As SldWorks.Surface
    Dim Feature                 As SldWorks.Feature
    Dim MathPoint               As SldWorks.MathPoint
    Dim RefPoint                As SldWorks.RefPoint
    Dim vRefPointFeatureArray   As Variant
    Dim XYZ                     As Variant
    Dim fso As Scripting.FileSystemObject
    Dim sFilePath As String
    Dim myOutFile As Scripting.TextStream
    Dim Msg As String
   
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    Set swSelMgr = swModel.SelectionManager
    Set swFace = swSelMgr.GetSelectedObject5(1)
    Set fso = CreateObject("Scripting.FileSystemObject")
    sFilePath = "C:\TEMP\OutFile.txt"
    Set swSurf = swFace.GetSurface
    
    vRefPointFeatureArray = swModel.FeatureManager.InsertReferencePoint(4, 0, 0.01, 1)
    Set Feature = vRefPointFeatureArray(0)
    Set RefPoint = Feature.GetSpecificFeature2
    Set MathPoint = RefPoint.GetRefPoint
    XYZ = MathPoint.ArrayData
    Set MathPoint = Nothing
    Set RefPoint = Nothing
    Set Feature = Nothing
    Msg = " Center of selected surface: " _
                        & vbCrLf _
                        & vbCrLf & " X = " & XYZ(0) * 1000 & " mm" _
                        & vbCrLf & " Y = " & XYZ(1) * 1000 & " mm" _
                        & vbCrLf & " Z = " & XYZ(2) * 1000 & " mm"
swApp.SendMsgToUser Msg

Set myOutFile = fso.OpenTextFile(sFilePath, ForWriting, Create:=True)
myOutFile.WriteLine Msg
myOutFile.Close
Set myOutFile = Nothing
Set fso = Nothing
End Sub

RE: Macro which save data a text file...

(OP)
Yeah... this is great... but how make that macro willbe save more data in text file and don't delete earlier saved data in this text file only add in writing below earlier saved text... For example I want not only save center of surface, taht for example length on line...

RE: Macro which save data a text file...

The line "Set myOutFile = fso.OpenTextFile...." opens a file for writing.  It will stay open until either your macro is finished or you close it.  If you want to add text to this file all through your macro then put this line towards the beginning of your macro and don't use "myOutFile.Close" until the end of your macro.  If the file already exists before you run the macro and you want to add text to the end then use "ForAppending" instead of "ForWriting" in the "OpenTextFile" function.

You could have found this answer quicker by searching the internet for either "OpenTextFile" or "TextStream".

RE: Macro which save data a text file...

(OP)
Your advices are very helpful for me, so I'm greatful.Thanks!

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Resources