|
-
Jun 28th, 2004, 11:45 AM
#1
Thread Starter
New Member
automatic change in directory?
I am working on writing some code in VBA. The purpose of the code is to first have the user locate a file (via a common dialog), nextrename that file to a specific name, then call a .bat file (via a shell command) which runs a small .exe file. After this, a text file is output by the .exe and it is read into two arrays and subsequently plotted using the MS chart control. My code seems to work fine except for one little problem. When I locate a file using the open dialog, the rest of the program will only work if the located file is in the directory of the .exe file. If I pull the located file from anywhere else on the hard drive, the bat file will never run, meaning that the .exe doesn't run and there is no output. I have tried to put the full extensions on all of tmy file names, but I can't figure out what is wrong. It seems like the 'default' directory is changing and then the application can't find the .bat file. But that is just a guess.
I will post the code here. Please note that the code is part of two different forms. I will attempt to make the division clear in the code. Also, I appologize for the sloppiness of the code. I have a few different people working on it and I have been making some changes. Thanks for your help.
---Andy--------------------------------------
Private Sub cmbgMotionOK_Click()
'frmInputfile.Hide
'This opens the input file -AW 6/25/04
dlgOpen1.ShowOpen
inputf = dlgOpen1.FileName
'MsgBox (inputf)
''''''''''''''''''''''''''''''''''''''''''''''''''
'This takes the input file and copies it to the standard file '1.sim' needed for the fortran code.
filnam = inputf
outfil2 = "c:\App77\G77\1.sim"
Set fs = CreateObject("Scripting.FileSystemObject")
Set outfil = fs.CreateTextFile(outfil2, True)
Dim TextLine
k = -1
Open filnam For Input As #1 ' Open file.
Do While Not EOF(1) ' Loop until end of file.
Line Input #1, TextLine ' Read line into variable.
'MsgBox (TextLine)
k = k + 1
'MsgBox (k)
If k = 0 Then
ElseIf (k > 0) Then
outfil.WriteLine (TextLine)
End If
Loop
statnum2 = k
Close #1 ' Close file #1.
outfil.Close ' Close file #2.
''''''''''''''''''''''''''''''''''''''''''''''''''
inputf = outfil2
'frmgMotion.Hide
'frmRunDos.Show
'*********************************************************************************************
'This is the code that actually runs the fortran application.
'The following code is modified from Fan Yang 7/2/02
Dim Cmd As String ', 'Buffer As String,
Dim FileNum As Integer
Dim hProcess As Long, ProcessId As Long, exitCode As Long
' Create the command and write it to a batch file.
'Cmd = "pushd G77" & Chr(10) & "spec002cl1.exe " & Chr(10) & "popd"
'MsgBox (Cmd)
FileNum = FreeFile
'Open BATFILE_NAME For Output As #FileNum
'Print #FileNum, Cmd
'Close #FileNum
' Run the batch file
ProcessId = Shell(BATFILE_NAME, vbMinimizedFocus)
' Wait until the Shell process is completed.
hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, False, ProcessId)
Do
Call GetExitCodeProcess(hProcess, exitCode)
DoEvents
Loop While exitCode = STATUS_PENDING
Call CloseHandle(hProcess)
'Kill BATFILE_NAME
frmgMotion.Hide
frmPlot.Show
End Sub
'Second form code begins here###################
Dim OldRowCount As Long
Dim PenColor As Boolean, ShowMarker As Boolean
Dim ChartPoints() As Double
Dim lRow As Long, lRow2 As Long
Dim XValue As Single, YValue As Single
Dim Curseries As Integer
Dim k As Integer
Dim k2 As Integer
Private Sub cmbPlotNext_Click()
frmPlot.Hide
End
End Sub
Private Sub cmbPlotOK_Click()
'************************************************************************************
'This puts the data from the output file into the arrays for plotting -AW 6/25/04
k2 = 0
Open "C:\App77\G77\output.sim" For Input As #1
While Not EOF(1)
Line Input #1, temp$
number1 = Val(Mid$(temp$, 1, 10))
TimestepArray(k2) = number1
'lstComplete1.AddItem TimestepArray(k2)
number2 = Val(Mid$(temp$, 13, 10))
AccArray(k2) = number2
'lstComplete2.AddItem AccArray(k2)
k2 = k2 + 1
Wend
Close #1
'************************************************************************************
With MSChart1
.chartType = VtChChartType2dXY
.ShowLegend = False
With .Plot.Axis(VtChAxisIdY).AxisTitle
.VtFont.Size = 12
.Visible = True
.Text = "Spectral Acceleration"
End With
With .Plot.Axis(VtChAxisIdX).AxisTitle
.VtFont.Size = 12
.Visible = True
.Text = "Time"
End With
.Title.VtFont.Size = 12
.Title = "Response Spectra"
.Legend.Location.LocationType = VtChLocationTypeBottom
.Plot.Axis(VtChAxisIdY).AxisScale.Type = VtChScaleTypeLinear
.Plot.Axis(VtChAxisIdX).AxisScale.Type = VtChScaleTypeLinear
'Tip from KB article Q194221:
.Plot.UniformAxis = False
'.Footnote.Text = "Footnote goes here"
End With
PenColor = True 'Draw in color
ShowMarker = False 'Don't show plot points
ChartIt
MSChart1.Refresh
'************************************************************************************
'This finds the maximum acceleration value and the corresponding time value for it. -AW 6/25/04
A = 0
B = 0
k = 0
For Rep = 1 To UBound(AccArray)
A = AccArray(k)
If A > B Then
B = A
ind = k
Else
End If
k = k + 1
Next
amax = B
timeamax = TimestepArray(ind)
'MsgBox (B)
'MsgBox (ind)
'MsgBox (timeamax)
MSChart1.Footnote.Text = "Amax = " & amax & " m/s at " & timeamax & " seconds."
'*************************************************************************************
End Sub
Private Sub ChartIt()
Curseries = 1
MousePointer = 11
ReDim ChartPoints(1 To 108, 1 To 10)
k = 0
'Create the array data:
For lRow = 1 To UBound(ChartPoints, 1)
'create the X and Y values:
XValue = TimestepArray(k)
'msgBox (TimestepArray(k))
YValue = AccArray(k)
ChartPoints(lRow, 1) = XValue
ChartPoints(lRow, 2) = YValue
k = k + 1
Next lRow
'We need to increase the ColumnCount. For X-Y Scatter graphs, we
'need 2 columns for each series.
MSChart1.ColumnCount = Curseries * 2
With MSChart1
With .Plot
.Wall.Brush.Style = VtBrushStyleSolid
.Wall.Brush.FillColor.Set 255, 255, 225
If PenColor Then
.Wall.Brush.FillColor.Set 255, 255, 225
'You can set the individual Pen colors here, or just use
'the defaults.
Else 'Based on an article in the VB KB:
'Uncomment the next line if you want the wall color to
'be white:
.Wall.Brush.FillColor.Set 255, 255, 255
'Set the different patterns for Black and White plotting.
'You need to set the Pen for only the 'X' column:
Select Case Curseries * 2 - 1
Case 1
.SeriesCollection(1).Pen.Style = VtPenStyleSolid
.SeriesCollection(1).Pen.VtColor.Set 0, 0, 0
Case 3
.SeriesCollection(3).Pen.Style = VtPenStyleDashed
.SeriesCollection(3).Pen.VtColor.Set 0, 0, 0
Case 5
.SeriesCollection(5).Pen.Style = VtPenStyleDotted
.SeriesCollection(5).Pen.VtColor.Set 0, 0, 0
Case 7
.SeriesCollection(7).Pen.Style = VtPenStyleDitted
.SeriesCollection(7).Pen.VtColor.Set 0, 0, 0
End Select
End If
End With
.ColumnLabelCount = Curseries * 2
'If the current series has more plot points that the previous
'one, we need to change .RowCount accordingly:
If UBound(ChartPoints, 1) > OldRowCount& Then
.RowCount = UBound(ChartPoints, 1)
End If
'Both of the next 2 lines seem to do the same thing:
.Plot.SeriesCollection(Curseries * 2 - 1).SeriesMarker.Show = ShowMarker
.Plot.SeriesCollection.Item(Curseries * 2 - 1).SeriesMarker.Show = ShowMarker
'Create the plot points for this series from the ChartPoints array:
For lRow = 1 To UBound(ChartPoints, 1)
.DataGrid.SetData lRow, Curseries * 2 - 1, ChartPoints(lRow, 1), False
.DataGrid.SetData lRow, Curseries * 2, ChartPoints(lRow, 2), False
Next
'Remove null points from *this* series, if it has *fewer*
'points than the prior ones. If you don't remove null points,
'then the graph will add 0,0 points, erroneously. See MS
'Knowledge Base article Q177685 for more info:
For lRow2 = lRow To OldRowCount&
.DataGrid.SetData lRow2, Curseries * 2 - 1, 0, True
.DataGrid.SetData lRow2, Curseries * 2, 0, True
Next
'Remove null points from *prior* series, if this series
'has *more* points than the prior ones:
If Curseries > 1 Then
For lRow = OldRowCount& + 1 To .RowCount
For lRow2 = 1 To Curseries - 1
.DataGrid.SetData lRow, lRow2 * 2 - 1, 0, True
.DataGrid.SetData lRow, lRow2 * 2, 0, True
Next
Next
End If
'Store the current RowCount
OldRowCount& = .RowCount
.Column = Curseries * 2 - 1
.ColumnLabel = "Series " & Str(Curseries)
.Visible = True
.Refresh
End With
SubExit:
MousePointer = 0
End Sub
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|