0

So far the code below is what I am using. It is running on a Siemens HMI (IPC477d) but when I test the script out, the error I get is

An Unhandled exception ('Bad File name or number') occurred in HmiRTm.exe[5984].

I want to know where is the mistake in my code.

The line of Code SmartTags(...) is part of the Software within HMI for handling PLC tags coming through. So it's just a variable in the PLC Code. The variable itself will always be a string value. I dont know if this was important but I thought I would throw that out there for anyone wondering what is happening with that line of code.

Sub CreateTXTReport()
'Tip:
' 1. Use the <CTRL+SPACE> or <CTRL+I> shortcut to open a list of all objects and functions
' 2. Write the code using the HMI Runtime object.
'  Example: HmiRuntime.Screens("Screen_1").
' 3. Use the <CTRL+J> shortcut to create an object reference.
'Write the code as of this position:

    Dim TT,DT
    'If(SmartTags("
    TT = FormatDateTime(Time,3)
    DT = FormatDateTime(Date,0)
    Dim fso, MYfile, strFileName, strFullName, strPath
    strPath = "D:\txtFiles"
    strFileName = "BatchFile_" & DT & "_" & TT & ".txt" 
    SmartTags("BatchFileName") = strFileName
    Const forWriting=1,forReading=2, forAppending=8
    Set fso = CreateObject("Scripting.FileSystemObject")
    strFullName = fso.BuildPath(strPath,strFileName)
    'If (fso.FileExists("D:\txtFiles\" & strFileName)) = True Then
        'Set MYfile = fso.OpenTextFile("D:\txtFiles\" & strFileName, forAppending, True)
    'Else
        'Set MYfile = fso.CreateTextFile("D:\txtFiles\" & strFileName, forWriting, True)
        'MYfile.WriteLine DT & "," & TT & "," & "BatchFile"
    'End If
    If (fso.FileExists(strFullName)) = True Then
        Set MYfile = fso.OpenTextFile(strFullName, forAppending, True)
    Else
        Set MYfile = fso.CreateTextFile(strFullName, forWriting, True)
        MYfile.WriteLine DT & "," & TT & "," & "BatchFile"
    End If
    MYfile.WriteLine(SmartTags("ReportVariable"))

    MYfile.Close
    PrintReport("BatchFile_" & DT & "_" & TT & ".txt" )
End Sub
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
ali aslam
  • 23
  • 6
  • Excuse me, doesn't your **strFileName** have to begin with the slash **'\'**? Or your **strPath** end with it? In order to make a consistent **strFullName**? – statosdotcom Jul 19 '16 at 21:23
  • Well the Commented out code included the slashes but it still threw out the same error. – ali aslam Jul 19 '16 at 21:30
  • 1
    Nice. Are you sure **DT** and **TT** have valid values (not **':'** or some other special char)? – statosdotcom Jul 19 '16 at 21:53
  • 3
    @statosdotcom The `BuildPath` method handles path separators when constructing a path, so you don't have to keep track of which part does or doesn't start or end with a backslash. It's pretty likely that a slash or colon in `DT` or `TT` causes the issue, though. – Ansgar Wiechers Jul 19 '16 at 22:10
  • @Ansgar Wiechers thank you! – statosdotcom Jul 19 '16 at 22:12
  • `strFileName = Replace(Replace("BatchFile_" & DT & "_" & TT & ".txt", "/", ""), ":", "")` – Jim Hewitt Jul 19 '16 at 22:22
  • 1
    @JimHewitt That might help, but generally you have better control if you [construct the timestamps yourself](http://stackoverflow.com/a/24906047/1630171). That way you know what the format looks like without having to rely on the system's locale. – Ansgar Wiechers Jul 19 '16 at 22:54

0 Answers0