How do I open a File Dialog box in IDEAScript? - ideascript

I would like to have the user select a file to import into my IDEA project as a part of an IDEAScript macro. How can I create the File Dialog box and get the file the user selects?

The following code will open a File Dialog window and return the file path for the file the user chooses.
Function GetFileName()
Dim ShellApp, Ret, s, i
Set ShellApp = CreateObject("Shell.Application")
Set Ret = ShellApp.BrowseForFolder(0, "Choose a file.", 16384)
MsgBox Ret.ParentFolder.ParseName(Ret.Title).Path
End Function

Related

Looping through a .zip file to open an Excel file, can't get file extension?

I'm trying to loop through a directory with .zip files and pull out the file which is an Excel type file having specific string in the name. I can see the files within .zip having .xlsx extension, but why I try to get this extension name (file_ext) using GetExtensionName, it's empty. Here is a slice of my code:
'Create instance of the shell application
Set obj_app = CreateObject("Shell.Application")
'Get the .zip file
Set obj_file = obj_app.Namespace((str_zip_file)).Items
'Loop through each file within the .zip file
For Each obj_stream In obj_file
str_file = obj_stream.Name
If obj_stream.Type Like "Microsoft Excel Worksheet" And InStr(str_file, "ESG") Then
Set obj_target = obj_app.Namespace(obj_subfolder.Path)
'If it's an Excel file, set the file name to `str_excel_file` and exit the loop
str_excel_file = str_file
obj_target.CopyHere obj_stream
file_ext = LCase(obj_fso.GetExtensionName(obj_subfolder.Path & "\" & str_file))
Exit For
End If
Next obj_stream
Also, when pulled out of .zip into the directory when actual .zip is placed (done by obj_target.CopyHere obj_stream) it appears as being without '.xlsx' extension. What am I doing wrong?

Return to SaveAs Dialog Box powerbuilder

I want to export a datawindow informations to a .txt file. When I click the export button the SaveAs Dialog Box opens. If a .txt document with the same name exists then a MessageBox pop-up questioning if I want to replace the file. If I press NO then SaveAs Dialog Box is closing. How can I prevent SaveAs Dialog Box from closing? I want to return in it and give another name to file.
Here is my code:
li_rc = GetFileSaveName (lcs_title, ls_path_filename, ls_filename, 'txt', 'TXT File (*.txt), *.txt' )
if li_rc > 0 then
if FileExists (ls_path_filename) then
//Existing file has been found
if MessageBox (lcs_title, 'Replace the file '+ ls_path_filename +'?', Question!, YesNo!, 1) = 2 then
//Do not replace the file
//Return to dialog?
else
//Replace the file
dw_1.SaveAs(ls_path_filename, Text!, false)
end if
end if
end if
Thanks!
Try this
boolean lb_need_file = true
do while lb_need_file
li_rc = GetFileSaveName (lcs_title, ls_path_filename, ls_filename, 'txt', 'TXT File (*.txt), *.txt' )
if li_rc > 0 then
if FileExists (ls_path_filename) then
//Existing file has been found
if MessageBox (lcs_title, 'Replace the file '+ ls_path_filename +'?', Question!, YesNo!, 1) = 2 then
//Do not replace the file
//Return to dialog?
else
lb_need_file = false
//Replace the file
dw_1.SaveAs(ls_path_filename, Text!, false)
end if
end if
end if
loop
Please let us know if this works for you

Looping through file , temp file interfering

My script would require accessing all excel file in some other folder.
Excel create a temporary file which the script would try to access and return an error.
I dont even know what to look for .
Set System_exp = CreateObject("Scripting.FileSystemObject")
Set folder = System_exp.Getfolder("C:\Sumthing\data") 'Working directory'
Set file_list = folder.Files
For Each file In file_list
Set Workbook = Workbooks.Open(file)
[Some loop/function here]
Next
End Sub
Was't expecting the temporary file .
Run-time error '1004' : Excel cannot open the file '~$Data 1.xlsx' because the file format or file extension not valid. Verify that the file has not been corrupted and that the file extension matches the format of the file.
Apparently it some sort of backup file in case excel crash . What can I do?
You can test the filename:
For Each file In file_list
If Not file.Name Like "~*" Then
Set Workbook = Workbooks.Open(file)
[Some loop/function here]
End If
Next

Opening a dynamically created Excel workbook

I have a Windows Forms app with an Excel file I created using SpreadsheetGear. My application uses SaveFileDialog() to prompt the user to save the file to their computer using the following code:
'Bring up the save dialog to save the newly created Excel file
Using saveFileDialog1 As New SaveFileDialog()
saveFileDialog1.FileName = "ExportFile.xlsx"
If DialogResult.OK <> saveFileDialog1.ShowDialog() Then
Return
End If
Try
e.Result.SaveAs(saveFileDialog1.FileName, FileFormat.OpenXMLWorkbook)
Catch ex As Exception
MessageBox.Show("Error: " & ex.Message, "Save Error", MessageBoxButtons.OK)
End Try
End Using
That code works fine but the user doesn't want to worry about saving the file. He just wants it to open right away so he can copy/paste the information he needs and then discard the file without having to navigate to his save destination. I can't seem to get SaveFileDialog() to do that for me.
Is there a better way to do this?
This closed question helped me discover my answer. Process.Start with the filename from the SaveFileDialog works.
Updated Code:
'Bring up the save dialog to save the newly created Excel file
Using saveFileDialog1 As New SaveFileDialog()
saveFileDialog1.FileName = "ExportFile.xlsx"
If DialogResult.OK <> saveFileDialog1.ShowDialog() Then
Return
End If
Try
e.Result.SaveAs(saveFileDialog1.FileName, FileFormat.OpenXMLWorkbook)
If File.Exists(saveFileDialog1.FileName) Then
Process.Start(saveFileDialog1.FileName) 'Open the newly created Excel file
Else
Throw New Exception("Could not find file to open. Please try again.")
End If
Catch ex As Exception
MessageBox.Show("Error: " & ex.Message, "Save Error", MessageBoxButtons.OK)
End Try
End Using

How do I make an Excel VBA Function that works when I reopen the document?

OK, I've just written a very simple VBA script that goes off and grabs the file size of a file found at a specified location (edit update code from dscarr):
Public Function FileSize(path As String) As Variant
On Error GoTo Err_FileSize:
Dim retVal As Variant
Dim filesys As Object
Dim file As Object
retVal = ""
Set filesys = CreateObject("Scripting.FileSystemObject")
Set file = filesys.GetFile(path)
retVal = file.Size
Exit_FileSize: On Error Resume Next
FileSize = retVal
Exit Function
Err_FileSize: retVal = "Error: " & Err.Description
Resume Exit_FileSize
End Function
If I import this into a new workbook, save the document as a "Excel Macro-Enabled Workbook" alongside an empty file called readme.txt and then put "./readme.txt" in A1 and "=FileSize(A1)" in A2, A2 evaluates properly to the file size of readme.txt, 0 bytes. Great. If I then save the document, close and reopen it, I'm warned that macros are disabled. If I enable them, the content of A2 has changed to #VALUE! and nothing I do will make the function work again. Has anyone seen this before, can anyone point out the mistake that I'm making here?
edit: the issue seems to be caused by me using relative paths. I can't explain why it works for a new workbook, but not once saved and reopened, but using absolute paths solves the problem, which as dscarr identified, was a "File Not Found" issue.
I duplicated your code in an Excel 2007 Macro Enabled workbook and found that it worked just fine with the following caveats
It does not automatically update the value (e.g. run the FileSize function) when you open the workbook. This can be compensated for by adding some code to the Worrkbook_Open event handler that calculates the workbook.
I get the #VALUE error when it cannot find the file that is specified. Indeed, when the file is missing or incorrectly named the line "Set file = filesys.GetFile(path)" throws the error number 53 "File Not Found". In this case the return value is never set because the line of code that sets it is never called. You could try setting a default value of "File Not Found" before actually attempting to retrieve the file size.
In Excel 2010, Go File->Options->Trust Center->Macro Settings and make sure the Enable option button has been selected.

Resources