0

I am writing an HTA file with VBScript as the scripting language. I have a function where the user is prompted to choose the folder in which they would like to save a document. I didn't write this code myself, but instead borrowed it from here.

Function SelectFolder(myStartFolder)
Dim objFolder, objItem, objShell

Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.BrowseForFolder(0, "Select Folder to Save New File", 0, myStartFolder)

If IsObject(objFolder) Then SelectFolder = objFolder.Self.Path
End Function

I call this function in another one in order when I make the file and prompt the user to choose where to save it:

Sub Example()
Dim destPath, destFile, objWorkbook
destPath = SelectFolder(libPath)
destPath = destPath & "\Sample Export"

Set destFile = CreateObject("Excel.Application")
Set objWorkbook = destFile.Workbooks.Add()
objWorkbook.SaveAs(destPath)

(code to edit excel file)

End Sub

Example() works fine except when someone chooses to save their document in one of the four default libraries in Windows (Documents, Music, Pictures, Videos). In that case, I receive an error saying "The file could not be accessed" and indicating the error is at the line that says "objWorkbook.SaveAs(destPath)". The error box then gives me the following suggestions:

  • Make sure the specified folder exists
  • Make sure the folder that contains the file is not read-only
  • Make sure the file name does not contain any of the following characters: < > ? { } : | or *
  • Make sure the file/path name doesn't contain more than 218 characters.

The error occurs when I open the HTA file and click a button calling Example(), which then opens a dialog box to ask the user to choose the file location. When Documents, Music, Pictures, or Videos is chosen, a "Script Error" box pops up with the error message listed above. I am not familiar enough with VBScript to know what the problem is exactly. I would appreciate any suggestions as to what to do.

4
  • Please read carefully ==> How do I ask a good question? ==> How to create a Minimal, Complete, and Verifiable example ==> What types of questions should I avoid asking? ===> What topics can I ask about here? Commented Jun 27, 2017 at 17:28
  • ^... + files and folders in the question are messed up. Please elaborate your question, and add the relevant code to the question too. Commented Jun 28, 2017 at 4:00
  • You haven't shown your code. Is the Dialog returning an error? If so, what error? or Is the error in the next line, after the dialog has returned a reference to a system virtual folder which only works with shell commands, not file commands? Commented Jun 28, 2017 at 6:05
  • I have added code and elaborated on the error. Apologies for the lack of information in the initial question. Commented Jun 29, 2017 at 15:41

1 Answer 1

0

I don't know exactly what the solution to the above issue was. However, I noticed that the path I pass to SelectFolder had a typo in it. libPath was supposed to be to the %userprofile% from the environment variable. However, I had pluralized "userprofiles" so that it was %userprofiles% instead. This took me to somewhere that looked right but wasn't. I fixed the typo and now BrowseForFolder takes me to C:/Users/*username* like it's supposed to. It turns out that I was being sent to the Desktop location because of the fourth argument in BrowseForFolder, instead of the user profile like I wanted.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.