[Solved] Macro similar to "OpenDocument" & "SaveAs"

The Application Programming Interface and the OASIS Open Document Format

[Solved] Macro similar to "OpenDocument" & "SaveAs"

Postby Chas94539 » Sat Jun 28, 2008 10:21 pm

Hi All!

I'm new to OOo. I have just recently switched from using MS Office to Open Office. So far, I really like the functionality and the similarity of OOo to MS Office!

To make my experience a bit more robust, I would like to have a single button on a tool bar which would open a specific directory. The function would be very similar to the "open directory" icon on the standard toolbar. The only difference would be the directory which is opened. The standard "open document" button opens the last used directory, I would like to have a custom button which would open "D:\ChasData\Document\SeqFile\2008\" and then I could choose the file in that directory I wanted to edit.

A second macro I would like is a button which would allow me to "SaveAs" to specific directory with a file name of "caf-2008-00xx" The last two digits (xx) would be manually filled in at the time the file is saved. An example of the filename would be “caf-2008-0098.odt" where the "98" would be supplied manually. Also, the file type (“.odt”, “.doc”, etc) would be manually selected at the time the file is saved, just like with the "File|SaveAs" function.

In both cases, the directories are fixed, so I expected this to be a simple task to write a macro to do it. However, I have been overwhelmed by the amount of documentation available! Trying to sort through it has been very time consuming.

I *think* the .uno:open and the .uno:saveas are the two tool bar functions I would like to access. If there is a way to access them and have them open to a specific directory, that would be great. However, I cannot find a reference for the array values of .name and .value to pass to the function, so I cannot properly create the array for both the directory name and a partial filename.

I tried using the following code which was generated by the macro recorder. It would open a directory, but it was the last directory opened, not the specific directory I wanted to go to.

Code: Select all   Expand viewCollapse view
sub openfile
     rem ----------------------------------------------------------------------
     rem define variables
     dim document   as object
     dim dispatcher as object
     rem ----------------------------------------------------------------------
     rem get access to the document
     document   = ThisComponent.CurrentController.Frame
     dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
     rem ----------------------------------------------------------------------
     dispatcher.executeDispatch(document, ".uno:Open", "", 0, Array())
end sub

I modified this code so as to remove the “rem” (remark) in front of the last line of code.

I'm pretty sure I could complete this macro if I had a reference document defining the various array values for the “Array().name” and “Array().value”, but then I could be wrong.

I have been working on this for about a week and have 18 pages of macros I have tried. Unfortunately, I don't feel I'm much closer to a solution. What is really frustrating, is this appears to be such a simple task to do.

This exercise has not been a total loss so far. I have successfully created two other macros. One to open my “Catalog” file and the other to paste unformatted text into a document.

Thanks in advance for any help you may be able to provide.

Bye... :-)
Charles
Last edited by Chas94539 on Sun Jun 29, 2008 7:48 pm, edited 2 times in total.
OOo 3.0.X on Ms Windows XP
Chas94539
 
Posts: 5
Joined: Sun Jun 22, 2008 11:39 pm

Re: Macro similar to "OpenDocument" & "SaveAs"

Postby squenson » Sun Jun 29, 2008 8:49 am

I have found a similar macro in French (here) and adapted and enhanced it:
Code: Select all   Expand viewCollapse view
Sub OpenSpecificFolder()

   Dim FP As Object
   Dim ShExec As Object
   Dim X As Integer


   FP = CreateUnoService("com.sun.star.ui.dialogs.FilePicker")

                     ' Define below the path that should be
                     ' open by default
   FP.SetDisplayDirectory("C:\_Temp")
   
                     ' Define below the filters to only
                     ' show files with specified extension
                     ' First string is plain text, second is the filter itself
   FP.appendFilter("Calc documents (*.ods)", "*.ods")
   FP.appendFilter("Calc templates (*.odt)", "*.odt")
   FP.appendFilter("Text documents", "*.txt;*.csv;*.doc")
   FP.appendFilter("All files", "*.*")
   
                     ' Display the "open" dialog box
                     ' and if a file has been selected, open it
   X = FP.Execute()
   If X = 1 Then
      ShExec = createUnoService("com.sun.star.system.SystemShellExecute")
      ShExec.execute(Fichier.Files(0), "", 0)
   End If
   
End Sub
LibreOffice 4.2.3.3. on Ubuntu 14.04
User avatar
squenson
Volunteer
 
Posts: 1885
Joined: Wed Jan 30, 2008 9:21 pm
Location: Lausanne, Switzerland

Re: Macro similar to "OpenDocument" & "SaveAs"

Postby Chas94539 » Sun Jun 29, 2008 10:02 am

WoW! Thank you squenson for the macro. I had to change "fichier" to "FP" Near the bottom of the macro to get it to work, but it does work!!!! I really appreciate your taking the time to post it! Here is the version I will use that works:

Code: Select all   Expand viewCollapse view
Sub OpenSpecificFolder()
'
'*************************************************************************************************
'Created by SilkyRoad and posted at:
'     http://ooo.developpez.com/faq/?page=Fichier#Question29
'Modified by squenson and posted at:
'     http://user.services.openoffice.org/en/forum/viewtopic.php?
'     f=25&t=7322&sid=8fa3a62509c2a5169e7ee7a8c09f9344
'Modified by Chas94539 on 2008-06-29
'-------------------------------------------------------------------
' Purpose:
'
' This macro will open a specific directory and
'allow a file in the directory to be opened.
'
'-------------------------------------------------------------------
' History
'-------------------------------------------------------------------
' v 1.0    2008-06-29   CAF release.
'**************************************************************************************************
   Dim FP As Object
   Dim ShExec As Object
   Dim X As Integer


   FP = CreateUnoService("com.sun.star.ui.dialogs.FilePicker")

                     ' Define below the path that should be
                     ' open by default
   FP.SetDisplayDirectory("D:\ChasData\Document\SeqFile\2008\")
   
                     ' Define below the filters to only
                     ' show files with specified extension
                     ' First string is plain text, second is the filter itself
'   FP.appendFilter("Calc documents (*.ods)", "*.ods")
'   FP.appendFilter("Calc templates (*.odt)", "*.odt")
'   FP.appendFilter("Text documents", "*.txt;*.csv;*.doc")
   FP.appendFilter("All files", "*.*")
   
                     ' Display the "open" dialog box
                     ' and if a file has been selected, open it
   X = FP.Execute()
   If X = 1 Then
      ShExec = createUnoService("com.sun.star.system.SystemShellExecute")
      ShExec.execute(FP.Files(0), "", 0)
   End If
   
End Sub



Thank you soooo much for this macro.

All I need now is a macro for the "SaveAs" portion and I'll be all set.

Bye... :-)
Charles
OOo 3.0.X on Ms Windows XP
Chas94539
 
Posts: 5
Joined: Sun Jun 22, 2008 11:39 pm

Re: Macro similar to "OpenDocument" & "SaveAs"

Postby hanya » Sun Jun 29, 2008 5:53 pm

Chas94539 wrote:All I need now is a macro for the "SaveAs" portion and I'll be all set.

If you do not need all filter names installed in your OOo, easy to make the macro to do it.
This is an example code and modify SaveAsToSpecificDirectory, GetFilters and GetSuffixAndFileType subroutines according to your environment.
Code: Select all   Expand viewCollapse view
Type FilterDescription
  FileType As String ' display name of the file
  FilterName As String ' internal filter name
  FileExtension As String ' file extension
End Type


Sub SaveAsToSpecificDirectory()
  ' modify according to your environment
  Const sDirectory = "file:///home/user/Desktop/test/"
  Const sPrefix = "caf-2008-00"
  Dim sSuffix As String
  Dim oFilter As Variant
  Dim sFileURL As String
 
  oFilters = GetFilters()
  oResult = GetSuffixAndFileType( oFilters )
  sSuffix = oResult(0)
  oFilter = oResult(1) ' if null, canceled
  bOverwrite = oResult(2)
 
  If NOT IsNull( oFilter ) Then
    sFileURL = sDirectory & sPrefix & sSuffix & "." & oFilter.FileExtension
   
    SaveFile( sFileURL, oFilter.FilterName, bOverwrite )
  End If
End Sub


Function GetFilters()
  Dim tFilters(1) As FilterDescription
  tFilters(0).FileType = "ODF Text Document (.odt)"
  tFilters(0).FilterName = "writer8"
  tFilters(0).FileExtension = "odt"
  tFilters(1).FileType = "Microsoft Word 97/2000/XP (.doc)"
  tFilters(1).FilterName = "MS Word 97"
  tFilters(1).FileExtension = "doc"
  GetFilters = tFilters
End Function


Function GetSuffixAndFileType( oFilters As Object ) As Object
  ' modify these names according to the dialog
  Const sDialogLibName = "Standard"
  Const sDialogName = "Dialog1"
  Dim oDialog As Object
  Dim oDialogModel As Object
  Dim sSuffix As String
  Dim bOverwrite As Boolean
  Dim n As Integer
  n = UBound(oFilters)
  Dim sFilterNames( n )
 
  DialogLibraries.loadLibrary( sDialogLibName )
  oDialog = createUnoDialog( _
      DialogLibraries.getByName( sDialogLibName ).getByName( sDialogName ) )
 
  For i = 0 To n step 1
    sFilterNames(i) = oFilters(i).FileType
  Next
 
  oListBox = oDialog.getControl( "list_filetype" )
  oListBox.addItems( sFilterNames, 0 )
  oListBox.selectItemPos( 0, True )
 
  If oDialog.execute() = 1 Then
    sSuffix = oDialog.getControl( "edit_suffix" ).getText()
    bOverwrite = CBool( oDialog.getControl( "cb_overwrite" ).getState() )
    n = oListBox.getSelectedItemPos()
   
    GetSuffixAndFileType = Array( sSuffix, oFilters( n ), bOverwrite )
  Else
    GetSuffixAndFileType = Array( "", Null, False )
  End If
End Function


Sub SaveFile( _
    sFileURL As String, sFilterName As String, bOverWrite As Boolean )
  Dim oFrame As Object
  Dim aURL As New com.sun.star.util.URL
  Dim Args(2) As New com.sun.star.beans.PropertyValue
 
  oFrame = ThisComponent.getCurrentController().getFrame()
 
  aURL.Complete = ".uno:SaveAs"
  CreateUnoService("com.sun.star.util.URLTransformer")._
      parseStrict(aURL)
 
  Args(0).Name = "URL"
  Args(0).Value = sFileURL
  Args(1).Name = "FilterName"
  Args(1).Value = sFilterName
  Args(2).Name = "Overwrite"
  Args(2).Value = bOverWrite
 
  oD = oFrame.queryDispatch( aURL, "_self", 0 )
  If NOT IsNull( oD ) Then
    oD.dispatch( aURL, Args )
  End If
End Sub

This macro needs the dialog is there in the attached file.
SaveAs_01.odt
This file includes macros and the Dialog1.
(10.31 KiB) Downloaded 810 times


Getting the list of the installed filters is hard work but I have tried to do it in my extension:
http://extensions.services.openoffice.o ... kmarksmenu
And you can make a menu to open a file from a specific directory can be make easily using the extension.
Please, edit this thread's initial post and add "[Solved]" to the subject line if your problem has been solved.
Apache OpenOffice 4-dev on Xubuntu 14.04
hanya
Volunteer
 
Posts: 884
Joined: Fri Nov 23, 2007 9:27 am
Location: Japan

Re: Macro similar to "OpenDocument" & "SaveAs"

Postby Chas94539 » Sun Jun 29, 2008 7:45 pm

Hi hanya!

WoW! The "SaveAs" macro worked first time!!!! Thank you soooo much for sending it to me. I really appreciate the time and effort you put into writing this macro. It is very complex and I'm sure you spent a couple of hours writing and debugging it. Thank you soooo much!!!!

This group is really fantastic. I'm amazed that in less than 24-hours after posting the request, both macros were completed. I want to thank everyone that participated, especially hanya and squenson for spending the time and effort to help me out!

I hope others will find these macros useful. If you do, please post a reply here.

I'm now going to mark these as "Solved".

Thank you!

Bye... :-)
Charles
OOo 3.0.X on Ms Windows XP
Chas94539
 
Posts: 5
Joined: Sun Jun 22, 2008 11:39 pm


Return to UNO API and ODF

Who is online

Users browsing this forum: No registered users and 1 guest