Now we can write some code behind the Form.
Forms are special classes and the tools on the form have as series of possible events when certain actions are taken.
For instance when the Cancel button is selected the program must end.
1/ Open the saved drawing and type VBA to go to the VBAIDE and select the form from the project window.
Double click on the Cancel Button and a code window will appear with:
Code: Select all
Private Sub cmdCancel_Click()
End Sub
VBA has created a Private Sub on our cmdCancel button when it is Clicked.
A private sub can only be accessed within our form and no one can access this from IntelliCAD.
All we need to do is type End between the two lines and our Cancel button will now end the program and unload the form.
Your Code should look like:
Code: Select all
Private Sub cmdCancel_Click()
End
End Sub
In the project window double click on the frmConvertDrawings to display the form and press F5
The form will display and click on the Cancel button to close the form -very easy.
2/ Next we are going to create an Initialize event on the form.
This just means that when the form is opened (Initialized) certain actions or events will take place automatically.
To do this double click on the form and the code window will open and you will see this code:
Code: Select all
Private Sub UserForm_Click()
End Sub
This is a click event and we want a Initialize event so look at the upper right area of the code window with a drop down with Click in it. Select the arrow to expose all the events we can have with the form and select Initialize. The adjacent left drop down should have UserForm in it and selecting this you can see all tools that are on the form.
VBA will write this code:
Code: Select all
Private Sub UserForm_Initialize()
End Sub
Just delete the click event on the UserForm i.e.
Code: Select all
Private Sub UserForm_Click()
End Sub
3/ We want to populate our Folders ComboBox when the form is Initialized.
Our Folder structure is:
Main Jobs Folder.
Folder Job 1
Drawing 1
Drawing 2
Drawing...n
Folder Job 2
Drawing 1
Drawing 2
Drawing...n
Folder Job ...............n
Drawing 1
Drawing 2
Drawing...n
We want to display Folder Job 1 to n in our Folders ComboBox and we do this by adding a procedure in our form code window.
The code to do this is:
Code: Select all
Sub ReadFolders(RFPath As String)
' Loop through the Folders in RFPath and add these to a ComboBox named cboFolders
Dim FolderName As String
' Retrieve the first Folder.
FolderName = Dir(RFPath, vbDirectory)
' Start the loop.
Do While FolderName <> ""
' Ignore the current Folder and the encompassing Folder.
If FolderName <> "." And FolderName <> ".." Then
' Use bitwise comparison to make sure FolderName is a Folder.
If (GetAttr(RFPath & FolderName) And vbDirectory) = vbDirectory Then
' Add entry to cboFolders only if it is a Folder
cboFolders.AddItem FolderName
End If
End If
' Get next entry.
FolderName = Dir
Loop
End Sub
All this does is loop through the folders within a certain path and ignore "." and ".." which are the root Folders above the Folders we want and if they are a Folder we use the AddItem method of our ComboBox to populate our cboFolders ComboBox.
To automatically run this procedure we insert the following code between our Initialize code:
Code: Select all
Private Sub UserForm_Initialize()
' Populate the ComboBox cboFolders with the Folder Names
ReadFolders "C:\??????\Main Jobs Folder\"
End Sub
Put in whatever Path you have to point to the Main Jobs Folder and don't forget to add a Backslash.
Try this out by double clicking the Form name in the project window and pressing F5 as previously described.
Tomorrow we will add code to populate the Drawings 1 to n in our list box and set-up our routine to loop through these drawings and save these in R2000 format.
I should add that this could also be used to do batch activities to a range of drawings such as updating title blocks, changing layers or printing drawings. So the project is very useful.