| View previous topic :: View next topic |
| Author |
Message |
schelle General User


Joined: 30 Oct 2003 Posts: 45 Location: Australia
|
Posted: Thu Oct 30, 2003 11:54 pm Post subject: macro to save in three formats |
|
|
Hi,
I have just created my first (extremely basic) macro. It does what I want it to - when I hit "shift+ctrl+s", it saves the open document 4 times - in a backup folder as an .RTF, a .DOC and an .SXW file, and in a working folder as an .SXW.
What I want to know is, can I get it to use the open document's name when it saves, or would I need to write a new macro for each document name?
Here is the macro I produced.
REM ***** BASIC *****
Sub Main
End Sub
sub saver
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 ----------------------------------------------------------------------
dim args1(1) as new com.sun.star.beans.PropertyValue
args1(0).Name = "URL"
args1(0).Value = "file:///H:/backup/essay.rtf"
args1(1).Name = "FilterName"
args1(1).Value = "Rich Text Format"
dispatcher.executeDispatch(document, ".uno:SaveAs", "", 0, args1())
rem ----------------------------------------------------------------------
dim args2(1) as new com.sun.star.beans.PropertyValue
args2(0).Name = "URL"
args2(0).Value = "file:///H:/backup/essay.doc"
args2(1).Name = "FilterName"
args2(1).Value = "MS Word 97"
dispatcher.executeDispatch(document, ".uno:SaveAs", "", 0, args2())
rem ----------------------------------------------------------------------
dim args3(1) as new com.sun.star.beans.PropertyValue
args3(0).Name = "URL"
args3(0).Value = "file:///H:/backup/essay.sxw"
args3(1).Name = "FilterName"
args3(1).Value = "StarOffice XML (Writer)"
dispatcher.executeDispatch(document, ".uno:SaveAs", "", 0, args3())
rem ----------------------------------------------------------------------
dim args4(1) as new com.sun.star.beans.PropertyValue
args4(0).Name = "URL"
args4(0).Value = "file:///J:/working/essay.sxw"
args4(1).Name = "FilterName"
args4(1).Value = "StarOffice XML (Writer)"
dispatcher.executeDispatch(document, ".uno:SaveAs", "", 0, args4())
end sub
In this example, the document name is "essay". If I opened a document named "story" and hit "shift+ctrl+s" then story would be saved 4 times as essay!!! I want it to save as story, obviously (or notes, or whatever the open document is named).
Can anyone help???
Schelle _________________ "Poetry is the journal of a sea animal living on land, wanting to fly in the air."
(Carl Sandberg) |
|
| Back to top |
|
 |
DannyB Moderator


