| View previous topic :: View next topic |
| Author |
Message |
lesguilw Power User

Joined: 09 Nov 2005 Posts: 57 Location: Frenchy in Sevilla, Spain
|
Posted: Thu Nov 17, 2005 2:48 am Post subject: Adapted Save As dialog: FilePicker or DispatchHelper? |
|
|
Hi folks,
Once again I put my fate in the hands of the almighty OOoForum community . I did quite a lot of homework on this case so please don't let me down!
I created a macro that launches a Save As dialog with a few modifications. For the moment I achieved, using a FilePicker, to set the directory I want, a default name and filters. What I want to do now is to access the controls of the FilePocker dialog to modificate them, for example setting the CHECKBOX_AUTOEXTENSION ticked as default.
In this thread: http://www.oooforum.org/forum/viewtopic.phtml?t=8098&highlight=filepicker Gonzalo gives a way of accessing the controls but I tried it without success. After searching the API it seems that the "setControlProperty" method no longer exists in the XFilePicker interface or object ( http://api.openoffice.org/docs/common/ref/com/sun/star/ui/dialogs/FilePicker.html ). I neither could find the controlIds he uses. So instead I tried using the "setValue" method of the XFilePickerControlAccess ( http://api.openoffice.org/docs/common/ref/com/sun/star/ui/dialogs/XFilePickerControlAccess.html ) that exists but the second argument "ControlAction" ( http://api.openoffice.org/docs/common/ref/com/sun/star/ui/dialogs/XFilePickerControlAccess.html#setValue ) is rather strange and I don't know to which value set it, all I tried failed.
I continued my search on the forum and found that post (among many many others): http://www.oooforum.org/forum/viewtopic.phtml?t=8020&highlight=filepicker where DannyB suggests to use the DispatchHelper instead of the FilePicker and it's true that it is better since it seems to show the Save As dialog box of the File Menu, with the checkbox ticked and all the filters. But here I can't anymore set the file name to the value I wish...
Here is the code I use for the moment: with the FilePicker:
| Code: |
Sub SaveAs()
Dim oDoc As Object
oDoc = ThisComponent
'MimeType = oDoc.DocumentInfo.MIMEType
'MsgBox MimeType
' Set the Dialog Arguments to a Template for a FILESAVE Dialog Box
sFilePickerArgs = Array(com.sun.star.ui.dialogs.TemplateDescription.FILESAVE_AUTOEXTENSION_PASSWORD )
' register the Service for Filepicker
oFilePicker = CreateUnoService( "com.sun.star.ui.dialogs.FilePicker" )
With oFilePicker
sDirectory = oFilePicker.getDisplayDirectory
.Initialize ( sFilePickerArgs() )
.SetDisplayDirectory (sDirectory)
.SetDefaultName ("a_name_that_will_be_set_according_to_info_given_by_another_dialog")
.AppendFilter( "All files (*.*)", "*.*" )
.AppendFilter( "OpenDocument Text (.odt)", "*.odt" )
.AppendFilter( "OpenDocument Text Template (.ott)", "*.ott" )
.SetCurrentFilter( "All files (*.*)" )
.SetValue(CHECKBOX_AUTOEXTENSION, ???What should I put there???, true)
'.setControlProperty ("AutoExtensionBox", "Visible", false) <-- This doesn't work
End With
'Open the dialog box the Dialog
If oFilePicker.execute() Then
sFiles = oFilePicker.getFiles()
sFileURL = sFiles(0)
oDoc.storeToURL( sFileURL, Array() )
End If
End Sub
|
The code with the DispatchHelper is DannyB's one.
So can anyone tell me how to access the controls of the FilePicker or how to specify a file name to the DispatchHelper dialog?
Many thanks,
Will |
|
| Back to top |
|
 |
SergeM Super User

Joined: 09 Sep 2003 Posts: 3211 Location: Troyes France
|
|
| Back to top |
|
 |
lesguilw Power User

Joined: 09 Nov 2005 Posts: 57 Location: Frenchy in Sevilla, Spain
|
Posted: Thu Nov 17, 2005 5:35 am Post subject: |
|
|
| Thanks for the link and your cool thread but unfortunately your Save As example stops where my problems begin... |
|
| Back to top |
|
 |
ms777 Super User


Joined: 07 Feb 2004 Posts: 1355
|
|
| Back to top |
|
 |
lesguilw Power User

Joined: 09 Nov 2005 Posts: 57 Location: Frenchy in Sevilla, Spain
|
Posted: Fri Nov 18, 2005 6:08 am Post subject: |
|
|
Thanks for the thread ms777, this code was way out of my league and I had a hard time understanding it. But I learned a lot searching what it was doing, thanks!
I have a few problem though:
1 - The listener you use ("com.sun.star.awt.XTopWindowListener") seems not to work with the SaveAs dialog... when it opens, the code in the TopWFormula_windowOpened doesn't execute. So I looked for another one but can't find any list of all the listeners, I did find a FilePickerListener but the method "open" is not included in it. Do you know which listener I could use?
2 - How can I retrieve the names of the controls I am looking for? The rols are obvious (I just have to open the SaveAs window and see what they are) but the names... do I have to guess the name or there is a way to retrieve them?
3 - This is a more general problem: I tried to test things using the FilePickerListener but I couldn't initialize it. I should use the FilePickerNotifier interface that have the add and remove method for the listener. My problem is that the API ( http://api.openoffice.org/docs/common/ref/com/sun/star/ui/dialogs/XFilePickerNotifier.html ) doesn't specify any object that have this interface. How can I know which object to create to be able to use the interface?
Thanks,
Will |
|
| Back to top |
|
 |
SergeM Super User

Joined: 09 Sep 2003 Posts: 3211 Location: Troyes France
|
|
| Back to top |
|
 |
ms777 Super User


Joined: 07 Feb 2004 Posts: 1355
|
Posted: Sat Nov 19, 2005 10:14 am Post subject: |
|
|
Hi,
it seems not to be necessary to use the TopWindowListener. Try the below code. Note that on my System (Win98SE) the behavior varies, depending on your config settings (Tools/Options/General/OpenSave/Use OO Dialogs checked or not). With system dialogs, the listener leads to a complete OO block, even if all msgbox are commented out. With OO dialogs, everything works fine.
So start with the listener add / remove commented out, and then see what happens ...
Good luck,
ms777
| Code: |
sub Test
oDoc = ThisComponent
oList = createUnoListener("FP_", "com.sun.star.ui.dialogs.XFilePickerListener")
' Set the Dialog Arguments to a Template for a FILESAVE Dialog Box
sFilePickerArgs = Array(com.sun.star.ui.dialogs.TemplateDescription.FILESAVE_AUTOEXTENSION_PASSWORD )
' register the Service for Filepicker
oFilePicker = CreateUnoService( "com.sun.star.ui.dialogs.FilePicker" )
With oFilePicker
sDirectory = oFilePicker.getDisplayDirectory
.Initialize ( sFilePickerArgs() )
.SetDisplayDirectory (sDirectory)
.SetDefaultName ("a_name_that_will_be_set_according_to_info_given_by_another_dialog")
.AppendFilter( "All files (*.*)", "*.*" )
.AppendFilter( "OpenDocument Text (.odt)", "*.odt" )
.AppendFilter( "OpenDocument Text Template (.ott)", "*.ott" )
.SetCurrentFilter( "All files (*.*)" )
.setLabel(com.sun.star.ui.dialogs.CommonFilePickerElementIds.EDIT_FILEURL_LABEL,"Type here, dummy !")
.setValue(com.sun.star.ui.dialogs.ExtendedFilePickerElementIds.CHECKBOX_AUTOEXTENSION, 0, false)
.setValue(com.sun.star.ui.dialogs.ExtendedFilePickerElementIds.CHECKBOX_PASSWORD, 0, true)
.EnableControl(com.sun.star.ui.dialogs.ExtendedFilePickerElementIds.CHECKBOX_PASSWORD, false)
End With
' oFilePicker.addFilePickerListener(oList)
'Open the dialog box the Dialog
If oFilePicker.execute() Then
sFiles = oFilePicker.getFiles()
sFileURL = sFiles(0)
End If
' oFilePicker.removeFilePickerListener(oList)
call msgbox(sFileUrl)
end sub
sub FP_fileSelectionChanged( aEvent as Any)
'call msgbox("fileSelectionChanged")
end sub
sub FP_directoryChanged( aEvent as Any)
'call msgbox("directoryChanged")
end sub
function FP_helpRequested( aEvent as Any) as String
'call msgbox("helpRequested")
end function
sub FP_controlStateChanged( aEvent as Any)
'call msgbox("controlStateChanged")
end sub
sub FP_dialogSizeChanged( aEvent as Any)
'call msgbox("dialogSizeChanged")
end sub
|
|
|
| Back to top |
|
 |
lesguilw Power User

Joined: 09 Nov 2005 Posts: 57 Location: Frenchy in Sevilla, Spain
|
Posted: Mon Nov 21, 2005 2:10 am Post subject: |
|
|
Thanks ms777 this works perfectly!
Let's make a little summary of all this and provide a clean code:
If you want to make an adapted / modified / changed SaveAs dialog you can use the following code:
| Code: |
Sub SaveAs()
Dim oDoc
oDoc = ThisComponent
'Set the Dialog Arguments to a Template for a FILESAVE Dialog Box
sFilePickerArgs = Array(com.sun.star.ui.dialogs.TemplateDescription.FILESAVE_AUTOEXTENSION_PASSWORD )
'Register the Service for Filepicker
oFilePicker = CreateUnoService( "com.sun.star.ui.dialogs.FilePicker" )
'Configure the FilePicker
With oFilePicker
sDirectory = oFilePicker.getDisplayDirectory
.Initialize ( sFilePickerArgs() )
.SetDisplayDirectory (sDirectory)
.SetDefaultName ("a_name_that_will_be_set_according_to_info_given_by_another_dialog")
.AppendFilter( "All files (*.*)", "*.*" )
.AppendFilter( "OpenDocument Text (.odt)", "*.odt" )
.AppendFilter( "OpenDocument Text Template (.ott)", "*.ott" )
.SetCurrentFilter( "OpenDocument Text (.odt)" )
.SetValue(com.sun.star.ui.dialogs.ExtendedFilePickerElementIds.CHECKBOX_AUTOEXTENSION, 0, true)
.SetValue(com.sun.star.ui.dialogs.ExtendedFilePickerElementIds.CHECKBOX_PASSWORD, 0, false)
'.EnableControl(com.sun.star.ui.dialogs.ExtendedFilePickerElementIds.CHECKBOX_PASSWORD, false)
End With
'Open the dialog box and saves file
If oFilePicker.execute() Then
sFiles = oFilePicker.getFiles()
sFileURL = sFiles(0)
oDoc.storeToURL( sFileURL, Array() )
End If
End Sub
|
For information on the FilePicker see: http://api.openoffice.org/docs/common/ref/com/sun/star/ui/dialogs/module-ix.html
Particularly, the interface com.sun.star.ui.dialogs.TemplateDescription gives you the constants to configure the content of the FilePicker.
You can access the controls using the CommonFilePickerElementIds and ExtendedFilePickerElementIds and the code example above. |
|
| Back to top |
|
 |
lesguilw Power User

Joined: 09 Nov 2005 Posts: 57 Location: Frenchy in Sevilla, Spain
|
Posted: Tue Nov 22, 2005 6:07 am Post subject: |
|
|
Now I am trying to push it a bit forward but I am still having problems...
The thing is that using the FilePicker that way doesn't exactly match the behaviour of the regular SaveAs dialog, because when you save, the newly saved document doesn't replace the old one in the OOo window. I still have the old document open.
To fix that I tried to add this line at the end of the above script:
| Code: | | oDoc = StarDesktop.LoadComponentFromURL(sFileURL, "_self", 0, Array()) |
But nothing happens...
If I try this one:
| Code: | | oDoc = StarDesktop.LoadComponentFromURL(sFileURL, "_blank", 0, Array()) |
I obtain my document in a new window, the old one still stays on (normal) but my macros are not accessible unless I close and re-open the document.
I also tried to close the oDoc with
before doing
| Code: | | oDoc = StarDesktop.LoadComponentFromURL(sFileURL, "_self", 0, Array()) |
but it refuses to close, the dispose method didn't help either.
An idea anyone?
Will
Last edited by lesguilw on Tue Nov 22, 2005 10:13 am; edited 1 time in total |
|
| Back to top |
|
 |
SergeM Super User

Joined: 09 Sep 2003 Posts: 3211 Location: Troyes France
|
|
| Back to top |
|
 |
lesguilw Power User

Joined: 09 Nov 2005 Posts: 57 Location: Frenchy in Sevilla, Spain
|
Posted: Thu Nov 24, 2005 3:44 am Post subject: |
|
|
Ok, I think I am not too far from a solution but it still doesn't work perfectly, I have one more problem.
Now I use this code :
| Code: | Sub SaveAs()
Dim oDoc
oDoc = ThisComponent
oDocCtrl = oDoc.getCurrentController()
oDocFrame = oDocCtrl.getFrame()
'MimeType = oDoc.DocumentInfo.MIMEType
'MsgBox MimeType
'Set the Dialog Arguments to a Template for a FILESAVE Dialog Box
sFilePickerArgs = Array(com.sun.star.ui.dialogs.TemplateDescription.FILESAVE_AUTOEXTENSION_PASSWORD )
'Register the Service for Filepicker
oFilePicker = CreateUnoService( "com.sun.star.ui.dialogs.FilePicker" )
'Configure the FilePicker
With oFilePicker
'Initia el FilePicker con las opciones de template FILESAVE_AUTOEXTENSION_PASSWORD
.Initialize ( sFilePickerArgs() )
'Define la carpeta a utilizar
sDirectory = oFilePicker.getDisplayDirectory
.SetDisplayDirectory (sDirectory)
'Define el nombre del documento
.SetDefaultName (oDlgMetadatos.Model.txtIdDoc.Text)
'Define los filtros de documentos
.AppendFilter( "All files (*.*)", "*.*" )
.AppendFilter( "OpenDocument Text (.odt)", "*.odt" )
.AppendFilter( "OpenDocument Text Template (.ott)", "*.ott" )
.SetCurrentFilter( "OpenDocument Text (.odt)" )
'Configura las opciones de autoextension y password
.SetValue(com.sun.star.ui.dialogs.ExtendedFilePickerElementIds.CHECKBOX_AUTOEXTENSION, 0, true)
.SetValue(com.sun.star.ui.dialogs.ExtendedFilePickerElementIds.CHECKBOX_PASSWORD, 0, false)
'.EnableControl(com.sun.star.ui.dialogs.ExtendedFilePickerElementIds.CHECKBOX_PASSWORD, false)
End With
'Open the dialog box and saves file
If oFilePicker.execute() Then
sFiles = oFilePicker.getFiles()
sFileURL = sFiles(0)
oDoc.storeToURL( sFileURL, Array() )
MsgBox sFileURL
DocumentReOpen(sFileURL)
End If
End Sub
Sub DocumentReOpen(sFileURL As String)
oDoc = ThisComponent
If (Not GlobalScope.BasicLibraries.isLibraryLoaded("Tools")) Then
GlobalScope.BasicLibraries.LoadLibrary("Tools")
End If
oDocReOpened = StarDesktop.LoadComponentFromURL(sFileURL, "_blank", 0, Array())
DisposeDocument(oDoc)
End Sub |
The DocumentReOpen method is used to close the old document and open the saved one. This works fine, the old document closes thanks to the DisposeDocument() method found in the tool library and the new document opens. The problem is I can't use the macros inside the new document unless I close it and reopen it again. Neither the buttons nor the "Run Macro..." option can trigger one the macros unless I do this "restart".
It seems that the LoadComponentFromURL() doens't work normally.
Anyone knows why?
Will |
|
| Back to top |
|
 |
lesguilw Power User

