PDA

View Full Version : select file



garylee
06-06-2005, 11:19 AM
The structure of the my program is like this::

i. macro run the main function
ii. main function calls up open_from_txt
iii. open_from_txt open the text data file
-- read one line at a time
-- calls up Modify_DWG (another function that modify the drawings)
-- read the next line and calls up Modify_DWG again until end of file


Sub open_from_txt
Dim TXT_FILE As String

'open the text file that contain all the drawings that need to be print
TXT_FILE = "C:\Documents and Settings\CAD4\Desktop\GaryLee\AutoCad Programming\File_To_Print.txt"
Open TXT_FILE For Input As #1

'read until end of file
Do While Not EOF(1)
Input #1, DWG 'assign the dwgs' path to variable DWG
Modify_DWG 'another function that modify the drawings
Loop

Close #1

End Sub


Right now I use a text file to store all the drawings' path.
I would like to use the "select file" window to update my drawings' path

***select file window***
http://www.eng.uwaterloo.ca/%7Ektlee/selectfile.jpg


I have two ideas in my head.

1st: continue use the text file, update the text file everytime
2nd: don't use the text file, instead, use a string array to store the paths

I know how to do the input/output and array.

My first question is : how to obtain the drawing's path info when using
the windows built up "select file window" function??

I did borland programming for so long but I am new to Visual.
There are lots of visual functions are new to me.

My second question is : how can I pass varaible into
CommandButton1_Click() function ??

My Third question is : how can I refer to a varaible(declared in other sub)
when I am in the CommandButton1_Click() function. "For example I would like to use the value of the TXT_FILE(declared in the ThisDrawing/ open_from_txt sub) when I am in the userform1/CommandButton1_Click() function"

I have three questions in total, please feel free to reply to any of them

thank you very mcuh

gary lee