Joined: 02 Apr 2003 Posts: 3991 Location: Lawrence, Kansas, USA
|
Posted: Fri Oct 31, 2003 9:23 am Post subject: |
|
|
I don't know if this helps. But you can get the document's title like this.
| Code: | oTextDoc = ThisComponent
cDocumentTitle = oTextDoc.getDocumentInfo().Title
MsgBox( cDocumentTitle )
|
The document title needs to be set in
File --> Properties --> Description --> Title
You could get the document's current pathname like this...
| Code: | oTextDoc = ThisComponent
cDocumentURL = oTextDoc.getLocation()
cDocumentPathname = ConvertFromURL( cDocumentTitle )
MsgBox( "The document's pathname is: " + cDocumentPathname )
|
Here is a complete set of macro code that saves the document, a backup copy, and the copies in several other formats. The document name is based on the document title. So you MUST first set the document title by picking
File --> Properties --> Description --> Title.
| Code: | Sub Main
oTextDoc = ThisComponent
cDocumentTitle = oTextDoc.getDocumentInfo().Title
If Len( cDocumentTitle ) = 0 Then
MsgBox( "You need to set the document title first. Pick File -> Properties -> Description -> Title." )
Exit Sub
EndIf
cFolderToSave = "c:\documents and settings\dbrewer\desktop"
cBackupFolder = "c:\documents and settings\dbrewer\desktop\backup"
' Save the document as SXW
cURL = ConvertToURL( cFolderToSave + "\" + cDocumentTitle + ".sxw" )
oTextDoc.storeAsUrl( cURL, Array() )
' Save a backup as SXW
cURL = ConvertToURL( cBackupFolder + "\" + cDocumentTitle + ".sxw" )
oTextDoc.storeToUrl( cURL, Array() )
' Save a backup as RTF
cURL = ConvertToURL( cBackupFolder + "\" + cDocumentTitle + ".rtf" )
oTextDoc.storeToUrl( cURL, Array( MakePropertyValue( "FilterName", "Rich Text Format" ) ) )
' Save a backup as DOC
cURL = ConvertToURL( cBackupFolder + "\" + cDocumentTitle + ".doc" )
oTextDoc.storeToUrl( cURL, Array( MakePropertyValue( "FilterName", "MS WinWord 6.0" ) ) )
' Save a backup as a PDF
cURL = ConvertToURL( cBackupFolder + "\" + cDocumentTitle + ".pdf" )
oTextDoc.storeToUrl( cURL, Array( MakePropertyValue( "FilterName", "writer_pdf_Export" ) ) )
End Sub
' Create and return a new com.sun.star.beans.PropertyValue.
'
Function MakePropertyValue( Optional cName As String, Optional uValue ) As com.sun.star.beans.PropertyValue
Dim oPropertyValue As New com.sun.star.beans.PropertyValue
If Not IsMissing( cName ) Then
oPropertyValue.Name = cName
EndIf
If Not IsMissing( uValue ) Then
oPropertyValue.Value = uValue
EndIf
MakePropertyValue() = oPropertyValue
End Function
|
This is hand-written code. Not the stuff generated by the macro recorder. _________________ Want to make OOo Drawings like the colored flower design to the left? |
|
| Back to top |
|
 |
schelle General User


Joined: 30 Oct 2003 Posts: 45 Location: Australia
|
Posted: Fri Oct 31, 2003 7:58 pm Post subject: |
|
|
A thousand blessings upon you, oh great and mighty problem saver.
That works a treat - with the added advantage of a .pdf copy as well.
Thank you OOo and THANK YOU, DannyB! _________________ "Poetry is the journal of a sea animal living on land, wanting to fly in the air."
(Carl Sandberg) |
|
| Back to top |
|
 |
schelle General User


Joined: 30 Oct 2003 Posts: 45 Location: Australia
|
Posted: Wed Nov 05, 2003 8:26 pm Post subject: |
|
|
I'm pushing my luck now ... is there a way to automate this process, so that the macro will run itself, say every 10 minutes?
And is it possible for it to warn me if I am about to overwrite my working file (and backups) because I have opened an older version stored in a different folder? Possibly by suggesting I change the document title every time i manually save (assuming automatic saving is possible)?
(and what about a cure for human stupidity while you're there???)
Thanks,
Schelle _________________ "Poetry is the journal of a sea animal living on land, wanting to fly in the air."
(Carl Sandberg) |
|
| Back to top |
|
 |
csh General User

Joined: 22 Dec 2003 Posts: 8
|
Posted: Mon Dec 22, 2003 3:26 pm Post subject: |
|
|
| DannyB wrote: | I don't know if this helps. But you can get the document's title like this.
| Code: | oTextDoc = ThisComponent
cDocumentTitle = oTextDoc.getDocumentInfo().Title
MsgBox( cDocumentTitle )
|
|
This didn't work for me. I kept getting errors about a missing path "/<document title>"
Also, how could I modify this script to save based on the filename, rather than the document title? My document titles, if I use them, are always different than my filenames. |
|
| Back to top |
|
 |
JohnV Administrator

Joined: 07 Mar 2003 Posts: 8976 Location: Lexinton, Kentucky, USA
|
Posted: Mon Dec 22, 2003 4:59 pm Post subject: |
|
|
Hi csh,
I just copied and pasted your code and it works just fine.
If you have saved the current file you can get its URL with " thisComponent.URL" and use that as the starting point for saving to other file types.
Last edited by JohnV on Tue Dec 23, 2003 11:05 am; edited 1 time in total |
|
| Back to top |
|
 |
csh General User

Joined: 22 Dec 2003 Posts: 8
|
Posted: Tue Dec 23, 2003 10:52 am Post subject: |
|
|
| JohnV wrote: | Hi csh,
I just copied and pasted your code and it works just fine.
If you have saved the current file you can get its URL with " thisComponent.URL" and use that a the starting point for saving to other file types. |
What OS are you running?
I'm running Linux, and this isn't working for me. Here is the exact error that I am getting, trying to save this document:
/home/shannon/files/doc/resume_new.stw
The path "/resume_new" does not exist.
I'll try using thisComponent.URL later, but I have a feeling something more fundamental is breaking. |
|
| Back to top |
|
 |
DannyB Moderator


Joined: 02 Apr 2003 Posts: 3991 Location: Lawrence, Kansas, USA
|
|
| Back to top |
|
 |
JohnV Administrator

Joined: 07 Mar 2003 Posts: 8976 Location: Lexinton, Kentucky, USA
|
Posted: Tue Dec 23, 2003 12:05 pm Post subject: |
|
|
While Danny was posting I was composing so here's mine.
My OS was Windows98 but I just did this in Linux which ran fine:
| Code: | Doc = thisComponent
DocUrl = doc.URL
DocUrl = Left(DocUrl,Len(DocUrl)-7) & "BAK.sxw"
path = "/home/john/docs/resume_new.sxw"
'Save over current file assuming the original file is resume_new
cURL = ConvertToURL( path )
doc.storeToUrl( cURL,Array() )
'Save with another name, i.e., resume_BAK.swx, same assumption
cURL = ConvertToURL( DocUrl )
doc.storeToUrl( cURL,Array() ) |
|
|
| Back to top |
|
 |
csh General User

Joined: 22 Dec 2003 Posts: 8
|
Posted: Tue Dec 23, 2003 2:36 pm Post subject: |
|
|
| JohnV wrote: | While Danny was posting I was composing so here's mine.
My OS was Windows98 but I just did this in Linux which ran fine:
| Code: | Doc = thisComponent
DocUrl = doc.URL
DocUrl = Left(DocUrl,Len(DocUrl)-7) & "BAK.sxw"
path = "/home/john/docs/resume_new.sxw"
'Save over current file assuming the original file is resume_new
cURL = ConvertToURL( path )
doc.storeToUrl( cURL,Array() )
'Save with another name, i.e., resume_BAK.swx, same assumption
cURL = ConvertToURL( DocUrl )
doc.storeToUrl( cURL,Array() ) |
|
Ah, that's simpler than what I did using string operations.
Now, how do you call a routine to save in the other formats?
When I call the routines in the original posted script, after creating a URL
like I want it, OO says the routine doesn't exist. I'm talking about the
routine which calls the filters named in the Array() call. |
|
| Back to top |
|
 |
csh General User

Joined: 22 Dec 2003 Posts: 8
|
Posted: Tue Dec 23, 2003 3:01 pm Post subject: |
|
|
I process the string as a pathname first, and then convert it to a URL.
So far I have modified your code to look like this:
| Code: |
Sub Saver
oTextDoc = ThisComponent
cDocumentTitle = thisComponent.URL
position = InStr(cDocumentTitle,".sxw")
if (position = 0) then
msgbox("Document doesn't have .sxw extension: " + cDocumentTitle)
exit sub
endif
cdocumenttitle = left(cdocumenttitle,(position - 1))
' Save the document as SXW
cURL = ConvertToURL( cDocumentTitle + ".sxw" )
oTextDoc.storeAsUrl( cURL, Array() )
' Save a backup as RTF
cURL = ConvertToURL( cDocumentTitle + ".rtf" )
oTextDoc.storeToUrl( cURL, Array( MakePropertyValue( "FilterName", "Rich Text Format" ) ) )
' Save a backup as DOC
cURL = ConvertToURL( cDocumentTitle + ".doc" )
oTextDoc.storeToUrl( cURL, Array( MakePropertyValue( "FilterName", "MS WinWord 6.0" ) ) )
' Save a backup as a PDF
cURL = ConvertToURL( cDocumentTitle + ".pdf" )
oTextDoc.storeToUrl( cURL, Array( MakePropertyValue( "FilterName", "writer_pdf_Export" ) ) )
End Sub
|
This gives me the filenames/urls that I want.
However, the call:
| Code: |
' Save a backup as RTF
cURL = ConvertToURL( cDocumentTitle + ".rtf" )
oTextDoc.storeToUrl( cURL, Array( MakePropertyValue( "FilterName", "Rich Text Format" ) ) )
|
...is failing now.
The error says the function or method is missing.
The problem seems to be that oTextDoc is being set to nothing, or at least in the debugger the value shows up as "<?>". However, the first call to storeToUrl works fine.
Edit: I suppose that <?> just means it is a reference the debugger cannot print. I'm used to Perl or C where you get an address so you can tell if it is defined or not, so my assumption there is obviously wrong. That means the MakePropertyValue() function is missing I assume, and... I have no idea why that would be.
I would like to run this in the debugger, but I can't seem to figure out how to make "ThisDocument" point to a running instance of Writer. For now, I set a breakpoint, go to Writer, and start the macro. This is time consuming, and may not be doing what I think it is either.
I'm a UNIX programmer, and doing things in an IDE in BASIC like this feels really odd right now.
I'm still playing around, but any pointers appreciated.
Last edited by csh on Tue Dec 23, 2003 3:24 pm; edited 1 time in total |
|
| Back to top |
|
 |
DannyB Moderator


Joined: 02 Apr 2003 Posts: 3991 Location: Lawrence, Kansas, USA
|
|
| Back to top |
|
 |
csh General User

Joined: 22 Dec 2003 Posts: 8
|
Posted: Tue Dec 23, 2003 5:31 pm Post subject: |
|
|
| DannyB wrote: | | You do have the MakePropertyValue function I wrote? |
Crap...
I figured it was a built-in function, and completly missed the second routine.
Thanks.
Eventually I'll change this to use a single function for saving in a list of given formats. |
|
| Back to top |
|
 |
8daysaweek.co.uk Super User


Joined: 29 Nov 2003 Posts: 2130 Location: UK
|
Posted: Sat Dec 27, 2003 4:37 am Post subject: |
|
|
| DannyB wrote: | You could get the document's current pathname like this...
| Code: | oTextDoc = ThisComponent
cDocumentURL = oTextDoc.getLocation()
cDocumentPathname = ConvertFromURL( cDocumentTitle )
MsgBox( "The document's pathname is: " + cDocumentPathname )
|
|
I thought this was going to help me do what I want but not yet!
This gives me the path of the file including the filename. I would like to extract the path UP TO the filename (ie. the folder the file exists in)
I'm new to this but have learned a fair bit by playing with the code I can obviously get the Title, the URL and PathName, but is there a way of extracting the existing filename? (which I could use to find the current folder using an expression like JohnV's:
DocUrl = Left(DocUrl,Len(DocUrl)-7)
TIA _________________ James
www.8daysaweek.co.uk - A User-Focused OOo site |
|
| Back to top |
|
 |
JohnV Administrator

Joined: 07 Mar 2003 Posts: 8976 Location: Lexinton, Kentucky, USA
|
Posted: Sat Dec 27, 2003 9:19 am Post subject: |
|
|
| Quote: | I would like to extract the path UP TO the filename (ie. the folder the file exists in)
|
Here's one way;
| Code: | oDoc = thisComponent
sUrl = oDoc.URL
x = Len(sUrl)
while (Mid(sUrl,x,1) <> "/")
x = x - 1
wend
sUrl = Left(sUrl,x)
msgbox sUrl |
|
|
| 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
|