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 to test a file before opening it

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


Joined: 16 Jun 2005
Posts: 3
Location: Annawan, IL

PostPosted: Tue Aug 09, 2005 1:52 pm    Post subject: How to test a file before opening it Reply with quote

Hello,

I'm looking to figure out how to test a file before opening it.

Here's the existing macro code, copied and pasted from the raw .xba file (hence the "'s):

Code:
Sub SxwToDoc( filenameList )

   filenameArray=parse_filenames(filenameList)

   for i = 0 to (Array1_Size(filenameArray)-1)
           cFile = filenameArray(i)

           cURL = ConvertToURL( cFile )

           oDoc = StarDesktop.loadComponentFromURL( cURL, "_blank", 0, Array(_
                    MakePropertyValue( "Hidden", True ),_
                ) )


           cFile = Left( cFile, Len( cFile ) - 4 ) + ".doc"
           cURL = ConvertToURL( cFile )


           oDoc.storeToURL( cURL, Array ( MakePropertyValue ("FilterName","MS Word 97") ) )
           oDoc.close( True )

   next

End Sub


I'm calling this from some php code, in a headless environment (-invisible option using Xvfb for the display), and if I pass it a bogus file name, it hangs OpenOffice as it's waiting for someone to click on the OpenOffice Basic Error popup. If I turn off the headless stuff and watch what it's doing, it pops open the Macro Basic IDE and highlights the line with the storeToURL call.

I'm guessing then, that I need to somehow test this file to make sure it exists before I try and write to it.

Any one have a good suggestion for testing the file? I've searched the oooforums, and haven't come up with much, but I'm willing to bet that this top has been discussed at some point, perhaps off-topic to another post.

Thanks in advance,

/cs
_________________
Chad Storm
Back to top
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger MSN Messenger
ms777
Super User
Super User


Joined: 07 Feb 2004
Posts: 1355

PostPosted: Tue Aug 09, 2005 11:42 pm    Post subject: Reply with quote

test for existance of a file is easy ...
Code:
oSFA = createUNOService ("com.sun.star.ucb.SimpleFileAccess")
if (oSFA.exists(cURL)) then
  ...
  endif


Hope that solves your problem

m777
Back to top
View user's profile Send private message
cstorm
Newbie
Newbie


Joined: 16 Jun 2005
Posts: 3
Location: Annawan, IL

PostPosted: Wed Aug 10, 2005 2:45 pm    Post subject: what about an invalid but existing sxw doc? Reply with quote

How do I check an sxw file to make sure it's actually xml compliant and won't cause openoffice to complain? I have the scenario where the file exists, but isn't XML-compliant, so efforts to convert it to a doc will hang a headless ooffice instance.

Thanks,

/cs
_________________
Chad Storm
Back to top
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger MSN Messenger
ms777
Super User
Super User


Joined: 07 Feb 2004
Posts: 1355

PostPosted: Wed Aug 10, 2005 3:34 pm    Post subject: Reply with quote

how about an on error construct ?
Code:
on error resume next
doc = loadComponentFromUrl(...)
on error goto 0
if Isnull(doc) then
...
endif
Back to top
View user's profile Send private message
Marinus
OOo Advocate
OOo Advocate


Joined: 07 Nov 2004
Posts: 261

PostPosted: Mon Aug 15, 2005 4:41 pm    Post subject: Reply with quote

Not a true answer to checking XML compliancy but perhaps of help still...

Code:

sub Main
   ' Check integrity
   oDoc = StarDesktop.getCurrentComponent()
   
   ' *** adjust for other documenttypes ***
   if not oDoc.SupportsService("com.sun.star.sheet.SpreadsheetDocument") then
      ' do something
   endif
end sub


Cheers,

Marinus.
Back to top
View user's profile Send private message Visit poster's website
cstorm
Newbie
Newbie


Joined: 16 Jun 2005
Posts: 3
Location: Annawan, IL

PostPosted: Mon Aug 15, 2005 4:51 pm    Post subject: Sweet Reply with quote

The on error construct worked perfectly for my needs... thanks ms777 on that suggestion! For this particular situation, if the SXW document is corrupt, I don't want to continue at all, but rather exit out immediately...

Here's what I ended up using:

Code:
Sub X
on error goto ErrorHandler
.
.
.
Exit Sub
ErrorHandler:
    Reset
End Sub

_________________
Chad Storm
Back to top
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger MSN Messenger
Cybb20
Super User
Super User


Joined: 02 Mar 2004
Posts: 1569
Location: Frankfurt, Germany

PostPosted: Wed Aug 17, 2005 3:32 pm    Post subject: Reply with quote

cstorm: Problem solved ? Smile

Christian
_________________
- Knowledge is Power -
Back to top
View user's profile Send private message Send e-mail
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