Tommy
06-06-2005, 12:12 PM
'the selectfilewindow should have a dir1.path that gives you the path but I really need to know the exact name of the control you are using
Function GetPath() As String
Dim CPath As String
TXT_FILE = "C:\Documents and Settings\CAD4\Desktop\GaryLee\AutoCad Programming\File_To_Print.txt"
CPath = Left(TXT_FILE, InStrRev(TXT_FILE, "\") - 1)
GetPath = CPath '< sure wish it was return, I like that better, this makes me think it is redefining the function
End Function

'So far as the variables in other sub define a global var as in

Public SomeVar As String
Public TXT_FILE as String

Killian
06-07-2005, 02:27 AM
Hi Gary,

I got your PM...
So I guess this is AutoCAD and it doesn't have a file dialog available as part of it's VBA object model?
You can use the Windows one by calling a Win API function. Below is the code: the type structure to pass to the function needs to be set like this but it isn't required to set all the values (like the handles to the window and app), just the ones you need to get the file path back and customize the dialog.'###declaration for WinAPI function
Declare Function GetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As Long

'### define type to pass to GetOpenFileName function
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

Function ReadTxtFile() As Boolean

Dim OFName As OPENFILENAME

OFName.lStructSize = Len(OFName)
OFName.lpstrFilter = "Text Files (*.txt)" & Chr(0) & "*.txt" & Chr(0) & _
"All Files (*.*)" & Chr(0) & "*.*" & Chr(0)
'create a buffer for the file
OFName.lpstrFile = Space(254)
'set the maximum length of a returned file
OFName.nMaxFile = 255
'Create a buffer for the file title
OFName.lpstrFileTitle = Space(254)
'Set the maximum length of a returned file title
OFName.nMaxFileTitle = 255
'Set the initial directory
OFName.lpstrInitialDir = "C:\"
'Set the title
OFName.lpstrTitle = "Select Text File..."
'No flags
OFName.flags = 0

'Show the 'Open File'-dialog
If GetOpenFileName(OFName) Then
MsgBox "Code here to open this file: " & Trim(OFName.lpstrFile)
'return true if successful
ReadTxtFile = True
Else
MsgBox "Cancel was pressed"
ReadTxtFile = False
End If

End Function

Question 2: if you declare your variable at the top of the form code, it's scope will be module level and available to all routines on the form'###################################################################### ##
'### form/module start
Option Explicit

Dim strA As String ' <--- module scope, visible to all routines/controls

Private Sub CommandButton1_Click()
Dim strB As String ' <--- procedure scope, visible only to CommandButton1
End Sub

Private Sub CommandButton2_Click()
Dim strC As String ' <--- procedure scope, visible only to CommandButton2
End Sub
'### form/module end
'########################################################################Qu estion 3: Again a question of scope. If you declare your variable as Public, it will be available throughout the project (so you'll have to be careful you keep track of when it's modified).
Another alternative would be to write a Public Function in the module where the variable is available that returns it and call that function from the Command_Click event code'###################################################################### ##
'### Userform CommandButton1_Click code
Private Sub CommandButton1_Click()
CommandButton1.Caption = GetstrTemp()
End Sub

'########################################################################
'### Function in module where strA is declared at module level
Public Function GetstrTemp() As String
GetstrTemp = strA
End Function

garylee
06-07-2005, 08:06 AM
To: Killian

you mentioned "file dialog available as part of it's VBA object model."

I didn't write any file dialog program for this project.

Does AutoCAD have any built in value or function that I can use??

All I want is the user be able to select the file in a window display.
when the user selected one or multiply drawings from the display,
The function will return the directary of the drawings and the drawing file name.


Furthermore, I tried your code, however, VB gives a error message

Private Enum and user-defined types cannot be used as parameters or return types for public procedures, public data members, or fields of public user-defined types

Do you know why did I get this message??
What did I do wrong??

Thank you very much
great helper

gary lee

Tommy
06-07-2005, 08:59 AM
gary lee,

Killian has given you some very good pointers.

So far as a Common Dialog Killian mentioned it is in the tools menu, Pick References, a dialog box will come up, in the list there will be Microsoft Common Dialog Control. Check the checkbox. This will palce a control in you project that will allow you to select what and how is displayed.


Function ShowSomeFiles()
Dim CPath As String 'path string
Dim arrFls() As String
Dim ddd As String 'tmp string
Dim CDialog As CommonDialog '< define the dialog box
Set CDialog = New CommonDialog ' create a new reference to it
With CDialog
'give the default file extensions
.DefaultExt = ".txt"
'give a filter for showing the files you want selected
.Filter = "Text Files | *.txt"
'tell the dialog box to just show the text files (the first group of filters help has more details)
.FilterIndex = 1
'the default directory you want to start in
.InitDir = "c:\acad"
'set the dialog title to let user know what you are looking for
.DialogTitle = "Select Text Files"
'set a flag that (for this one) says the file must exist and thay are allowed to select multiple files
.Flags = cdlOFNFileMustExist & cdlOFNAllowMultiselect
' increase buffer to max to allow enough room for multiple files
.MaxFileSize = 32000
End With
CDialog.ShowOpen
' get the return string of selected files
' my example
'"C:\Documents and Settings\Administrator\Desktop\VBAexpress setfileattribute.txt 31556Ph3eje.txt autocadgroup.txt commandbar.txt DDA Report.txt fixmycode.txt getopenfilename.txt Modvbaexpress.txt nodelete.txt pv.txt replaceacadsolidwpoly.txt ResizeRichTextboxControl.txt"
ddd = CDialog.FileName
'get directory with files
arrFls = Split(ddd, Chr(0))
'get directory
CPath = arrFls(0)
'for each file show a message box
For i = 1 To UBound(arrFls)
MsgBox arrFls(i)
Next
End Function

garylee
06-09-2005, 08:29 AM
hi tommy,,

I looked through the VBA reference for a few time, however, just can't find the Microsoft Common Dialog Control type library. Is Microsoft Common Dialog Control the full name of the type library?? I am using VB editor in excel 2000. Can I just download the common dialog control type library from any site??

thank you

garylee
06-09-2005, 09:24 AM
I found the microsoft common dialog control at the toolbox/additional controls.

However, when I try to insert it to a form, error message said "the control could not be created because it is not properly licensed".

Do I need to pay to use the common dialog control??

Tommy
06-09-2005, 10:02 AM
Hi garylee,

I am not sure about having to purchase the control, I have VB and it came with it. One cause could be it is not registered correctly.

You could use Killian's code, the error you are getting is because the code has been copied in the ThisWorkBook/WorkSheet module . You would need to add a module that is not referencing anything else (as in ThisWorkBook or WorkSheet). So in the menu ~ in VBIDE (not excel)~ pick Insert>Module. There will be a new screen pop up in this screen insert the code Killian posted. This should allow you to browse the HD as you want. The code I posted is the same thing other than I used the control and Killian has used API which is using the control without the "programmers" interface.

If you still are haveing trouble let me know :)

garylee
06-09-2005, 10:41 AM
thanks tommy,

I got everything to work except select multi-file.

In killian's code

flags as long
.
.
.
'no flags
flags = 0


I tried to change it to the following, but it still only allow to select one file.
flags = cdlOFNAllowMultiselect

what should I set the flags to???

thx

Tommy
06-09-2005, 11:01 AM
OFName.flags = "&H200" '< will allow multiselect :)

garylee
06-09-2005, 01:10 PM
hi tommy,

this is really the last last question now,, haha

when set flags to ""&H200"", it's true that I can do multi-select.

However, it's give me a different window(old version), see attached image.
http://www.eng.uwaterloo.ca/%7Ektlee/old_win.jpg
using "&H200" (old version window)


http://www.eng.uwaterloo.ca/%7Ektlee/new_win.jpg
without using "&H200" (new window version)

Is that other flag that I can set to which will open with new window version
and multi-file select??

thx buddy

Tommy
06-09-2005, 01:54 PM
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/userinput/commondialogboxlibrary/commondialogboxreference/commondialogboxstructures/openfilename.asp

Whew that was wild! To use mutil select you must also turn on the explorer flag to get the new interface (I did not know this sorry) the above article has all of the information except for the flag "numbers".

OFName.flags = "&H80200"