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

Using com for OOo with VB scripts

 
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Code Snippets
View previous topic :: View next topic  
Author Message
rchee
Newbie
Newbie


Joined: 11 Jan 2006
Posts: 2

PostPosted: Thu Jan 12, 2006 12:18 am    Post subject: Using com for OOo with VB scripts Reply with quote

Hi, I am new to programming. Been writing VBscripts for myself. Thanks to articles by cybb20, alain didier and others on ducoment conversion, I have made a VBscript to use with my OOo 1.4 to convert sxw to doc. This does not need openoffice macro because it uses the com directly. I thought I would share this with the forum members:

----------8<-----------------8<--------------------------------------8<--------------------------8<---

' get argument passed on to this VBscript
Dim StrFname
If Wscript.Arguments.Count < 1 Then 'this checks that filename is specified
Wscript.Echo "usage: sxw2doc <Filename.sxw>"
Wscript.Quit(1)
End If
StrFname = Wscript.Arguments(0)
msgbox(StrFname)
'change the path to openoffice url format
StrFname = Replace(StrFname, ":" , "|")
StrFname = Replace(StrFname, "\" , "/")
StrFname = "file:///" & StrFname
msgbox(StrFname)


' main
Dim objServiceManager
Dim objDesktop
Dim args(0)
Dim cURL
Dim oDoc


Set objServiceManager= CreateObject("com.sun.star.ServiceManager")
Set Stardesktop= objServiceManager.createInstance("com.sun.star.frame.Desktop")


cURL = StrFname


Set args(0) = MakePropertyValue("Hidden", True)

Set oDoc = StarDesktop.loadComponentFromURL( cURL, "_blank", 0, args )

cURL = Left(StrFname, len(StrFname)-4) & ".doc"

Set args(0) = MakePropertyValue( "FilterName", "MS Word 97" )
call oDoc.storeToURL( cURL, args )
oDoc.close( True )

Set oDoc = nothing
Set Stardesktop = nothing
Set objServiceManager = nothing
msgbox "Done. Remember that SOffice is still on." 'I do not know how to kill soffice
wscript.quit

Function MakePropertyValue(cName, uValue)
Dim oStruct
Set oStruct = objServiceManager.Bridge_GetStruct("com.sun.star.beans.PropertyValue")
oStruct.Name = cName
oStruct.Value = uValue
Set MakePropertyValue = oStruct
End Function


---------8<-----------8<-----------------8<--------------------8<---------------------

You will need to use folder options to associated .sxw files with the following :
"C:\windows\wscript.exe" "C:\some\path\to\above\VBscript.vbs" "%1"

In detail: Just cut and paste the above script (between the scissors ---8<----) into a text file (using notepad). Then name the script above as sxw2doc.vbs (select all files *.* as the save format). Then move the file to whenever you like, but remember where you put it. Then click on Tools>Folder options>File Types. Here, from the file type list, pick .sxw and click the 'advanced' button. In the edit box, select new. In the new box, enter an action, example "convert sxw2doc" (this is just a label that will appear when you right-click on sxw files). Application used to perform this action will be "C:windows\wscript.exe" "C:\where\you\put\sxw2doc.vbs" "%1".
All done when you finish clicking on all the OK buttons.

If you want to remove it, just go to tools>folder options.file types again and delete "convert sxw2doc". You can easily delete sxw2doc.vbs too.

I am also new to forums, so please excuse me if any offence. I hope you find this vbscript helpful, and please don't blame me for damages.
Back to top
View user's profile Send private message
SergeM
Super User
Super User


Joined: 09 Sep 2003
Posts: 3211
Location: Troyes France

PostPosted: Thu Jan 12, 2006 8:51 am    Post subject: Reply with quote

It's always a good idea to share Code. Thank you.
_________________
Linux & Windows OOo3.0
UNO & C++ : WIKI
http://wiki.services.openoffice.org/wiki/Using_Cpp_with_the_OOo_SDK
In French
http://wiki.services.openoffice.org/wiki/Documentation/FR/Cpp_Guide
Back to top
View user's profile Send private message Visit poster's website
hol.sten
Super User
Super User


Joined: 14 Nov 2004
Posts: 3533
Location: Hamburg, Germany

PostPosted: Thu Jan 12, 2006 11:35 am    Post subject: Re: Using com for OOo with VB scripts Reply with quote

rchee wrote:
Application used to perform this action will be "C:windows\wscript.exe" "C:\where\you\put\sxw2doc.vbs" "%1".

First of all: I never thought that it might be that easy to create a PDF with OOo Smile

To get your code working on my Windows XP SP 2 computer I had to change the action to
Code:
"C:\windows\system32\wscript.exe" "C:\where\you\put\sxw2doc.vbs" "%1"


After I got it running I simply changed
Code:
cURL = Left(StrFname, len(StrFname)-4) & ".doc"

Set args(0) = MakePropertyValue( "FilterName", "MS Word 97" )

to
Code:
cURL = Left(StrFname, len(StrFname)-4) & ".pdf"

Set args(0) = MakePropertyValue( "FilterName", "writer_pdf_Export" )

and had a really useful script.

With kind regards
hol.sten
Back to top
View user's profile Send private message
rchee
Newbie
Newbie


Joined: 11 Jan 2006
Posts: 2

PostPosted: Tue Jan 31, 2006 6:11 am    Post subject: Thank you Reply with quote

Thank you to SergeM and hol.sten for noticing. In particular, I am glad hol.sten is able to modify the codes to his/her advantage (I forget wscript is in a different folder for certain versions of windows). Thank you again.
Back to top
View user's profile Send private message
tahoar
Newbie
Newbie


Joined: 25 Jan 2009
Posts: 3

PostPosted: Mon Jan 26, 2009 2:29 am    Post subject: Reply with quote

This code is very close to what I'm looking for, except, instead of saving the document in another format (call oDoc.storeToURL( cURL, args ), I want to print oDoc directly to the default printer without the printer dialog prompt.

I think I have to use the dispatcher object, but all my efforts have failed. Can someone please help?

Thanks,
Tom
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 Code Snippets 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