I have a workbook that writes out several text files for use with a separate program.
I also have a command button that allows the user to select a directory for the output. That code, from "Excel 2010 Power Programming with VBA" is as follows:
Private Sub SetDirectory_Click()
With Application.FileDialog(msoFileDialogFolderPicker)
.InitialFileName = Application.DefaultFilePath & "\"
.Title = "Select a location for the backup."
.Show
If .SelectedItems.Count = 0 Then
MsgBox "Canceled"
Else
MsgBox .SelectedItems(1)
End If
End With
End Sub
The second command button created the text files. However, the code I have is still not directing the text file output to the proper, user selected directory. Below is the code for creating one of the text files:
Dim i As Long
Dim CoName As String ' Variable for Tank Company Name
Dim TankNum As String * 4 ' Variable for Tank Number
Dim Locat As String ' Variable for Location of Tank (City,State)
Dim DY As Single ' Variable for Nodal Displacement
Dim YM As Single ' Variable for Young's Modulus
Dim PR As Single ' Variable for Poisson's Ratio
Dim R1 As Single ' Variable for Real Constant 1 - Tank Bottom Thickness
Dim R2 As Single ' Variable for Real Constant 2 - Tank Shell Thickness
Dim intFH As Integer ' Dummy integer variable for opening text file
' Create and Open the file Empty.txt. This is the ANSYS Macro file to create the FE Model
intFH = FreeFile()
Open "EMPTY.txt" For Output As intFH
' Assign Cell values to specific variables
YM = Cells(14, 3).Value ' Get Young's Modulus from Worksheet
PR = Cells(15, 3).Value ' Get Poisson's Ratio from Worksheet
R1 = Cells(11, 3).Value ' Get Real Constant 1 from Worksheet
R2 = Cells(12, 3).Value ' Get Real Constant 2 from Worksheet
CoName = Cells(3, 2).Value ' Get Tank Company Name from Worksheet
TankNum = Cells(4, 3).Value ' Get Tank Number from Worksheet
Locat = Cells(5, 2).Value ' Get Location of Tank (City,State)
'Write Information to the EMPTY.txt file - ANSYS Macro
Print #intFH, "EMPTY"
Print #intFH, "/prep 7"
Print #intFH, "/TITLE," & CoName & "Tank Number" & TankNum & "at" & Locat ' Title for FE Model
Print #intFH, "ET,1,SHELL51" 'Set Element type for ANSYS Model
Print #intFH, "MP,EX,1," & YM ' Get Young's Modulus from Worksheet
Print #intFH, "MP,PRXY,1," & PR 'Get Poisson's Ratio from Worksheet
Print #intFH, "R,1," & R1 'Get Real Constant 1 (Tank Bottom Thickness) from Worksheet
Print #intFH, "R,2," & R2 'Get Real Constant 2 (Tank Shell Thickness) from Worksheet
Print #intFH, "NREAD,Node,txt" ' Read in Nodes for FE Model
Print #intFH, "EREAD,Element,txt" ' read in Elements for FE Model
' Loop to print out node displacements
For i = 1 To 40
DY = Cells(i + 4, 12).Value
Print #intFH, "D," & i & ",UY," & DY
Next i
Print #intFH, "finish"
Print #intFH, "/EOF"
'Close File
Close #intFH
I imagine I need a line of code to set the directory but I am not sure what it would be. Any help is greatly appreciated.
I also have a command button that allows the user to select a directory for the output. That code, from "Excel 2010 Power Programming with VBA" is as follows:
Private Sub SetDirectory_Click()
With Application.FileDialog(msoFileDialogFolderPicker)
.InitialFileName = Application.DefaultFilePath & "\"
.Title = "Select a location for the backup."
.Show
If .SelectedItems.Count = 0 Then
MsgBox "Canceled"
Else
MsgBox .SelectedItems(1)
End If
End With
End Sub
The second command button created the text files. However, the code I have is still not directing the text file output to the proper, user selected directory. Below is the code for creating one of the text files:
Dim i As Long
Dim CoName As String ' Variable for Tank Company Name
Dim TankNum As String * 4 ' Variable for Tank Number
Dim Locat As String ' Variable for Location of Tank (City,State)
Dim DY As Single ' Variable for Nodal Displacement
Dim YM As Single ' Variable for Young's Modulus
Dim PR As Single ' Variable for Poisson's Ratio
Dim R1 As Single ' Variable for Real Constant 1 - Tank Bottom Thickness
Dim R2 As Single ' Variable for Real Constant 2 - Tank Shell Thickness
Dim intFH As Integer ' Dummy integer variable for opening text file
' Create and Open the file Empty.txt. This is the ANSYS Macro file to create the FE Model
intFH = FreeFile()
Open "EMPTY.txt" For Output As intFH
' Assign Cell values to specific variables
YM = Cells(14, 3).Value ' Get Young's Modulus from Worksheet
PR = Cells(15, 3).Value ' Get Poisson's Ratio from Worksheet
R1 = Cells(11, 3).Value ' Get Real Constant 1 from Worksheet
R2 = Cells(12, 3).Value ' Get Real Constant 2 from Worksheet
CoName = Cells(3, 2).Value ' Get Tank Company Name from Worksheet
TankNum = Cells(4, 3).Value ' Get Tank Number from Worksheet
Locat = Cells(5, 2).Value ' Get Location of Tank (City,State)
'Write Information to the EMPTY.txt file - ANSYS Macro
Print #intFH, "EMPTY"
Print #intFH, "/prep 7"
Print #intFH, "/TITLE," & CoName & "Tank Number" & TankNum & "at" & Locat ' Title for FE Model
Print #intFH, "ET,1,SHELL51" 'Set Element type for ANSYS Model
Print #intFH, "MP,EX,1," & YM ' Get Young's Modulus from Worksheet
Print #intFH, "MP,PRXY,1," & PR 'Get Poisson's Ratio from Worksheet
Print #intFH, "R,1," & R1 'Get Real Constant 1 (Tank Bottom Thickness) from Worksheet
Print #intFH, "R,2," & R2 'Get Real Constant 2 (Tank Shell Thickness) from Worksheet
Print #intFH, "NREAD,Node,txt" ' Read in Nodes for FE Model
Print #intFH, "EREAD,Element,txt" ' read in Elements for FE Model
' Loop to print out node displacements
For i = 1 To 40
DY = Cells(i + 4, 12).Value
Print #intFH, "D," & i & ",UY," & DY
Next i
Print #intFH, "finish"
Print #intFH, "/EOF"
'Close File
Close #intFH
I imagine I need a line of code to set the directory but I am not sure what it would be. Any help is greatly appreciated.