jjackson2004
asked on
Windows 7 CommonDialog class
I have this old script that works with XP, but Win 7 does not support UserAccounts.CommonDialog. Apparently it is now OpenFileDialog. I have been googling and reading about it, but I am not strong on this, so would anyone mind showing me how to update the following script to work in Win 7 (it would be great if it still worked in XP as well). The examples I have been finding seem to be overly complicated.
Dim objDialog as variant
Dim intResult as integer
Dim cFileName as string
Dim sTmp as string
dim oShell as variant
oShell = createobject("wscript.shel l")
sTmp = oShell.CurrentDirectory
objDialog = CreateObject("UserAccounts .CommonDia log")
rem objDialog.Filter = "Word/Excel Documents|*.doc|Text Files|*.txt|All Files|*.*"
objDialog.Filter = "All Files|*.*|Word Documents|*.doc |Excel Files|*.xls"
objDialog.FilterIndex = 1
objDialog.InitialDir = "C:\mswams\mergdocs"
intResult = objDialog.ShowOpen
if (intResult>0) then
cFileName = objDialog.FileName
PanelFieldPutData("D1",0,c FileName)
end if
rem if (intResult=0) then
rem messagebox("Word Merge","You must select a document to use!",4,0)
rem goto exitprg
rem end if
ReleaseObject(objDialog)
oShell.CurrentDirectory = sTmp
Dim objDialog as variant
Dim intResult as integer
Dim cFileName as string
Dim sTmp as string
dim oShell as variant
oShell = createobject("wscript.shel
sTmp = oShell.CurrentDirectory
objDialog = CreateObject("UserAccounts
rem objDialog.Filter = "Word/Excel Documents|*.doc|Text Files|*.txt|All Files|*.*"
objDialog.Filter = "All Files|*.*|Word Documents|*.doc |Excel Files|*.xls"
objDialog.FilterIndex = 1
objDialog.InitialDir = "C:\mswams\mergdocs"
intResult = objDialog.ShowOpen
if (intResult>0) then
cFileName = objDialog.FileName
PanelFieldPutData("D1",0,c
end if
rem if (intResult=0) then
rem messagebox("Word Merge","You must select a document to use!",4,0)
rem goto exitprg
rem end if
ReleaseObject(objDialog)
oShell.CurrentDirectory = sTmp
ASKER
I tried the vbs and js and answered my own question as to them. It needs to be modified to run as either of them.
Hi, as far as I know, there is no other direct COM object for scripts, but you can leverage the HTML Helper class by using a HTA instead. Save this code as a HTA file, and give it a go.
Regards,
Rob,
Regards,
Rob,
<head>
<title>File Browse Dialog</title>
<HTA:APPLICATION
APPLICATIONNAME="File Browe Dialog"
BORDER="thin"
SCROLL="no"
SINGLEINSTANCE="yes"
WINDOWSTATE="normal"
>
</head>
<script language="VBScript">
Sub Window_onLoad
intWidth = 800
intHeight = 600
Me.ResizeTo intWidth, intHeight
Me.MoveTo ((Screen.Width / 2) - (intWidth / 2)),((Screen.Height / 2) - (intHeight / 2))
End Sub
Sub OpenFile
's = Dlg.openfiledlg(, , CStr("Windows Installer Files(*.msi;*.msm)|*.msi;*.msm|"), CStr("Choose File"))
s = Dlg.openfiledlg(, , CStr("Text Files(*.txt;*.csv)|*.txt;*.csv|"), CStr("Choose File"))
If (Len(s) = 0) Then
MsgBox "No file was selected."
Else
'--strange HTMLDlgHelper behavior. Returns a string ending with nulls.
'-- The nulls won't affect using the string, but they will matter if you test the string.
'-- For instance: If UCase(Right(s, 3)) = "TXT" Then .... That won't work unless the nulls are snipped.
'-- so check for nulls. If first null is first character that will return "". Otherwise there's a path string to
'-- extract from the string buffer.
i2 = InStr(s, Chr(0))
If i2 > 1 Then s = Left(s, (i2 - 1))
MsgBox "The selected file is " & s
End If
End Sub
</script>
<body>
<table width='90%' height = '100%' align='center' border='0'>
<tr>
<td align=center>
<INPUT NAME="btnOpenFile" TYPE="button" TITLE="Browse to select file." VALUE="Open File" ONCLICK="OpenFile"></INPUT>
</td>
</tr>
</table>
<OBJECT ID=Dlg CLASSID="CLSID:3050F4E1-98B5-11CF-BB82-00AA00BDCE0B" width=0 height=0> </OBJECT>
</body>
ASKER
I cannot use html with this application that I am aware of.
Hmmm, you may need to do some sort of "wrapper" from your application that then launches the HTA.
Your application could run a VBScript that runs
mshta \\server\share\MyHTA.hta
then the HTA could write the selected file to a local file, which the parent VBScript would then pick up and send back to your application.
Rob.
Your application could run a VBScript that runs
mshta \\server\share\MyHTA.hta
then the HTA could write the selected file to a local file, which the parent VBScript would then pick up and send back to your application.
Rob.
ASKER
We seem to be missing the point, I need assistance modifying the script, first and foremost. I can test it in the program.
Well, there's also the BrowseForFolder method of the Shell.Application object, but that doesn't appear to work on Windows 7 to select files anymore (although oddly, folders do work).
You can also create script the creation of a HTML page to select a file:
Regards,
Rob.
You can also create script the creation of a HTML page to select a file:
Option Explicit
WScript.Echo "Selected file: " & ChooseFile( )
Function ChooseFile( )
' Select File dialog based on a script by Mayayana
' Known issues:
' * Tree view always opens Desktop folder
' * In Win7/IE8 only the file NAME is returned correctly, the path returned will always be C:\fakepath\
' * If a shortcut to a file is selected, the name of that FILE will be returned, not the shortcut's
On Error Resume Next
Dim objIE, strSelected
ChooseFile = ""
Set objIE = CreateObject( "InternetExplorer.Application" )
objIE.visible = False
objIE.Navigate( "about:blank" )
Do Until objIE.ReadyState = 4
Loop
objIE.Document.Write "<HTML><BODY><INPUT ID=""FileSelect"" NAME=""FileSelect"" TYPE=""file""><BODY></HTML>"
With objIE.Document.all.FileSelect
.focus
.click
strSelected = .value
End With
objIE.Quit
Set objIE = Nothing
ChooseFile = strSelected
End Function
Regards,
Rob.
ASKER
thank you for your efforts, but again, I do not wish to pursue a html solution, what I am looking for is to update the existing script if there is a way for it to work with both xp and win 7, otherwise, I can add the win 7 script to this one and just check for the OS and then execute the appropriate script.
Hi, in that case, I don't know of anything native that does work for both operating systems. You need to employ a different approach for each. You will need to use a function to check for the OS, as you mentioned, and branch out to a different method.
There is code here that should work on both, but it requires that Powershell is installed, although that shouldn't be a problem, since it comes with Windows 7:
http://todayguesswhat.blogspot.com.au/2012/08/windows-7-replacement-for.html
and there's a script here that uses a bunch of objects, depending on what works:
https://gist.github.com/wangye/1932941
Rob.
There is code here that should work on both, but it requires that Powershell is installed, although that shouldn't be a problem, since it comes with Windows 7:
http://todayguesswhat.blogspot.com.au/2012/08/windows-7-replacement-for.html
and there's a script here that uses a bunch of objects, depending on what works:
https://gist.github.com/wangye/1932941
Rob.
Just out of curiosity, do you have a method that works with Windows 7, without using MS Office?
Rob.
Rob.
ASKER
No, this is the first exposure to common dialog with win 7. One of my clients wants to standardize on win 7, but I need to get this script error fixed.
OK, so try the approaches I have linked to above, and if one of those works, I can help you combine with your existing Windows XP one.
Rob.
Rob.
ASKER
I am setting up the new laptop so that I can test.
ASKER
Cannot use the powershell option since it requires the installation of powershell. The other script is overly complicated. It seems overkill to replace 15 lines of code with 150. So I am back where I started.
ASKER
I was finally able to get a browse windows to show up using
oTest = CreateObject("Scripting.Fi leSystemOb ject")
objDialog = CreateObject("Shell.Applic ation")
intResult = objDialog.BrowseForFolder( 0, "Choose a File", 16384)
but it is not selection type window I would prefer. But question about this on is does anyone know of a method for that class where I can put in the filters for the type of files that it shows (i.e. DOC, XLS, PDF)?
thanks
oTest = CreateObject("Scripting.Fi
objDialog = CreateObject("Shell.Applic
intResult = objDialog.BrowseForFolder(
but it is not selection type window I would prefer. But question about this on is does anyone know of a method for that class where I can put in the filters for the type of files that it shows (i.e. DOC, XLS, PDF)?
thanks
ASKER
apparently the oTest is not necessary.
The BrowseForFolder method (http://msdn.microsoft.com/en-us/library/windows/desktop/bb774065(v=vs.85).aspx) does not support a file filter, and I couldn't get the file selection to work with Windows 7. The only thing that would work with that is folder selection.
I have taken out one of the options from the github code I linked to that works for me on Windows 7 and Windows XP.
Regards,
Rob.
I have taken out one of the options from the github code I linked to that works for me on Windows 7 and Windows XP.
Regards,
Rob.
strFile = OpenFileDialog
If strFile = "" Then
WScript.Echo "You did not select a file."
Else
WScript.Echo "You selected: " & strFile
End If
Function OpenFileDialog
' Bypasses c:\fakepath\file.txt problem
' http://pastebin.com/txVgnLBV
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
Dim shell
Set shell = CreateObject("WScript.Shell")
Dim tempFolder : Set tempFolder = fso.GetSpecialFolder(2)
Dim tempName : tempName = fso.GetTempName()
Dim tempFile : Set tempFile = tempFolder.CreateTextFile(tempName & ".hta")
Dim tempBaseName
tempBaseName = tempFolder & "\" & tempName
tempFile.Write _
"<html>" & _
" <head>" & _
" <title>Browse</title>" & _
" </head>" & _
" <body>" & _
" <input type='file' id='f'>" & _
" <script type='text/javascript'>" & _
" var f = document.getElementById('f');" & _
" f.click();" & _
" var fso = new ActiveXObject('Scripting.FileSystemObject');" & _
" var file = fso.OpenTextFile('" & _
Replace(tempBaseName,"\", "\\") & ".txt" & "', 2, true);" & _
" file.Write(f.value);" & _
" file.Close();" & _
" window.close();" & _
" </script>" & _
" </body>" & _
"</html>"
tempFile.Close
Set tempFile = Nothing
Set tempFolder = Nothing
shell.Run tempBaseName & ".hta", 1, True
Set tempFile = fso.OpenTextFile(tempBaseName & ".txt", 1, false)
If Not tempFile.AtEndOfStream Then
OpenFileDialog = tempFile.ReadLine
Else
OpenFileDialog = ""
End If
tempFile.Close
fso.DeleteFile tempBaseName & ".hta"
fso.DeleteFile tempBaseName & ".txt"
Set tempFile = Nothing
Set fso = Nothing
End Function
ASKER
I am still messing around with the BrowseForFolder trying to get it to work. Hard to believe that something so simple as the CommonDialog is turning out to be so difficult to replace. Perhaps you can answer something for me. Every answer I am getting, the person always brings html in to the equation. I cannot figure out for the life of me why everyone insists on using html.
I can't do function calls in the scripting that I know of, so I would have to figure out how to translate that code into the script and how to replace the html part and get it to work. I also don't need the temp folder as there is a specific folder that I wish to default to.
Thanks for your response. I was beginning to think either I was a pariah or this was too difficult a task.
On another note, I found many references to GetOpenFileName, but cannot find anything to tell me what class to use it with and how to initiate it. I also found Ifilename or similar (where all the variables begin with 'I', but again, could not find how you initiated them (which createobject I would use) or how to reference them.
Thanks again!
I can't do function calls in the scripting that I know of, so I would have to figure out how to translate that code into the script and how to replace the html part and get it to work. I also don't need the temp folder as there is a specific folder that I wish to default to.
Thanks for your response. I was beginning to think either I was a pariah or this was too difficult a task.
On another note, I found many references to GetOpenFileName, but cannot find anything to tell me what class to use it with and how to initiate it. I also found Ifilename or similar (where all the variables begin with 'I', but again, could not find how you initiated them (which createobject I would use) or how to reference them.
Thanks again!
>> Every answer I am getting, the person always brings html in to the equation. I cannot figure out for the life of me why everyone insists on using html.
This appears to be because Microsoft discontinued the ActiveX open file dialog in Vista onwards:
http://blogs.technet.com/b/heyscriptingguy/archive/2009/09/01/hey-scripting-guy-september-1.aspx
There just doesn't appear to be an ActiveX object usable by VBScript or JavaScript for this purpose anymore. HTML, on the other hand, as a different invocation technology, has the ability to use the HTML objects for file dialogs, such as the "file" input type, or the 3050F4E1-98B5-11CF-BB82-00 AA00BDCE0B object class. I have tried using this class directly in VBScript, but it doesn't work either.
The other references you are seeing are available to other lower level programming technologies, or Powershell, since they are part of the .NET framework, which VBScript cannot use.
If you want the above code without the function call, you can use this:
I hope that helps.
Rob.
This appears to be because Microsoft discontinued the ActiveX open file dialog in Vista onwards:
http://blogs.technet.com/b/heyscriptingguy/archive/2009/09/01/hey-scripting-guy-september-1.aspx
There just doesn't appear to be an ActiveX object usable by VBScript or JavaScript for this purpose anymore. HTML, on the other hand, as a different invocation technology, has the ability to use the HTML objects for file dialogs, such as the "file" input type, or the 3050F4E1-98B5-11CF-BB82-00
The other references you are seeing are available to other lower level programming technologies, or Powershell, since they are part of the .NET framework, which VBScript cannot use.
If you want the above code without the function call, you can use this:
' Bypasses c:\fakepath\file.txt problem
' http://pastebin.com/txVgnLBV
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
Dim shell
Set shell = CreateObject("WScript.Shell")
Dim tempFolder : Set tempFolder = fso.GetSpecialFolder(2)
Dim tempName : tempName = fso.GetTempName()
Dim tempFile : Set tempFile = tempFolder.CreateTextFile(tempName & ".hta")
Dim tempBaseName
tempBaseName = tempFolder & "\" & tempName
tempFile.Write _
"<html>" & _
" <head>" & _
" <title>Browse</title>" & _
" </head>" & _
" <body>" & _
" <input type='file' id='f'>" & _
" <script type='text/javascript'>" & _
" var f = document.getElementById('f');" & _
" f.click();" & _
" var fso = new ActiveXObject('Scripting.FileSystemObject');" & _
" var file = fso.OpenTextFile('" & _
Replace(tempBaseName,"\", "\\") & ".txt" & "', 2, true);" & _
" file.Write(f.value);" & _
" file.Close();" & _
" window.close();" & _
" </script>" & _
" </body>" & _
"</html>"
tempFile.Close
Set tempFile = Nothing
Set tempFolder = Nothing
shell.Run tempBaseName & ".hta", 1, True
Set tempFile = fso.OpenTextFile(tempBaseName & ".txt", 1, false)
If Not tempFile.AtEndOfStream Then
strFile = tempFile.ReadLine
Else
strFile = ""
End If
tempFile.Close
fso.DeleteFile tempBaseName & ".hta"
fso.DeleteFile tempBaseName & ".txt"
Set tempFile = Nothing
Set fso = Nothing
If strFile = "" Then
WScript.Echo "You did not select a file."
Else
WScript.Echo "You selected: " & strFile
End If
I hope that helps.
Rob.
ASKER
Thanks for the explanation. I think I am starting to get it. I tried your script as vbs and I like the dialog box it uses better. Would you mind modifying your code above so that it only looks for Word (doc docx), Excel (xls, xlsx) and PDF files.
Oh, hang on, the filter cannot be used with the standard "file" HTML object. I'll rework it with the HTMLDlgHelper class....
OK, try this.
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
Dim shell
Set shell = CreateObject("WScript.Shell")
Dim tempFolder : Set tempFolder = fso.GetSpecialFolder(2)
Dim tempName : tempName = fso.GetTempName()
Dim tempFile : Set tempFile = tempFolder.CreateTextFile(tempName & ".hta")
Dim tempBaseName
tempBaseName = tempFolder & "\" & tempName
tempFile.Write _
"<html>" & vbCrLf & _
" <head>" & vbCrLf & _
" <title>Browse</title>" & vbCrLf & _
" </head>" & vbCrLf & _
" <body>" & vbCrLf & _
" <OBJECT ID=Dlg CLASSID=""CLSID:3050F4E1-98B5-11CF-BB82-00AA00BDCE0B"" width=0 height=0> </OBJECT>" & vbCrLf & _
" <script language=""vbscript"">" & vbCrLf & _
" Sub Window_onload" & vbCrLf & _
" intLeft = window.screenLeft" & vbCrLf & _
" intTop = window.screenTop" & vbCrLf & _
" window.moveTo -2000,-2000" & vbCrLf & _
" window.ResizeTo 1,1" & vbCrLf & _
" Openfile" & vbCrLf & _
" End Sub" & vbCrLf & _
" Sub Openfile" & vbCrLf & _
" Set objFSO = CreateObject(""Scripting.FileSystemObject"")" & vbCrLf & _
" Set objFile = objFSO.CreateTextFile(""" & Replace(tempBaseName,"\", "\\") & ".txt" & """, True)" & vbCrLf & _
" s = Dlg.openfiledlg(, , CStr(""Microsoft Word(*.doc;*.docx)|*.doc;*.docx|Microsoft Excel(*.xls;*.xlsx)|*.xls;*.xlsx|Adobe PDF(*.pdf)|*.pdf|""), CStr(""Choose File""))" & vbCrLf & _
" If (Len(s) = 0) Then" & vbCrLf & _
" 'MsgBox ""No file was selected.""" & vbCrLf & _
" Else" & vbCrLf & _
" i2 = InStr(s, Chr(0))" & vbCrLf & _
" If i2 > 1 Then s = Left(s, (i2 - 1))" & vbCrLf & _
" 'MsgBox ""The selected file is "" & s" & vbCrLf & _
" objFile.Write s" & vbCrLf & _
" End If" & vbCrLf & _
" objFile.Close" & vbCrLf & _
" window.close()" & vbCrLf & _
" End Sub" & vbCrLf & _
" </script>" & vbCrLf & _
" </body>" & vbCrLf & _
"</html>"
tempFile.Close
Set tempFile = Nothing
Set tempFolder = Nothing
shell.Run tempBaseName & ".hta", 1, True
Set tempFile = fso.OpenTextFile(tempBaseName & ".txt", 1, false)
If Not tempFile.AtEndOfStream Then
strFile = tempFile.ReadLine
Else
strFile = ""
End If
tempFile.Close
fso.DeleteFile tempBaseName & ".hta"
fso.DeleteFile tempBaseName & ".txt"
Set tempFile = Nothing
Set fso = Nothing
If strFile = "" Then
WScript.Echo "You did not select a file."
Else
WScript.Echo "You selected: " & strFile
End If
ASKER
damn vaio keyboard, it just erased a long comment.
I am having a problem with "tempBaseName = tempFolder & "\" & tempName". It is saying operands are not same type.
I have gone back and forth declaring the following vars as string and variant.
Dim tempFolder : Set tempFolder = fso.GetSpecialFolder(2)
Dim tempName : tempName = fso.GetTempName()
Dim tempFile : Set tempFile = tempFolder.CreateTextFile( tempName & ".hta")
Dim tempBaseName
tempBaseName = tempFolder & "\" & tempName
If I rem out that line to get past, I then receive error on the tempFile.Write (Unexpected token. Expected -=
I did some research and found the scripting does not support multi-line commands. How can we break that up into individual lines (I don't know what line length I am allowed)
Thanks
I am having a problem with "tempBaseName = tempFolder & "\" & tempName". It is saying operands are not same type.
I have gone back and forth declaring the following vars as string and variant.
Dim tempFolder : Set tempFolder = fso.GetSpecialFolder(2)
Dim tempName : tempName = fso.GetTempName()
Dim tempFile : Set tempFile = tempFolder.CreateTextFile(
Dim tempBaseName
tempBaseName = tempFolder & "\" & tempName
If I rem out that line to get past, I then receive error on the tempFile.Write (Unexpected token. Expected -=
I did some research and found the scripting does not support multi-line commands. How can we break that up into individual lines (I don't know what line length I am allowed)
Thanks
Hmmm, it shouldn't be a line length problem. Maybe it things the strings are of a different type. Try
tempBaseName = CStr(tempFolder) & "\" & CStr(tempName)
or maybe even
tempBaseName = tempFolder + "\" + tempName
or
tempBaseName = CStr(tempFolder) + "\" + CStr(tempName)
It depends on the scripting functions available to your engine.
Rob.
tempBaseName = CStr(tempFolder) & "\" & CStr(tempName)
or maybe even
tempBaseName = tempFolder + "\" + tempName
or
tempBaseName = CStr(tempFolder) + "\" + CStr(tempName)
It depends on the scripting functions available to your engine.
Rob.
ASKER
The length issue is in regards to the tempFile.write and it needing to be on single lines
when using
had already tried the + option, it didn't like the "\" being a string and added to the variants.
Also it does not recognize cStr or CStr as valid functions.
Perhaps we can create a object that only contains "\".
I assume that those variable are do be declared as variants (objects) and not strings.
when using
had already tried the + option, it didn't like the "\" being a string and added to the variants.
Also it does not recognize cStr or CStr as valid functions.
Perhaps we can create a object that only contains "\".
I assume that those variable are do be declared as variants (objects) and not strings.
Oh, I see what you mean. You're saying you can't use the line continuation characters. In that case, try this:
I also changed
Dim tempFolder : Set tempFolder = fso.GetSpecialFolder(2)
to
Dim tempFolder : Set tempFolder = fso.GetSpecialFolder(2).Pa th
to make it a string.
Regards,
Rob.
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
Dim shell
Set shell = CreateObject("WScript.Shell")
Dim tempFolder : tempFolder = fso.GetSpecialFolder(2).Path
Dim tempName : tempName = fso.GetTempName()
Dim tempBaseName
tempBaseName = tempFolder & "\" & tempName
Dim tempFile : Set tempFile = fso.CreateTextFile(tempBaseName & ".hta") tempFile.WriteLine "<html>"
tempFile.WriteLine " <head>"
tempFile.WriteLine " <title>Browse</title>"
tempFile.WriteLine " </head>"
tempFile.WriteLine " <body>"
tempFile.WriteLine " <OBJECT ID=Dlg CLASSID=""CLSID:3050F4E1-98B5-11CF-BB82-00AA00BDCE0B"" width=0 height=0> </OBJECT>"
tempFile.WriteLine " <script language=""vbscript"">"
tempFile.WriteLine " Sub Window_onload"
tempFile.WriteLine " intLeft = window.screenLeft"
tempFile.WriteLine " intTop = window.screenTop"
tempFile.WriteLine " window.moveTo -2000,-2000"
tempFile.WriteLine " window.ResizeTo 1,1"
tempFile.WriteLine " Openfile"
tempFile.WriteLine " End Sub"
tempFile.WriteLine " Sub Openfile"
tempFile.WriteLine " Set objFSO = CreateObject(""Scripting.FileSystemObject"")"
tempFile.WriteLine " Set objFile = objFSO.CreateTextFile(""" & Replace(tempBaseName,"\", "\\") & ".txt" & """, True)"
tempFile.WriteLine " s = Dlg.openfiledlg(, , CStr(""Microsoft Word(*.doc;*.docx)|*.doc;*.docx|Microsoft Excel(*.xls;*.xlsx)|*.xls;*.xlsx|Adobe PDF(*.pdf)|*.pdf|""), CStr(""Choose File""))"
tempFile.WriteLine " If (Len(s) = 0) Then"
tempFile.WriteLine " 'MsgBox ""No file was selected."""
tempFile.WriteLine " Else"
tempFile.WriteLine " i2 = InStr(s, Chr(0))"
tempFile.WriteLine " If i2 > 1 Then s = Left(s, (i2 - 1))"
tempFile.WriteLine " 'MsgBox ""The selected file is "" & s"
tempFile.WriteLine " objFile.Write s"
tempFile.WriteLine " End If"
tempFile.WriteLine " objFile.Close"
tempFile.WriteLine " window.close()"
tempFile.WriteLine " End Sub"
tempFile.WriteLine " </script>"
tempFile.WriteLine " </body>"
tempFile.WriteLine "</html>"
tempFile.Close
Set tempFile = Nothing
Set tempFolder = Nothing
shell.Run tempBaseName & ".hta", 1, True
Set tempFile = fso.OpenTextFile(tempBaseName & ".txt", 1, false)
If Not tempFile.AtEndOfStream Then
strFile = tempFile.ReadLine
Else
strFile = ""
End If
tempFile.Close
fso.DeleteFile tempBaseName & ".hta"
fso.DeleteFile tempBaseName & ".txt"
Set tempFile = Nothing
Set fso = Nothing
If strFile = "" Then
WScript.Echo "You did not select a file."
Else
WScript.Echo "You selected: " & strFile
End If
I also changed
Dim tempFolder : Set tempFolder = fso.GetSpecialFolder(2)
to
Dim tempFolder : Set tempFolder = fso.GetSpecialFolder(2).Pa
to make it a string.
Regards,
Rob.
ASKER
so i should declare these vars as string?
Dim tempFolder : Set tempFolder = fso.GetSpecialFolder(2).Pa th
Dim tempName : tempName = fso.GetTempName()
Dim tempFile : Set tempFile = tempFolder.CreateTextFile( tempName & ".hta")
Dim tempBaseName
Dim tempFolder : Set tempFolder = fso.GetSpecialFolder(2).Pa
Dim tempName : tempName = fso.GetTempName()
Dim tempFile : Set tempFile = tempFolder.CreateTextFile(
Dim tempBaseName
No, you can't. In VBScript, there is no type delcarations. Your scripting engine may support it, but I doubt it. You can try
Dim tempName As String
but I don't think that will work.
What was happening with your "operands are not same type" error, is that this:
Dim tempFolder : Set tempFolder = fso.GetSpecialFolder(2)
actually sets tempFolder to a "folder" type "object", because the GetSpecialFolder method returns an object, from which you can access multiple properties. Then this:
Dim tempName : tempName = fso.GetTempName()
sets tempName to a "string" type variable, because the GetTempName method returns only a string. Therefore, when you try to concatenate those two together by using
tempBaseName = tempFolder & "\" & tempName
we were originally trying to concatenate an "object" with a "string", hence the different "types" in the attemp to combine them.
So, by using
Dim tempFolder : Set tempFolder = fso.GetSpecialFolder(2).Pa th
we are setting tempFolder to the "string" value obtained by the .Path property of the folder "object".
The reason that this (without the .Path) otherwise works in the Windows Script Host engine, is that some objects have a "default property" which VBScript is aware of, so when you reference the object directltly, it spits out the default property, in this case, the "Path" string.
There's an article here on the "default properties" of some objects, if you're interested:
http://blogs.msdn.com/b/ericlippert/archive/2005/08/30/458051.aspx
I hope that helps.
Regards,
Rob.
Dim tempName As String
but I don't think that will work.
What was happening with your "operands are not same type" error, is that this:
Dim tempFolder : Set tempFolder = fso.GetSpecialFolder(2)
actually sets tempFolder to a "folder" type "object", because the GetSpecialFolder method returns an object, from which you can access multiple properties. Then this:
Dim tempName : tempName = fso.GetTempName()
sets tempName to a "string" type variable, because the GetTempName method returns only a string. Therefore, when you try to concatenate those two together by using
tempBaseName = tempFolder & "\" & tempName
we were originally trying to concatenate an "object" with a "string", hence the different "types" in the attemp to combine them.
So, by using
Dim tempFolder : Set tempFolder = fso.GetSpecialFolder(2).Pa
we are setting tempFolder to the "string" value obtained by the .Path property of the folder "object".
The reason that this (without the .Path) otherwise works in the Windows Script Host engine, is that some objects have a "default property" which VBScript is aware of, so when you reference the object directltly, it spits out the default property, in this case, the "Path" string.
There's an article here on the "default properties" of some objects, if you're interested:
http://blogs.msdn.com/b/ericlippert/archive/2005/08/30/458051.aspx
I hope that helps.
Regards,
Rob.
ASKER
When I try it with the .path, it turns it into a string and then I receive an error about an object being expected on this line
Dim tempFile : Set tempFile = tempFolder.CreateTextFile( tempName & ".hta")
Dim tempFile : Set tempFile = tempFolder.CreateTextFile(
ASKER
Is tempName just a randomly generated file name or is there some logic to the name?
ASKER
Ok, I was able to get past that part. But I am still receiving an error of Unexpected token
on the tempFile.WriteLine "<html>"
on the tempFile.WriteLine "<html>"
>> Is tempName just a randomly generated file name or is there some logic to the name?
Yes, it's just a randomly generated filename.
For these "WriteLine" lines:
tempFile.WriteLine "<html>"
Maybe each line requires parenthesis:
tempFile.WriteLine("<html> " )
If that still doesn't work, is the < symbol a special character in your scripting engine?
Rob.
Yes, it's just a randomly generated filename.
For these "WriteLine" lines:
tempFile.WriteLine "<html>"
Maybe each line requires parenthesis:
tempFile.WriteLine("<html>
If that still doesn't work, is the < symbol a special character in your scripting engine?
Rob.
ASKER
parenthesis corrected the error
OK cool. Let me know if there are any other issues.
Rob.
Rob.
ASKER
I am getting an error on this line:
tempFile.WriteLine )" <OBJECT ID=Dlg CLASSID=""CLSID:3050F4E1-9 8B5-11CF-B B82-00AA00 BDCE0B"" width=0 height=0> </OBJECT>")
function call Missing closing ")", Incorrect #parameters?
I also realized that I never tested the last script you gave me. So I put it in a vbs file and run it and I get an error.
tempFile.WriteLine )" <OBJECT ID=Dlg CLASSID=""CLSID:3050F4E1-9
function call Missing closing ")", Incorrect #parameters?
I also realized that I never tested the last script you gave me. So I put it in a vbs file and run it and I get an error.
ASKER
It could be that it does not like the numerous quote marks. Is it possible to several .Write to concatenate the line and then finish with a .Writeline?
ASKER
The error went away went I did the writes, but I do not know if they handles the quote marks that are supposed to be in the command that is written.
ASKER
if I put single quotes in there to replace the internal double quotes, will that affect the script that is written to tempFile?
For this error:
tempFile.WriteLine )" <OBJECT ID=Dlg CLASSID=""CLSID:3050F4E1-9 8B5-11CF-B B82-00AA00 BDCE0B"" width=0 height=0> </OBJECT>")
function call Missing closing ")", Incorrect #parameters?
You have
.WriteLine )" <
but it should be
.WriteLine (" <
You have the wrong bracket.
Other that that, the extra quotes should work....
Rob.
tempFile.WriteLine )" <OBJECT ID=Dlg CLASSID=""CLSID:3050F4E1-9
function call Missing closing ")", Incorrect #parameters?
You have
.WriteLine )" <
but it should be
.WriteLine (" <
You have the wrong bracket.
Other that that, the extra quotes should work....
Rob.
ASKER
I typed it in wrong on here, it is the correct bracket in the script
ASKER
Did you try to run the last vbs script you sent? Also, what is the line
tempFile.WriteLine " <OBJECT ID=Dlg CLASSID=""CLSID:3050F4E1-9 8B5-11CF-B B82-00AA00 BDCE0B"" width=0 height=0> </OBJECT>"
supposed to look like when it is written?
<OBJECT ID=Dlg CLASSID="CLSID:3050F4E1-98 B5-11CF-BB 82-00AA00B DCE0B" width=0 height=0> </OBJECT>) ?
tempFile.WriteLine " <OBJECT ID=Dlg CLASSID=""CLSID:3050F4E1-9
supposed to look like when it is written?
<OBJECT ID=Dlg CLASSID="CLSID:3050F4E1-98
Yes, that's exactly what it should look like in the HTA file that it rights. I have just updated the latest script I posted with a couple of fixes, so try that again. I'll post another version in a few minutes that doesn't have all the double quotes in it, but if you're scripting doesn't support the Chr() function, it won't work anyway....
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
did you make the changes in the tempFile part? I can add the double quote strDQ to the code, I am just curious what changes you made so that I don't have to redo the whole code.
ASKER
Actually I am going to have to test variations to see what will work as the script will only allow two double quotes in the tempFile
Sorry, I had forgot about the brackets on the WriteLine method. I have added those to the latest code above.
Rob.
Rob.
ASKER
it looks like tempFile.Write(strDQ) might work
OK good. In the latest code there I have added strDQ in the places where the double quote is required, so hopefully it works. I'm thinking the DeleteFile calls might need brackets too, so watch out for that.
Rob.
Rob.
ASKER
There a lot of "" in there.
No, there's only two "" in the latest code, which are
strFile = ""
and
If strFile = "" Then
I think those ones should be fine.
Rob.
strFile = ""
and
If strFile = "" Then
I think those ones should be fine.
Rob.
ASKER
there is more that that, but I did miss your change on the object id line
ASKER
I take it back, you are correct.
ASKER
getting an error on the second line below
shell.Run tempBaseName & ".hta", 1, True
Set tempFile = fso.OpenTextFile(tempBaseN ame & ".txt", 1, false)
The txt file was not created. I have attached the hta file that was created.
bloody system will not let me upload it. Added .txt to end.
rad88C8B.tmp.hta.txt
shell.Run tempBaseName & ".hta", 1, True
Set tempFile = fso.OpenTextFile(tempBaseN
The txt file was not created. I have attached the hta file that was created.
bloody system will not let me upload it. Added .txt to end.
rad88C8B.tmp.hta.txt
ASKER
ignore last comment. I D ten T error.
I needed to rename shell to my vars name (oShell)
but now I receive the error : "Unexpected token. expected -:" on the same line
I needed to rename shell to my vars name (oShell)
but now I receive the error : "Unexpected token. expected -:" on the same line
ASKER
how can I run the .hta file outside of the script?
ASKER
I ran it in IE, so it appears to run
ASKER
just not in the script, apologies for the numerous messages.
ASKER
not sure why Modulus_Twelve would email me that I need to attend to this question consider there was considerable activity by me throughout last night and early this morning. but at this point I am stuck so I am not sure what MT wishes for me to do.
ASKER
RobSampson: Thank you very much for your help. You have helped me figure out a lot of issues with this issue, or in a better wording, I have learned much from you.
I have turned in a support ticket with the support department of the company that makes the software that this scripting language is in.
Meanwhile, have you experience with software that has a scripting language similar to vbs but was not? Are you aware of any possible workarounds to get the .hta file to run?
When I read the reference to the shell run command, it refers to calling a program in the call (its example is notepad). Is it possible to call IE to run this .hta yet have IE stay unselfishly in the background and then remove itself from memory when it is finished running the .hta?
Thanks again.
I have turned in a support ticket with the support department of the company that makes the software that this scripting language is in.
Meanwhile, have you experience with software that has a scripting language similar to vbs but was not? Are you aware of any possible workarounds to get the .hta file to run?
When I read the reference to the shell run command, it refers to calling a program in the call (its example is notepad). Is it possible to call IE to run this .hta yet have IE stay unselfishly in the background and then remove itself from memory when it is finished running the .hta?
Thanks again.
Hi, sorry, I was away for the weekend. For the unexpected token error, change this:
oShell.Run tempBaseName & ".hta", 1, True
put brackets around it, but also, to run the HTA property, add mshta.exe to the start of the command, as in:
oShell.Run("mshta.exe " & tempBaseName & ".hta", 1, True)
and that should work.
To be able to see if the HTA runs outside the scripting environment, comment out
fso.DeleteFile tempBaseName & ".hta"
by putting an apostrophe at the start of the line. Then, browse to your %TEMP% folder, and you can run the rad####.tmp.hta file that is in there.
I hope that helps. It sounds like we're close.
Rob.
oShell.Run tempBaseName & ".hta", 1, True
put brackets around it, but also, to run the HTA property, add mshta.exe to the start of the command, as in:
oShell.Run("mshta.exe " & tempBaseName & ".hta", 1, True)
and that should work.
To be able to see if the HTA runs outside the scripting environment, comment out
fso.DeleteFile tempBaseName & ".hta"
by putting an apostrophe at the start of the line. Then, browse to your %TEMP% folder, and you can run the rad####.tmp.hta file that is in there.
I hope that helps. It sounds like we're close.
Rob.
ASKER
system is down for the night. I will try in the morning. Back to the .hta, is there a way to put a default directory for open in there?
Yes, you can change
tempFile.WriteLine(" s = Dlg.openfiledlg(, , CStr(" & strDQ & "Microsoft Word(*.doc;*.docx)|*.doc;* .docx|Micr osoft Excel(*.xls;*.xlsx)|*.xls; *.xlsx|Ado be PDF(*.pdf)|*.pdf|" & strDQ & "), CStr(" & strDQ & "Choose File" & strDQ & "))")
to
tempFile.WriteLine(" s = Dlg.openfiledlg(CStr(" & strDQ & "C:\Temp\*" & strDQ & "), , CStr(" & strDQ & "Microsoft Word(*.doc;*.docx)|*.doc;* .docx|Micr osoft Excel(*.xls;*.xlsx)|*.xls; *.xlsx|Ado be PDF(*.pdf)|*.pdf|" & strDQ & "), CStr(" & strDQ & "Choose File" & strDQ & "))")
where the "C:\Temp\*" is the folder to start at, and the * will be put in the filename box at the bottom. For some reason, this is the only way it works. The initDir parameter doesn't appear to work.
Regards,
Rob.
tempFile.WriteLine(" s = Dlg.openfiledlg(, , CStr(" & strDQ & "Microsoft Word(*.doc;*.docx)|*.doc;*
to
tempFile.WriteLine(" s = Dlg.openfiledlg(CStr(" & strDQ & "C:\Temp\*" & strDQ & "), , CStr(" & strDQ & "Microsoft Word(*.doc;*.docx)|*.doc;*
where the "C:\Temp\*" is the folder to start at, and the * will be put in the filename box at the bottom. For some reason, this is the only way it works. The initDir parameter doesn't appear to work.
Regards,
Rob.
ASKER
adding the mshta.exe did not make a difference.
Are you on a 64 bit system? If so, try using
oShell.Run("C:\Windows\Sys WOW64\msht a.exe " & strDQ & tempBaseName & ".hta" & strDQ, 1, True)
Otherwise, if it's a 32 bit system, try
oShell.Run("C:\Windows\Sys WOW64\msht a.exe " & strDQ & tempBaseName & ".hta" & strDQ, 1, True)
oShell.Run("C:\Windows\Sys
Otherwise, if it's a 32 bit system, try
oShell.Run("C:\Windows\Sys
ASKER
isn't there going to be a problem with the double quotes and strDQ?
I ran the last change and it still did not work. This damn thing just refuses to accept the run command apparently.
I have a tech ticket turned in for it but they have not responded back yet. I think they are stumped also.
I ran the last change and it still did not work. This damn thing just refuses to accept the run command apparently.
I have a tech ticket turned in for it but they have not responded back yet. I think they are stumped also.
ASKER
how about if we try a version of the .hta file that create an environment variable that contains the file name instead of saving it to a file? If a file is not selected, then it saves null to the environment variable. I can grab environment vars.
But the problem is that we can't get the HTA to run....so no matter what you have the HTA do, it still needs to run first.
Can you verify whether the MSHTA.EXE process is running in Task Manager when the script is executed?
Rob.
Can you verify whether the MSHTA.EXE process is running in Task Manager when the script is executed?
Rob.
ASKER
Well, with tech support assistance, it now executes the command, but it have very unacceptable behavior. You see IE open and it asks if I want to run the .hta file. Is there some parameters on the shell.run command that can tell it to run minimized and not to prompt?
ASKER
looks like I can change the 1 to a 7 to achieve the first part. That seemed to work, but it still asks the question about opening the file. Also, it did not return a filename in the tempfile variable.
ASKER
using mshta instead of iexplore seems to work. will run more tests.
OK, I will await your test results....
ASKER
Now receiving an unexpected token error on the fso.DeleteFile tempBaseName & ".txt", yet the command above it fso.DeleteFile tempBaseName & ".hta" executed, though it did not actually delete the file.
Try
fso.DeleteFile(tempBaseNam e & ".hta")
fso.DeleteFile(tempBaseNam e & ".txt")
Rob.
fso.DeleteFile(tempBaseNam
fso.DeleteFile(tempBaseNam
Rob.
ASKER
Looks like the kinks are worked out, now for the finale, I need the .hta script to default to f:\mswams\mergdocs
OK, I think you will have to use
tempFile.WriteLine(" s = Dlg.openfiledlg(CStr(" & strDQ & "F:\mswams\mergdocs\*" & strDQ & "), , CStr(" & strDQ & "Microsoft Word(*.doc;*.docx)|*.doc;* .docx|Micr osoft Excel(*.xls;*.xlsx)|*.xls; *.xlsx|Ado be PDF(*.pdf)|*.pdf|" & strDQ & "), CStr(" & strDQ & "Choose File" & strDQ & "))")
It will put * in the file name box at the bottom, but I couldn't work around that :-(
Rob.
tempFile.WriteLine(" s = Dlg.openfiledlg(CStr(" & strDQ & "F:\mswams\mergdocs\*" & strDQ & "), , CStr(" & strDQ & "Microsoft Word(*.doc;*.docx)|*.doc;*
It will put * in the file name box at the bottom, but I couldn't work around that :-(
Rob.
ASKER
My apologies, I remember you writing that earlier.
ASKER
One of my best experiences with experts exchange. He stayed with me the whole time and was very patient and helpful
Great! I hope it all worked out in the end. I think it may be best if you post the final code that is free of errors, since it was a fairly involved troubleshooting process. Your script engine is obviously a bit more strict that the Windows Script Host, so it would be good to see the final product.
Thanks for the grade and comments, by the way.
Rob.
Thanks for the grade and comments, by the way.
Rob.
ASKER