Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations GregLocock on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Macro which save data a text file... 2

Status
Not open for further replies.

picia

Mechanical
Mar 24, 2006
26
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...
 
Replies continue below

Recommended for you

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.
 
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
 
thanks handleman that
is it!
 
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?
 
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.
 
Exactlly this is it... I forgot check library in VBA... Thanks for YOu oncemore!
 
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
 
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
 
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...
 
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".
 
Your advices are very helpful for me, so I'm greatful.Thanks!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor