How to auto Open Excel File in LibreOffice Macro

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
User avatar
Tiger42
Posts: 66
Joined: Tue Apr 28, 2015 12:34 pm
Location: India

How to auto Open Excel File in LibreOffice Macro

Post by Tiger42 »

Hello Guys,

I know how to auto open CSV file in Macro, but this time I need to open input as a Excel file How can I open file when I run libreoffice macro
Here I am using code to open CSV file open as a input

Code: Select all

Sub MacroTest

Dim oDocument as Object
 FName="D:\\input_file" 
 fnURL=ConvertToURL(FName+".csv") 

'Create new document and import data
 oDocument = StarDesktop.LoadComponentFromURL( fnURL, "_blank", 0, _
 Array(MakePropertyValue( "FilterName", "Text - txt - csv (StarCalc)" ), _
         MakePropertyValue( "FilterOptions", "59/9,34,0,1,1/1/1/1/1/1/1/1" )

''' Function

Function MakePropertyValue( Optional cName As String, Optional uValue ) As com.sun.star.beans.PropertyValue 
   oPropertyValue = createUnoStruct( "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
Now what change would be here to open MS excel file

If anybody knows,.

Thnks
V
Last edited by Tiger42 on Thu Nov 17, 2016 9:30 am, edited 2 times in total.
Libre Office 5.0/ Open Office /MS Excel/ Windows 7-8
User avatar
probe1
Volunteer
Posts: 277
Joined: Mon Oct 08, 2007 1:34 am
Location: Chonburi Thailand

Re: How to auto Open Excel File in LibreOffice Macro

Post by probe1 »

Visit www.pitonyak.org and get Andrew's excellent Macro Document.
Proceed to read it!
Cheers
Winfried

DateTime2 extension: insert date, time or timestamp, formatted to your needs
Filaou
Posts: 1
Joined: Mon Jun 01, 2015 11:10 am

Re: How to auto Open Excel File in LibreOffice Macro

Post by Filaou »

if you try to assign a macro, it still only gives you the choice of macros in your Macros or in LibreOffice Macros?
User avatar
Tiger42
Posts: 66
Joined: Tue Apr 28, 2015 12:34 pm
Location: India

Re: How to auto Open Excel File in LibreOffice Macro

Post by Tiger42 »

Thnk you Dear, but couldn't found proper solution
Libre Office 5.0/ Open Office /MS Excel/ Windows 7-8
User avatar
probe1
Volunteer
Posts: 277
Joined: Mon Oct 08, 2007 1:34 am
Location: Chonburi Thailand

Re: How to auto Open Excel File in LibreOffice Macro

Post by probe1 »

You have to read it - searching Andrew's Macro Document for "excel" reveals I do find code examples.
Cheers
Winfried

DateTime2 extension: insert date, time or timestamp, formatted to your needs
User avatar
Tiger42
Posts: 66
Joined: Tue Apr 28, 2015 12:34 pm
Location: India

Re: How to auto Open Excel File in LibreOffice Macro

Post by Tiger42 »

probe1 wrote:You have to read it - searching Andrew's Macro Document for "excel" reveals I do find code examples.

I have also find but Exporting Excel file instead of Importing File :(
Libre Office 5.0/ Open Office /MS Excel/ Windows 7-8
User avatar
probe1
Volunteer
Posts: 277
Joined: Mon Oct 08, 2007 1:34 am
Location: Chonburi Thailand

Re: How to auto Open Excel File in LibreOffice Macro

Post by probe1 »

In your first post you have code to open a CSV file, using a TEXT filter.
Andrew shows you, how to set a FILTERNAME for Excel on export...

Now, go and combine your code with a new filtername
Cheers
Winfried

DateTime2 extension: insert date, time or timestamp, formatted to your needs
User avatar
Tiger42
Posts: 66
Joined: Tue Apr 28, 2015 12:34 pm
Location: India

Re: How to auto Open Excel File in LibreOffice Macro

Post by Tiger42 »

Hello Guys,

Still problem not resolved :(
If I can open .CSV file in macro then How can I open MS Excel file, or text file

In my case I have some input file which in MS excel format and .txt format , So there is issue
I have tried to find out but I couldn't find

If somebody knows then please let me know

Thnks
Tiger
Libre Office 5.0/ Open Office /MS Excel/ Windows 7-8
User avatar
Tiger42
Posts: 66
Joined: Tue Apr 28, 2015 12:34 pm
Location: India

Re: How to auto Open Excel File in LibreOffice Macro

Post by Tiger42 »

Hello Guys,
here some code I tried and its working
Now Just little issue here
If we are use this code then we can open file input as excel "Microsoft excel 97-2003 worksheet" but I can't open file "Microsoft excel worksheet"

Code: Select all

code

Sub Testmacro

Dim oDoc As Object
Dim sUrl As String

Dim Prop(0) as New com.sun.star.beans.PropertyValue

Prop(0).name="FilterName"
Prop(0).value="MS Excel 97"

sUrl = convertToURL("D:\Projects\Input.xls")

if fileExists(sUrl) then
    oDoc = stardesktop.LoadComponentFromURL(sUrl, "_blank",0, Prop())
else
    msgbox "Not found"
end if


rem define variables
dim document as object
dim dispatcher as object

document = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")

rem ----------------------------------------------------------------------
dim args1(0) as new com.sun.star.beans.PropertyValue
args1(0).Name = "Name"
args1(0).Value = "Source"

dispatcher.executeDispatch(document, ".uno:RenameTable", "", 0, args1())

End sub
Here I tried to open excel file (Microsoft excel 97-2003 worksheet) and just rename sheet
But If try with excel file (Microsoft excel worksheet) then error ocured, like basic runtime error

Is it cause because of filter name or anything? I also tried with different filter name of MS excel but its not working as well

anybody knows then let explain here

File attached here for reference

file ME97-2009W means Microsoft excel 97-2003 worksheet file
this file will open but
MEW nly file will not open
error showing this line

document = ThisComponent.CurrentController.Frame

thnks
Attachments
MEW only.xlsx
(12.45 KiB) Downloaded 229 times
ME97-2003W.xls
(34.5 KiB) Downloaded 243 times
Last edited by RoryOF on Mon Jun 22, 2015 1:20 pm, edited 1 time in total.
Reason: Added [code] tags. [RoryOF, Moderator]
Libre Office 5.0/ Open Office /MS Excel/ Windows 7-8
User avatar
Tiger42
Posts: 66
Joined: Tue Apr 28, 2015 12:34 pm
Location: India

Re: How to auto Open Excel File in LibreOffice Macro

Post by Tiger42 »

Problem solved out Now It will open both files Microsoft Excel Worksheet also will open

Code looks like

Code: Select all

Sub Testmacro

Dim oDoc As Object
Dim sUrl As String

Dim Prop(0) as New com.sun.star.beans.PropertyValue

sUrl = convertToURL("D:\Projects\Input.xls")

if fileExists(sUrl) then
    oDoc = stardesktop.LoadComponentFromURL(sUrl, "_blank",0, Prop())
else
    msgbox "Not found"
end if
Just remove filter name and it works

Thnks
Tiger
Libre Office 5.0/ Open Office /MS Excel/ Windows 7-8
Post Reply