hexagon logo

How to Create an "Object" with Basic Script

I am posting this here to have as a reference. Hope this helps people.

1.
Create a variable for the file system object.

Dim objFSO 'As FileSystemObject
Set objFSO = CreateObject("Scripting.FileSystemObject")


2.
Add a string variable for the directory path where you want to put the file and set it to the desired location or assign a variable from a pull-in from PC-Dmis. I use an operator input, or a hardcoded input.

'using a variable from your program.  The name highlighted in paranthesis and selected by quotations is your 'variable name.  Should always be in all caps.  ("HERE")

Dim opname
Set[SIZE=2][COLOR=#000000] opname = PCDPartProgram.GetVariableValue ("OPERATOR")
[/COLOR]
[/SIZE]'or you may use the following method.  Thus pulling in the program path.

Dim[SIZE=2][COLOR=#000000] PCDApp, PCDPartPrograms, PCDPartProgram, PCDCommands
Set[SIZE=2][COLOR=#000000] PCDApp = CreateObject("PCDLRN.Application")[/COLOR]
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Set[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#000000] PCDPartPrograms = PCDApp.PartPrograms[/COLOR]
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Set[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#000000] PCDPartProgram = PCDApp.ActivePartProgram[/COLOR]
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Set[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#000000] PCDCommands = PCDPartProgram.Commands

PCDpartprogram.Path 
[/COLOR][/SIZE][/COLOR][/SIZE]
[SIZE=2]' or assign the directory from the script.  Thus hardcoding it.

[/SIZE]Dim strDirectory 'As String
strDirectory = "C:\NewDirectory"
'or strDirectory = PCDpartprogram.Path
'or Dim strDirectory
'Set strDirectory = [SIZE=2][COLOR=#000000]PCDPartProgram.GetVariableValue [/COLOR][/SIZE]("FOLDERPATH")



3.
Make sure that the directory folder exists. If the directory is not there, then create it using the file system object or you can use the Program Path to save it in the same directory.

Dim objDirectory 'As Object
If objFSO.FolderExists(strDirectory) Then
Set objDirectory = objFSO.GetFolder(strDirectory)
Else
Set objDirectory = objFSO.CreateFolder(strDirectory)
End If



4.

Make a string variable for the file name and set it to an appropriate value.

Dim strFile 'As String
strFile = "NewFile.txt"


or again use the variable assign method. you can also use a message box and pull in the input from there.

Begin[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Dialog[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#000000] DIALOG_1 50,10, 300, 125, oOPERATORINPUT

Dim[SIZE=2][COLOR=#000000] Dialg [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#000000] DIALOG_1[/COLOR]
[/SIZE][/COLOR][/SIZE]TextBox 80,43,50,12, .EditBox_2

Job = Dialg.EditBox_2


5.


Create a variable for the text file and instantiate it. The CreateTextFile method has an optional parameter for overwriting the file it it already exists.

Dim objTextFile 'As Object
Dim blnOverwrite 'As Boolean
blnOverwrite = True
Set objTextFile = objFSO.CreateTextFile(strDirectory & "\" & strFile, blnOverwrite)





6.
Write data to the newly created file with the code below. There are two methods for writing to files, "Write" and "WriteLine." The latter method automatically includes a new line character for you, whereas the former just appends data to what is already present.

objTextFile.Write("This is ")
objTextFile.WriteLine("a new text file")
' This results in the string "This is a new text file"




7.
Close the file with code and properly release all objects.

objTextFile.Close
Set objTextFile = Nothing
Set objDirectory = Nothing
Set objFSO = Nothing
Parents Reply Children
No Data