TheSwamp
Code Red => VB(A) => Topic started by: deegeecees on February 03, 2006, 01:16:20 PM
-
I have a batch plot DVB that I slapped together from code fragments that works fairly well, the only thing I'd like to add would be a SAVE LIST, and a IMPORT LISToption. Due to the aggressive nature of the company I work for, I really can't spend the hours researching and developing something on my own here at work, and being a single Father, personal time is hardly an option, hence the call for help. Anybody?
(I'll post the DVB in a little while.)
-
In case anyone is interested, here it is...
BatchPlot (http://www.theswamp.org/lilly_pond/deegeecees/apps/PEI%20Batch%20Plot.dvb)
-
Here's the Write
Public Sub WriteEmAll()
Dim listLeng As Integer
Dim strFileName As String
strFileName = InputBox("What name for this list?", "List Name", CurDir & "\" & Date)
strFileName = strFileName & ".txt"
'write the Text to file
Open strFileName For Append Access Write As #10
For listLeng = 0 To ListBox1.ListCount - 1
Write #10, ListBox1.List(listLeng, 0)
Next listLeng
Close #10
End Sub
This is my calling routine with the question for the user to either retrieve the info from a text file or do the file dialog.
Sub BatchList()
Dim objFD As New FILEDIALOG
Dim strFiles As String
Dim strTFile As String
Dim cb As Integer
On Error GoTo Err_Handler
If MsgBox("Get drawing list from a saved text file?", vbYesNo, "Get Saved List") = vbYes Then
objFD.MultiSelect = False
objFD.Filter = "Text Files" _
& Chr$(0) & "*.txt" & Chr$(0)
strFiles = objFD.ShowOpen
Open strFiles For Input As #1 ' Open file for input.
Do While Not EOF(1) ' Loop until end of file.
Input #1, strTFile
BatchEmAll.ListBox1.AddItem strTFile
Loop
Close #1
Else
objFD.MultiSelect = True
objFD.Filter = "Drawing Files" _
& Chr$(0) & "*.dwg" & Chr$(0)
strFiles = objFD.ShowOpen
FormatMultiReturn (strFiles)
For cb = 1 To NameCount - 1
curName = ExtractedNames(cb)
BatchEmAll.ListBox1.AddItem curName
Next cb
curName = ExtractedNames(0)
BatchEmAll.ListBox1.AddItem curName
End If
Set objFD = Nothing
BatchEmAll.Show
Exit_Here:
Exit Sub
Err_Handler:
Select Case Err.Number
Case 9
Err.Clear
GoTo Exit_Here
Case Else
MsgBox Err.Number & " " & Err.Description
End Select
End Sub
-
Thanks Murph! I have questions, will post in a while (under the gun at the moment).
-
OK, questions:
1. Would both Subs go in a Module?
2. Will I need to add a button?
(I have limited at best knowledge of VBA)
-
The Write goes in the Form and I tied mine to a check box that reads "save this list for future use"
The BatchList goes in a module that I use to call up the batch list form. It has the MsgBox to ask the user if they want to get the list from a text file and goes from there.
-
Dim objFD As New FILEDIALOG
Type not defined
What Type Librarys do you have checked?
-
Whoopsie, forgot that.
Put the following code in a Class Module titles FILEDIALOG
Option Explicit
'//The Win32 API Functions///
Private Declare Function GetSaveFileName Lib _
"comdlg32.dll" Alias "GetSaveFileNameA" _
(pOpenfilename As OPENFILENAME) As Long
Private Declare Function GetOpenFileName Lib _
"comdlg32.dll" Alias "GetOpenFileNameA" _
(pOpenfilename As OPENFILENAME) As Long
Private Declare Function FindWindow Lib "user32" _
Alias "FindWindowA" (ByVal lpClassName As String, _
ByVal lpWindowName As String) As Long
'//A few of the available Flags///
Private Const OFN_FILEMUSTEXIST = &H1000
Private Const OFN_HIDEREADONLY = &H4
Private Const OFN_ALLOWMULTISELECT = &H200
'This one keeps your dialog from turning into
'A browse by folder dialog if multiselect is true!
'Not sure what I mean? Remove it from the flags
'In the "Show Open" & "Show Save" methods.
Private Const OFN_EXPLORER As Long = &H80000
'//The Structure
Private Type OPENFILENAME
lStructSize As Long
hwndOwner As Long
hInstance As Long
lpstrFilter As String
lpstrCustomFilter As String
nMaxCustFilter As Long
nFilterIndex As Long
lpstrFile As String
nMaxFile As Long
lpstrFileTitle As String
nMaxFileTitle As Long
lpstrInitialDir As String
lpstrTitle As String
flags As Long
nFileOffset As Integer
nFileExtension As Integer
lpstrDefExt As String
lCustData As Long
lpfnHook As Long
lpTemplateName As String
End Type
Private lngHwnd As Long
Private strFilter As String
Private strTitle As String
Private strDir As String
Private blnHideReadOnly As Boolean
Private blnAllowMulti As Boolean
Private blnMustExist As Boolean
Private Sub Class_Initialize()
'Set default values when
'class is first created
strDir = CurDir
strTitle = "Llamas Rule"
strFilter = "All Files" _
& Chr$(0) & "*.*" & Chr$(0)
lngHwnd = FindWindow(vbNullString, Application.Caption)
'None of the flags are set here!
End Sub
Public Property Let OwnerHwnd(WindowHandle As Long)
'//FOR YOU TODO//
'Use the API to validate this handle
lngHwnd = WindowHandle
'This value is set at startup to the handle of the
'AutoCAD Application window, if you want the owner
'to be a user form you will need to obtain its
'Handle by using the "FindUserForm" function in
'This class.
End Property
Public Property Get OwnerHwnd() As Long
OwnerHwnd = lngHwnd
End Property
Public Property Let Title(Caption As String)
'don't allow null strings
If Not Caption = vbNullString Then
strTitle = Caption
End If
End Property
Public Property Get Title() As String
Title = strTitle
End Property
Public Property Let Filter(ByVal FilterString As String)
'Filters change the type of files that are
'displayed in the dialog. I have designed this
'validation to use the same filter format the
'Common dialog OCX uses:
'"All Files (*.*)|*.*"
Dim intPos As Integer
Do While InStr(FilterString, "|") > 0
intPos = InStr(FilterString, "|")
If intPos > 0 Then
FilterString = Left$(FilterString, intPos - 1) _
& Chr$(0) & Right$(FilterString, _
Len(FilterString) - intPos)
End If
Loop
If Right$(FilterString, 2) <> Chr$(0) & Chr$(0) Then
FilterString = FilterString & Chr$(0)
End If
strFilter = FilterString
End Property
Public Property Get Filter() As String
'Here we reverse the process and return
'the Filter in the same format the it was
'entered
Dim intPos As Integer
Dim strTemp As String
strTemp = strFilter
Do While InStr(strTemp, Chr$(0)) > 0
intPos = InStr(strTemp, Chr$(0))
If intPos > 0 Then
strTemp = Left$(strTemp, intPos - 1) _
& "|" & Right$(strTemp, _
Len(strTemp) - intPos)
End If
Loop
If Right$(strTemp, 1) = "|" Then
strTemp = Left$(strTemp, Len(strTemp) - 1)
End If
Filter = strTemp
End Property
Public Property Let InitialDir(strFolder As String)
'Sets the directory the dialog displays when called
If Len(Dir(strFolder)) > 0 Then
strDir = strFolder
Else
Err.Raise 514, "FileDialog", "Invalid Initial Directory"
End If
End Property
Public Property Let HideReadOnly(blnVal As Boolean)
blnHideReadOnly = blnVal
End Property
Public Property Let MultiSelect(blnVal As Boolean)
'allow users to select more than one file using
'The Shift or CTRL keys during selection
blnAllowMulti = blnVal
End Property
Public Property Let FileMustExist(blnVal As Boolean)
blnMustExist = blnVal
End Property
'@~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~@
' Display and use the File open dialog
'@~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~@
Public Function ShowOpen() As String
Dim strTemp As String
Dim udtStruct As OPENFILENAME
udtStruct.lStructSize = Len(udtStruct)
'Use our private variable
udtStruct.hwndOwner = lngHwnd
'Use our private variable
udtStruct.lpstrFilter = strFilter
udtStruct.lpstrFile = Space$(254)
udtStruct.nMaxFile = 255
udtStruct.lpstrFileTitle = Space$(254)
udtStruct.nMaxFileTitle = 255
'Use our private variable
udtStruct.lpstrInitialDir = strDir
'Use our private variable
udtStruct.lpstrTitle = strTitle
'Ok, here we test our booleans to
'set the flag
If blnHideReadOnly And blnAllowMulti And blnMustExist Then
udtStruct.flags = OFN_HIDEREADONLY Or _
OFN_ALLOWMULTISELECT Or OFN_EXPLORER Or OFN_FILEMUSTEXIST
ElseIf blnHideReadOnly And blnAllowMulti Then
udtStruct.flags = OFN_ALLOWMULTISELECT _
Or OFN_EXPLORER Or OFN_HIDEREADONLY
ElseIf blnHideReadOnly And blnMustExist Then
udtStruct.flags = OFN_HIDEREADONLY Or OFN_FILEMUSTEXIST
ElseIf blnAllowMulti And blnMustExist Then
udtStruct.flags = OFN_ALLOWMULTISELECT Or _
OFN_EXPLORER Or OFN_FILEMUSTEXIST
ElseIf blnHideReadOnly Then
udtStruct.flags = OFN_HIDEREADONLY
ElseIf blnAllowMulti Then
udtStruct.flags = OFN_ALLOWMULTISELECT _
Or OFN_EXPLORER
ElseIf blnMustExist Then
udtStruct.flags = OFN_FILEMUSTEXIST
End If
If GetOpenFileName(udtStruct) Then
strTemp = (Trim(udtStruct.lpstrFile))
ShowOpen = Mid(strTemp, 1, Len(strTemp) - 1)
End If
End Function
'@~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~@
' Display and use the File Save dialog
'@~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~@
Public Function ShowSave() As String
Dim strTemp As String
Dim udtStruct As OPENFILENAME
udtStruct.lStructSize = Len(udtStruct)
'Use our private variable
udtStruct.hwndOwner = lngHwnd
'Use our private variable
udtStruct.lpstrFilter = strFilter
udtStruct.lpstrFile = Space$(254)
udtStruct.nMaxFile = 255
udtStruct.lpstrFileTitle = Space$(254)
udtStruct.nMaxFileTitle = 255
'Use our private variable
udtStruct.lpstrInitialDir = strDir
'Use our private variable
udtStruct.lpstrTitle = strTitle
If blnMustExist Then
udtStruct.flags = OFN_FILEMUSTEXIST
End If
If GetSaveFileName(udtStruct) Then
strTemp = (Trim(udtStruct.lpstrFile))
ShowSave = Mid(strTemp, 1, Len(strTemp) - 1)
End If
End Function
-
@Murphy,
just some unasked hint you can discard
If you have some spare time to invest, you could look at the
Scripting Runtimelibrary (look in your references it will be there)
and especially the filesystem object.
on the fly..:
Dim Fs as Scripting.Filesystemobject
Set Fs = New Scripting.filesystemobject
Opening these dialogs moving copying files or directory's is way easier that way.
-
I would suggest staying away from the Scripting object.
Use APIs instead. It will take a little longer to learn but
you will have a lower overhead on your project.
Murph has a wonderful FileDialog Class there as a matter
of fact I Use the exact same one.
-
Works every time for me.
I got it from the same place Chuck did.
We used to be on that board together years ago.
-
here is a good place to start for API stuff
http://www.mentalis.org/index2.shtml
You can even download a pretty good API Viewer