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 can I create a microsoft object in a macro?

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


Joined: 03 Jul 2003
Posts: 7
Location: Austria

PostPosted: Fri Jul 04, 2003 3:57 am    Post subject: How can I create a microsoft object in a macro? Reply with quote

I would like to use the Scripting.FileSystemObject in a Macro. How can i create it?

Set oFso = CreateObject("Scripting.FileSystemObject") does not work. Is there an other way?

Confused
_________________
Service with a smi)e
Back to top
View user's profile Send private message Visit poster's website
ij_poulton
Newbie
Newbie


Joined: 10 Aug 2003
Posts: 3

PostPosted: Sun Aug 10, 2003 8:50 pm    Post subject: Reply with quote

i would really like to know how to do this as well. OO is great but I use it on windows so really need to work with COM objects.
Back to top
View user's profile Send private message
kaps
General User
General User


Joined: 04 Dec 2003
Posts: 5

PostPosted: Tue Dec 16, 2003 2:41 am    Post subject: How can I create a microsoft object in a macro? Reply with quote

Hi,

Any inputs on this would be very useful. I have been trying to find this myself too.

Regards,

Kaps
Back to top
View user's profile Send private message
DannyB
Moderator
Moderator


Joined: 02 Apr 2003
Posts: 3991
Location: Lawrence, Kansas, USA

PostPosted: Tue Dec 16, 2003 3:16 pm    Post subject: Reply with quote

Myron wrote:
I would like to use the Scripting.FileSystemObject in a Macro. How can i create it?

CreateObject() is not a function of OOo Basic, so you cannot call it from OOo Basic, if that is what you mean by "in a macro". I'm assuming that you would want CreateObject() to return a COM object under Windows. Of course, OOo Basic doesn't know anything about COM as it is platform independant. A program you write in OOo Basic runs on Any OOo, even on an OS that you've never used, such as Mac OS X, or Linux. (Or some new platform to which OOo gets ported to tomorrow that didn't even exist today!)

Myron wrote:
Set oFso = CreateObject("Scripting.FileSystemObject") does not work. Is there an other way?

There may be another way, that also has the virtue of not being locked into Windows, and might do what you need.

ij_poulton wrote:
i would really like to know how to do this as well. OO is great but I use it on windows so really need to work with COM objects.

You might try writing in any Windows Automation language, such as Visual Basic. Then you can script OOo, plus use your Scripting.FileSystemObject, or any other features you are familiar with.

kaps wrote:
Any inputs on this would be very useful. I have been trying to find this myself too.


Since this might help several people, here is some information that I do not remember being discussed before on OOoForum.

Try looking at the Universal Content Broker chapter of the Developer's Guide.
http://api.openoffice.org/docs/DevelopersGuide/UCB/UCB.htm#1+Universal+Content+Broker

Now look at the API guide for the com.sun.star.ucb.SimpleFileAccess service.
http://api.openoffice.org/docs/common/ref/com/sun/star/ucb/SimpleFileAccess.html

As you can see, if you look at the XSmpleFileAccess interface....
http://api.openoffice.org/docs/common/ref/com/sun/star/ucb/XSimpleFileAccess.html
...this service provides a way to see if a file or folder exists. Delete a file or folder. Create a folder. Copy, move or rename a file or folder. Inspect some file attributes, and get or set the Read Only attribute. You can even open a file for reading and/or writing.

Are these the things you want to do? If so, then here is a brief example.

Code:
Sub CheckFileExists
   ' The SimpleFileAccess service provides mechanisms to open, read, write files,
   '  as well as scan the directories of folders to see what they contain.
   ' The advantage of this over Basic's ugly file manipulation is that this
   '  technique works the same way in any programming language.
   '  Furthermore, the program could be running on one machine, while the SimpleFileAccess
   '  accesses files from the point of view of the machine running OOo, not the machine
   '  where, say a remote Java or Python program is running.
   oSimpleFileAccess = createUnoService( "com.sun.star.ucb.SimpleFileAccess" )

   ' Let's check to see if a file exists.
   cPath = "/home/danny/Desktop/ledplanck.pdf"
   cURL = ConvertToURL( cPath )
   If oSimpleFileAccess.exists( cURL ) Then
      MsgBox( cPath + " exists." )
   EndIf
End Sub


This routine checks to see if a certian file exists on my Linux system. (It checks to see if a certian PDF file, recently posted to slashdot, is on my desktop.) I could have also called "isFolder" to see that the item existed, but was NOT a folder. After all, it could have been a folder with the name "ledplanck.pdf".

Want to see a file I/O example? First, we'll need some useful subroutines.....

