Hello Guest it is April 20, 2024, 04:11:22 AM

Author Topic: Is it possible to have an "Open File" box with the VB Script?  (Read 5741 times)

0 Members and 2 Guests are viewing this topic.

Offline arbus

*
  •  13 13
    • View Profile
    • car keys
Hi,
If I wanted to open a text file with data in it to load into a wizard. (not a gcode file) Is it possible to have an "Open File" dialog box?

Problem is that I need to select the file to open from a list.  Not just a preset file.  Is there some sort of open file box?

Re: Is it possible to have an "Open File" box with the VB Script?
« Reply #1 on: March 28, 2010, 10:57:27 AM »
Hi

There is no built in FileOpen dialog in this version of VBS, you have to access the CommonDialog DLL in Windows.

The method is well documented, the below does just enough to open a dialog, you will have to research the way to fill the structure properly and read the returned structure when the dialog exits.

It might just be a lot of work for what you are trying to achieve.

Paste the below into a file, saved as a macro in whatever profile you are using, and call it from the MDI line and a FileOpen dialog will appear.
A message will tell you the return value of the dialog.

regards

ArcEye

Code: [Select]
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' save as m*********.m1s and call from MDI line to test


Public Const OFN_LONGNAMES As Long = &H200000
Public Const OFN_PATHMUSTEXIST As Long = &H800
Public Const OFN_CREATEPROMPT As Long = &H2000
Public Const OFN_NODEREFERENCELINKS As Long = &H100000




Public Type OPENFILENAME

  nStructSize       As Long

  hWndOwner         As Long

  hInstance         As Long

  sFilter           As String

  sCustomFilter     As String

  nMaxCustFilter    As Long

  nFilterIndex      As Long

  sFile             As String

  nMaxFile          As Long

  sFileTitle        As String

  nMaxTitle         As Long

  sInitialDir       As String

  sDialogTitle      As String

  flags             As Long

  nFileOffset       As Integer

  nFileExtension    As Integer

  sDefFileExt       As String

  nCustData         As Long

  fnHook            As Long

  sTemplateName     As String

End Type



Public OFN As OPENFILENAME



Public Declare Function GetOpenFileName Lib "comdlg32" Alias "GetOpenFileNameA"(pOpenfilename As OPENFILENAME) As Long




Sub Main()

Dim bSuccess As Boolean

 

   bSuccess = false

   Message ""



   OFN.nStructSize = Len(OFN)

   OFN.hWndOwner = hwnd

   OFN.sFilter = "*"

   OFN.nFilterIndex = 1

   OFN.sFile = "" & Space$(256) & vbNullChar & vbNullChar

   OFN.nMaxFile = Len(OFN.sFile)

   OFN.sDefFileExt = "" & vbNullChar & vbNullChar

   OFN.sFileTitle = vbNullChar & Space$(256) & vbNullChar & vbNullChar

   OFN.nMaxTitle = Len(OFN.sFileTitle)

   OFN.sInitialDir = "" & vbNullChar & vbNullChar

   OFN.sDialogTitle = "" & vbNullChar & vbNullChar

   OFN.flags = OFN_LONGNAMES Or OFN_CREATEPROMPT Or OFN_NODEREFERENCELINKS Or OFN_PATHMUSTEXIST

   

   bSuccess = GetOpenFileName(OFN)

   

  Message "Dialog return was" & Str(bSuccess)
 

   If bSuccess Then



    '  do stuff here

   End If

 

End Sub


 

Offline arbus

*
  •  13 13
    • View Profile
    • car keys
Re: Is it possible to have an "Open File" box with the VB Script?
« Reply #2 on: March 31, 2010, 08:38:07 AM »
Thanks ArcEye for reply.
The code you posted was exactly what i was looking for.
But I have tried for hours to get the return value of the box.  For some reason i keep getting characters returned that are the right length of the file name, but not correct letters?? Just jumbled characters.

In the end I used the Mach VB combo box and DIR and Open commands to get it to do what i needed. 

Thanks Again.


Offline arbus

*
  •  13 13
    • View Profile
    • car keys
Re: Is it possible to have an "Open File" box with the VB Script?
« Reply #3 on: April 01, 2010, 08:11:13 AM »
ok, more issues on the same subject.

Made the open file and save file combo box and all works well on desktop computer.
Moved code to laptop (eeepc) & the DIR command wont work.

for eg.

count=3
Dim x() As String
ReDim x(count)
x(0) = Dir( "c:\Windows\*" )
For i = 1 To count
x(i) = Dir
MsgBox x(i) '3 files from c:\Windows
Next i


This works fine on the desktop, but on laptop it wont return any value.  I have checked that the directory path is correct. Even reloaded Mach3. 

Could there be an issue with eeepc?  everything else works fine on it.

Thanks

Ar
Re: Is it possible to have an "Open File" box with the VB Script?
« Reply #4 on: April 03, 2010, 07:16:13 AM »
Hi

Don't know of any laptop issues that would prevent directory access functions working.

I don't have Mach installed on a laptop so can't test that.

The code you posted works fine on my Vista desktop simulation set up and the MSDN VB documentation still shows that method for iterating through a directory listing

regards

ArcEye

Offline ger21

*
  • *
  •  6,295 6,295
    • View Profile
    • The CNC Woodworker
Re: Is it possible to have an "Open File" box with the VB Script?
« Reply #5 on: April 03, 2010, 08:15:26 AM »
I use this in an AutoCAD VBA macro. perhaps it'll help?

'Open File and get path - path = strTXTname

Dim strTXTtitle As String, strTXTname As String, strTXTpath As String

'set the default pointcloud filename
strTXTtitle = "c:\temp\~temp.txt"

Dim oComDlg As New CommonDialog
Dim retVal As Long

Set oComDlg = New CommonDialog
oComDlg.DefaultExt = "txt"
oComDlg.DialogTitle = "Open Point Cloud File"
oComDlg.Filter = "TXT files (*.txt)|*.txt|All files (*.*)|*.*"
oComDlg.Flags = OFN_OVERWRITEPROMPT
oComDlg.InitDir = strTXTpath

'file name (excluding path) and at least 2 NullChar's must fit in .MaxFileSize
'otherwise just use the null filename that oComDlg initialized with

If oComDlg.MaxFileSize - 1 > Len(strTXTtitle) Then
oComDlg.FileName = strTXTtitle & String(oComDlg.MaxFileSize - Len(strTXTtitle), vbNullChar)
End If

retVal = oComDlg.ShowOpen
If retVal <= 0 Then 'user cancelled or the API call failed, so exit sub
Exit Sub
End If
strTXTname = oComDlg.FileName
strTXTpath = oComDlg.Path

Set oComDlg = Nothing 'throw away common dialog
Gerry

2010 Screenset
http://www.thecncwoodworker.com/2010.html

JointCAM Dovetail and Box Joint software
http://www.g-forcecnc.com/jointcam.html
Re: Is it possible to have an "Open File" box with the VB Script?
« Reply #6 on: April 03, 2010, 09:37:26 AM »
This one works for me.

Set ObjFSO = CreateObject("UserAccounts.CommonDialog")

ObjFSO.Filter = "G-codes|*.nc|Text Documents|*.txt|All Files|*.*"

ObjFSO.FilterIndex = 3

ObjFSO.InitialDir = "c:\mach3"

InitFSO = ObjFSO.ShowOpen

If InitFSO = False Then
    MsgBox "Script Error: Please select a file!"
Else
    MsgBox "You selected the file: " & ObjFSO.FileName
End If