OpenOffice.org Forum at OOoForum.orgThe OpenOffice.org Forum
 
 [Home]   [FAQ]   [Search]   [Memberlist]   [Usergroups]   [Register
 [Profile]   [Log in to check your private messages]   [Log in

How to cancel the macro with file open dialog window?

 
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API
View previous topic :: View next topic  
Author Message
PsychoticBoredom
Power User
Power User


Joined: 04 Nov 2005
Posts: 68
Location: Houston, Texas

PostPosted: Tue Nov 29, 2005 8:51 am    Post subject: How to cancel the macro with file open dialog window? Reply with quote

I wrote a macro to open the file dialog window and allow the user to select a "csv" file to import into the current spreadsheet. If the user pushes the "open" or "help" button, everything works fine. The problem is when the user hits the "cancel" button, the macro doesn't stop running. What code should I add to detect when the user hits the "cancel" button in the file dialog window and then exit or stop the macro? The current code is as follows:

REM*************************************************************************************
REM*************************************************************************************
sub import()

dim bias_inputs as object, FilePath as string
dim oFileDialog as object, oFilestList as object

oFileDialog = CreateUnoService("com.sun.star.ui.dialogs.FilePicker")
oFileDialog.setMultiSelectionMode(False)
oFileDialog.setDisplayDirectory("file:///put_your_own_path_here/")
oFileDialog.setDefaultName("import_file_name.csv")
oFileDialog.execute()
oFilesList=oFileDialog().getFiles()

MsgBox("The file you picked was " + oFilesList(0))
FilePath = oFilesList(0)

dim fileProps(1) as new com.sun.star.beans.PropertyValue
' sets the imported file properties
fileProps(0).Name = "FilterName"
fileProps(0).Value = "Text - txt - csv (StarCalc)"
fileProps(1).Name = "FilterFlags"
fileProps(1).Value =
"9,34,0,1,1/1/2/1/3/1/4/1/5/1/6/1/7/1/8/1/9/1/10/4/11/1"

bias_inputs = StarDesktop.loadComponentFromURL(FilePath, "_blank", 0,
fileProps())
dim oDesktop as Object
oDesktop = createUnoService( "com.sun.star.frame.Desktop")

dim document as object
document = ThisComponent
'--------------------------------------------------------------------------
' Open file, send data to temporary location, and close the file
'--------------------------------------------------------------------------
' set focus to the newly opened file
bias_inputs.CurrentController.Frame.ContainerWindow.toFront()

' copy the data from the inputs file
dim oFromSheet as object, oFromRange as object
oFromSheet = bias_inputs.Sheets(0)
oFromRange = oFromSheet.getCellRangeByName("A1:A57")
oAllData = oFromRange.getDataArray()

' paste the data in temporary location
dim oToSheet as object, oToRange as object
oToSheet = ThisComponent.Sheets(0)
oToRange = oToSheet.getCellRangeByName("B115:B171")
oToRange.setDataArray(oAllData())

' close the bias inputs file
bias_inputs.dispose

'--------------------------------------------------------------------------
' Copy imported data to proper locations
'--------------------------------------------------------------------------

'Droop time (sec)
oToSheet.getCellRangeByName("D104").setDataArray(oToSheet.getCellRangeByName("B168").getDataArray)
'Droop VI
oToSheet.getCellRangeByName("D105").setDataArray(oToSheet.getCellRangeByName("B169").getDataArray)

'--------------------------------------------------------------------------
' Delete data from temporary location
'--------------------------------------------------------------------------
rem define variables
dim dispatcher as object

rem get access to the document
document = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")


dim args1(0) as new com.sun.star.beans.PropertyValue
args1(0).Name = "ToPoint"
args1(0).Value = "$B$115:$B$171"
dispatcher.executeDispatch(document, ".uno:GoToCell", "", 0, args1())


dim args2(0) as new com.sun.star.beans.PropertyValue
args2(0).Name = "Flags"
args2(0).Value = "A"
dispatcher.executeDispatch(document, ".uno:Delete", "", 0, args2())

'--------------------------------------------------------------------------
' Refresh the screen using the new data
'--------------------------------------------------------------------------
ThisComponent.UnlockControllers ' these two lines
ThisComponent.removeActionLock ' turn screen updating on

'--------------------------------------------------------------------------
' Place cursor at cell D1
'--------------------------------------------------------------------------

dim args3(0) as new com.sun.star.beans.PropertyValue
args3(0).Name = "ToPoint"
args3(0).Value = "$D$1"

dispatcher.executeDispatch(document, ".uno:GoToCell", "", 0, args3())

end sub


REM*************************************************************************************
REM*************************************************************************************
_________________
"Think like a genius. Act like a fool."
Back to top
View user's profile Send private message
ms777
Super User
Super User


Joined: 07 Feb 2004
Posts: 1355

PostPosted: Tue Nov 29, 2005 11:23 am    Post subject: Reply with quote

see e.g. http://www.oooforum.org/forum/viewtopic.phtml?p=71125#71125

Code:
o=CreateUnoService("com.sun.star.ui.dialogs.FilePicker")
o.setTitle(sTitle)
o.appendFilter(  sFilterName & " (" & sFilter & ")", sFilter)
o.setMultiSelectionMode(false)
o.setDisplayDirectory(ConvertToUrl(sStartDirectory))
 
if o.execute() = com.sun.star.ui.dialogs.ExecutableDialogResults.OK then
  ars = o.getFiles()
  BrowseForFile = ars(UBound(ars))
  else
  BrowseForFile = ""
  endif
Back to top
View user's profile Send private message
PsychoticBoredom
Power User
Power User


Joined: 04 Nov 2005
Posts: 68
Location: Houston, Texas

PostPosted: Wed Nov 30, 2005 8:49 am    Post subject: Reply with quote

Thank you for the info. I used your example to add the following lines to my code make it work:

if oFileDialog.execute() = com.sun.star.ui.dialogs.ExecutableDialogResults.OK then
oFilesList = oFilesDialog.getFiles()
' perform all operations of the macro here
'
'
else
exit sub 'exit the macro if the user hits cancel
endif
_________________
"Think like a genius. Act like a fool."
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API All times are GMT - 8 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group