Code:
' Convert a byte value from the range -128 to +127 into
'  an integer in the range 0 to 255.
' This function is the opposite of IntegerToByte().
Function ByteToInteger( ByVal nByte As Integer ) As Integer
   If nByte < 0 Then
      nByte = nByte + 256
   EndIf
   ByteToInteger() = nByte
End Function


' Convert an array of bytes to a string.
' Pass in an array of bytes.  Each "byte" is an integer value
'  from -128 to +127.  The array of bytes could have come
'  from a com.sun.star.io.XInputStream.
' The function returns a string.
' This function is the opposite of StringToByteArray().
Function ByteArrayToString( aByteArray )
   cBytes = ""
   For i = LBound( aByteArray ) To UBound( aByteArray )
      nByte = aByteArray(i)
      nByte = ByteToInteger( nByte )
      cBytes = cBytes + Chr( nByte )
   Next i
   ByteArrayToString() = cBytes
End Function


' This function is the opposite of ByteToInteger().
Function IntegerToByte( ByVal nByte As Integer ) As Integer
   If nByte > 127 Then
      nByte = nByte - 256
   EndIf
   IntegerToByte() = nByte
End Function


' Convert a string into an array of bytes.
' Pass a string value to the cString parameter.
' The function returns an array of bytes, suitable
'  for writing to a com.sun.star.io.XOutputStream.
' This function is the opposite of ByteArrayToString().
Function StringToByteArray( ByVal cString As String )
   nNumBytes = Len( cString )
   Dim aBytes(nNumBytes-1) As Integer
   For i = 1 To nNumBytes
      cChar = Mid( cString, i, 1 )
      nByte = Asc( cChar )
      nByte = IntegerToByte( nByte )
      aBytes(i-1) = nByte
   Next
   StringToByteArray() = aBytes()
End Function


' This is pure syntax sugar.
' Just a convenient way of writing a string value
'  to a com.sun.star.io.XOutputStream.
Sub FileWriteString( oOutputStream As com.sun.star.io.XOutputStream,_
               ByVal cString As String )
   ' Convert the string into an array of bytes.
   aBytesToWrite = StringToByteArray( cString )

   ' Write the bytes to the output file.
   oOutputStream.writeBytes( aBytesToWrite )
End Sub




With that out of the way, here is a short routine to write out a file with the contents "Hello World!".

Code:
Sub Main
   WriteHelloWorld( ConvertToURL( "/home/danny/Desktop/Hello World.txt" ) )
End Sub

Sub WriteHelloWorld( cOutputURL )
   oSimpleFileAccess = createUnoService( "com.sun.star.ucb.SimpleFileAccess" )

   ' Open output file.  Delete it if it already exists.   
   If oSimpleFileAccess.exists( cOutputURL ) Then
      oSimpleFileAccess.kill( cOutputURL )
   EndIf
   oOutputStream = oSimpleFileAccess.openFileWrite( cOutputURL )
   
   FileWriteString( oOutputStream, "Hello World!" )
   
   oOutputStream.closeOutput()
End Sub


This approach isn't especially efficient, but it works.

It has the advantage of working on any platform to which OOo has been ported.

So next month, when someone announces that OOo has been ported to the Commodore 64, or to the GameBoy, or something else equally pointless, you can rest assured that your file I/O code in Basic should work.

Besides the issue of differing operating systems, this code also has the property that the file operations take place on the machine where OOo is running, not where the macro is running. For example, if the macro were written in Python or Java, then the file I/O would take place on the OOo machine.

There is even one more issue. OOo Basic has it's own (ugly) file I/O primitives in the Basic language. Some Basic programmers will use these instead of the UNO API provided ones in OOo. The advantage of the approach I describe above is that it works when ported to any language. A Java or Python programmer would use exactly the same Services and Interfaces from the API. That is, the technique would be exactly the same, but expressed in a different programming language.

If I had more time, I would present an Input example. But you should be able to figure it out. Or I could post a routine I have, and let you study it. The routine I would post uses file I/O via. the SimpleFileAccess service to do the following....
* Read a C:\Smile26.BMP file from disk. (An icon that needs to be added to the toolbar or to a user-defined menu command.)
* Write some text into a text file.
* The text written out in the above step is a Basic Function that will reconstruct the binary value of the original C:\Smile26.BMP file. The Basic code is compact because it contains lines of Base64 encoded text (according to RFC 1521).
* This code example clearly does both Input and Output.

Sorry for such a long post. I tend to do that.
_________________
Want to make OOo Drawings like the colored flower design to the left?
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