Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

select file dialog box in vbscript?? 3

Status
Not open for further replies.

kskinne

Technical User
Oct 8, 2004
169
US
I recently got some vbscript put together and working correctly (because of the help on this forum, thank you), that prompts a user with an inputbox in which they enter a filename, and this filename is used as the datasource in a dts package:

Code:
Function Main()
  Dim strFileName
  strFileName = InputBox("Enter file name without the file extension:")
  DTSGlobalVariables("gVarSourceFileName").Value = "C:\Documents and Settings\kskinner\Desktop\NBC\Qwest CDRs\" & strFileName & ".csv"
  DTSGlobalVariables("gVarImport_FileValue").Value = strFileName
  Main = DTSTaskExecResult_Success
End Function

Is there vbscript code that I can use to replace the inputbox, and instead use a dialog box, whereby the user can browse for, and select, the file they want to use as the datasource?

Thank you,
Kevin
 
Sorry Kevin, after I posted I realized that seems to be the openfiledialog for some ASP add-on. So I had to figure out how to really do it. Try this:

Code:
Set OpenFileDialog = CreateObject("MSComDlg.CommonDialog")
With OpenFileDialog
.DialogTitle = "Some Title"
.Filter = "Text Files|*.txt"
.FilterIndex = 4
.MaxFileSize = 1024
.InitDir = "C:\"
.ShowOpen
End With

str = OpenFileDialog.FileName

msgBox str

Hope it helps,

Alex

Ignorance of certain subjects is a great part of wisdom
 
Alex thanks, this works great - the only thing is, is there a property I can use with the openfiledialog, in place of .FileName, that will return just the name of the file and not the full path and file name?

Thank you,
Kevin
 
Not that I know of, but you could capture the full path, and use the string manipulation functions in vbScript to capture everything to the right of your final '\'

Something like this:

Code:
str2 = Right(str, InStr(StrReverse(str), "\" )-1)

msgBox str2

Hope it helps,

Alex

Ignorance of certain subjects is a great part of wisdom
 
Alex that's kind of what I thought after not being able to find any members in the OpenFileDialog class that would return the results I needed - so that's the route I went, and this is what I'm using:

Code:
Function Main()
Set OpenFileDialog = CreateObject("MSComDlg.CommonDialog")
With OpenFileDialog
    .DialogTitle = "Select Data Source"
    .Filter = "All Files|*.*"
    .FilterIndex = 4
    .MaxFileSize = 1024
    .InitDir = "C:\Documents and Settings\kskinner\Desktop\NBC\Qwest CDRs\"
    .ShowOpen
End With
MyStr = OpenFileDialog.FileName
MyChrPos = InStrRev(MyStr, "\")
MyStrLen = Len(MyStr)
MyFileName = Mid(MyStr, (MyChrPos + 1), ((MyStrLen - MyChrPos) - 4))
DTSGlobalVariables("gVarData_Source").Value = MyStr
DTSGlobalVariables("gVarImport_File").Value = MyFileName
Main = DTSTaskExecResult_Success
End Function

Thanks for your help, I appreciate it

Kevin
 
Alex - the code you gave me works great on my workstation but I get a runtime error on a different workstation when it tries to run this part of the code:

Set OpenFileDialog = CreateObject("MSComDlg.CommonDialog")
With OpenFileDialog
.DialogTitle = "Select Data Source"
.Filter = "All Files|*.*"
.FilterIndex = 4
.MaxFileSize = 1024
.InitDir = "C:\Documents and Settings\kskinner\Desktop\NBC\Qwest CDRs\"
.ShowOpen
End With

It doesn't matter if I'm logged in or under a different login. Any idea why?

Thank you,
Kevin
 
I would guess that your specified initial directory does not exist on the workstation in question. Maybe try setting it to something generic like "C:\"?

I am not sure if there are any issues tha could arise from not having the proper VBS runtime on the client machine, but that may also be something that needs to be looked into.

Hope this helps,

Alex

[small]----signature below----[/small]
I don't do any programming whatsoever

Ignorance of certain subjects is a great part of wisdom
 
Alex I also posted this question in the vbscript forum, and they said it could be because MSComDlg.CommonDialog isn't part of the Windows OS. They recommended that I try this instead:

Set objDialog = CreateObject("UserAccounts.CommonDialog")
objDialog.Filter = "All Files|*.*"
objDialog.InitialDir = "S:\NBC\GC\CDRs\"
intResult = objDialog.ShowOpen
If intResult = 0 Then
Wscript.Quit
Else
MyStr = objDialog.FileName
End If

So I will give this a try - but there must be a way to get this working on that machine then. From doing some searching I think the comdlg32.ocx file is the activeX control file for this, is that correct? If I can download that, where would I need to save it in order for it to work?

Thanks,
Kevin
 
Kevin - I would go with what the vbScripters recommend. I really don't use vbScript much at all outside of DTS packages. See if their suggestion works on the other machine.

If it doesn't, I believe you are correct about that .ocx being the control for the Common Dialog, but I would not know the first thing about where to install it.

Let me know how the other thing works out for you, as I may have a need for something like this in the future.

Thanks,

Alex

[small]----signature below----[/small]
I don't do any programming whatsoever

Ignorance of certain subjects is a great part of wisdom
 
Alex fyi - that other snippet of code that I posted above, worked fine on both machines, so I replaced my previous code with that - thanks for your help

Kevin
 
Hey, thank YOU for sharing the results :)

[small]----signature below----[/small]
I don't do any programming whatsoever

Ignorance of certain subjects is a great part of wisdom
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top