Jump to content

Welcome to eMastercam

Register now to participate in the forums, access the download area, buy Mastercam training materials, post processors and more. This message will be removed once you have signed in.

Use your display name or email address to sign in:

Mcam VBScript question


Recommended Posts

I'm using the multi-post script off the ftp site. I've noticed that the AskString prompt leaves a space in the prompt window that you have to backspace to delete. If you don't then it can't find the path. Is there any way to allow the user to chose a destination folder without having to type a name in? It would speed up the process and reduce errors. I've gone through Mcam VBScript help, but either didn't find it or didn't know how to make it work.

Thanks

Link to comment
Share on other sites

quote:

I've noticed that the AskString prompt leaves a space in the prompt window that you have to backspace to delete. If you don't then it can't find the path.


I've noticed that too. You can get around that by using VBScript's Trim() function to get rid of any leading or trailing whitespace:

 

code:

Dim strName

 

strName = Trim(AskString("What's your name?"))

Even if you're not prompting for a directory, Trim() is still useful in those cases where the user accidentally hit the spacebar or whatever.

 

quote:

Is there any way to allow the user to chose a destination folder without having to type a name in?


You can use the folder browser dialog of the Application class in the WSH. Here's a function that you can use in your scripts:

 

code:

' Purpose: Prompt the user to select a directory and retrieve the selected path

' I: prompt to give the user on the directory selection dialog

' I: directory to start looking in

' O: string containing the directory selected (vbNullString if there's an error)

Function FolderBrowser(strPrompt, strStartFolder)

Const DEF_WINDOW_HANDLE = 0

Const DEF_NO_OPTIONS = 0

Dim objShell ' Shell Application object

Dim objFolder ' Folder object

Dim objFolderItem ' FolderItem object

 

 

On Error Resume Next

 

' Create Shell Application, Folder and FolderItem objects

Set objShell = CreateObject("Shell.Application")

Set objFolder = objShell.BrowseForFolder(DEF_WINDOW_HANDLE, strPrompt, DEF_NO_OPTIONS, strStartFolder)

Set objFolderItem = objFolder.Self

 

' Return a null string if there was an error

If Err.Number <> 0 Then

FolderBrowser = vbNullString

Err.Clear

Exit Function

End If

 

' Return the selected directory

FolderBrowser = objFolderItem.Path

 

' Cleanup

Set objShell = Nothing

Set objFolder = Nothing

Set objFolderItem = Nothing

End Function

You can use it like this:

 

code:

Dim strDir   ' path to the selected directory

 

strDir = FolderBrowser("Choose your cool directory.", "")

 

If (strDir = vbNullString) Then

' The user bailed on selecting a directory.

' Handle this any way you want.

End If

Or you can automatically start in a given folder...say the Program Files directory:

 

code:

Dim strDir   ' path to the selected directory

 

strDir = FolderBrowser("Choose your cool directory.", "C:Program Files")

 

If (strDir = vbNullString) Then

' The user bailed on selecting a directory.

' Handle this any way you want.

End If

HTH

Link to comment
Share on other sites

Yes, it works great. I just had 2 folders with similar names, one was empty and the other had all of our programs. I went to the wrong folder. I guess I panicked a little when I thought I'd deleted everything. I had a traumatic experience with that many years ago. bonk.gif

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.

Join us!

eMastercam - your online source for all things Mastercam.

Together, we are the strongest Mastercam community on the web with over 56,000 members, and our online store offers a wide selection of training materials for all applications and skill levels.

Follow us

×
×
  • Create New...