Joined: 09 Nov 2005 Posts: 57 Location: Frenchy in Sevilla, Spain
|
Posted: Thu Nov 24, 2005 4:14 am Post subject: |
|
|
I think I know why the macros doen't work, it is because the document is opened with macros disabled without any warning. I mean OOo doesn't ask me if I want them enabled or not... I am working on it thanks to Andrew Pytoniak's document page 40, hopefully I'll provide a working code soon  |
|
| Back to top |
|
 |
lesguilw Power User

Joined: 09 Nov 2005 Posts: 57 Location: Frenchy in Sevilla, Spain
|
Posted: Thu Nov 24, 2005 4:19 am Post subject: |
|
|
And here it is! Thanks Andrew for your great document!
| Code: | Sub SaveAs()
Dim oDoc
oDoc = ThisComponent
oDocCtrl = oDoc.getCurrentController()
oDocFrame = oDocCtrl.getFrame()
'MimeType = oDoc.DocumentInfo.MIMEType
'MsgBox MimeType
'Set the Dialog Arguments to a Template for a FILESAVE Dialog Box
sFilePickerArgs = Array(com.sun.star.ui.dialogs.TemplateDescription.FILESAVE_AUTOEXTENSION_PASSWORD )
'Register the Service for Filepicker
oFilePicker = CreateUnoService( "com.sun.star.ui.dialogs.FilePicker" )
'Configure the FilePicker
With oFilePicker
'Initia el FilePicker con las opciones de template FILESAVE_AUTOEXTENSION_PASSWORD
.Initialize ( sFilePickerArgs() )
'Define la carpeta a utilizar
sDirectory = oFilePicker.getDisplayDirectory
.SetDisplayDirectory (sDirectory)
'Define el nombre del documento
.SetDefaultName (oDlgMetadatos.Model.txtIdDoc.Text)
'Define los filtros de documentos
.AppendFilter( "All files (*.*)", "*.*" )
.AppendFilter( "OpenDocument Text (.odt)", "*.odt" )
.AppendFilter( "OpenDocument Text Template (.ott)", "*.ott" )
.SetCurrentFilter( "OpenDocument Text (.odt)" )
'Configura las opciones de autoextension y password
.SetValue(com.sun.star.ui.dialogs.ExtendedFilePickerElementIds.CHECKBOX_AUTOEXTENSION, 0, true)
.SetValue(com.sun.star.ui.dialogs.ExtendedFilePickerElementIds.CHECKBOX_PASSWORD, 0, false)
'.EnableControl(com.sun.star.ui.dialogs.ExtendedFilePickerElementIds.CHECKBOX_PASSWORD, false)
End With
'Open the dialog box and saves file
If oFilePicker.execute() Then
sFiles = oFilePicker.getFiles()
sFileURL = sFiles(0)
oDoc.storeToURL( sFileURL, Array() )
MsgBox sFileURL
DocumentReOpen(sFileURL)
End If
End Sub
Sub DocumentReOpen(sFileURL As String)
Dim mFileProperties(0) As New com.sun.star.beans.PropertyValue
oDoc = ThisComponent
If (Not GlobalScope.BasicLibraries.isLibraryLoaded("Tools")) Then
GlobalScope.BasicLibraries.LoadLibrary("Tools")
End If
mFileProperties(0).Name="MacroExecutionMode"
mFileProperties(0).Value=4
oDocReOpened = StarDesktop.LoadComponentFromURL(sFileURL, "_blank", 0, mFileProperties())
DisposeDocument(oDoc)
End Sub |
|
|
| Back to top |
|
 |
phazzy Power User

Joined: 26 Mar 2009 Posts: 60 Location: Bucharest, Romania
|
Posted: Tue Apr 12, 2011 4:37 am Post subject: |
|
|
Hello,
Thanks for the snippets they were really useful to me.
About the loading of the new document (the one that was just saved) into the same window, I think one can use storeAsURL as opposed to storeToURL.
From OOO API:
| Quote: |
storeAsURL - stores the object's persistent data to a URL and makes this URL the new location of the object.
storeToURL - stores the object's persistent data to a URL and continues to be a representation of the old URL.
|
Cheers |
|
| Back to top |
|
 |
|
|
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
|