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

Joined: 24 May 2007 Posts: 6
|
Posted: Thu May 24, 2007 2:32 pm Post subject: Visual Studio 2005 (VB .net) and OOo Calc automation... |
|
|
Hello,
I'm having a lot of trouble finding information on writing code to get VB.net to control Calc. (open the program, create / size / fill cells, etc...) Basically my program just takes text it gathers from textboxes and prints everything to a spreadsheet. (currently working with MS Excel, but our customers need a more cost effective solution, hopefully openoffice.org)
I have tried simply copying and pasting code from some of the help files posted around here but I always get an error.
For example I tried this:
Sub firstOOoProc()
Dim oSM As Object 'Root object for accessing OpenOffice from VB
Dim oDesk, oDoc As Object 'First objects from the API
Dim arg() 'Ignore it for the moment !
'Instanciate OOo : this line is mandatory with VB for OOo API
oSM = CreateObject("com.sun.star.ServiceManager")
'Create the first and most important service
oDesk = oSM.createInstance("com.sun.star.frame.Desktop")
'Create a new doc
oDoc = oDesk.loadComponentFromURL("private:factory/swriter", "_blank", 0, arg)
'Close the doc
oDoc.Close(True)
oDoc = Nothing
'Open an existing doc (pay attention to the syntax for first argument)
oDoc = oDesk.loadComponentFromURL("file:///c:/dev/ooo/test.doc", "_blank", 0, arg)
'Save the doc
Call oDoc.storeToURL("file:///c:/dev/ooo/test2.sxw", arg)
'Close the doc
oDoc.Close(True)
oDoc = Nothing
End Sub
When I try to run it (tab through the code) I get this error:
"Type mismatch. (Exception from HRESULT: 0x80020005 (DISP_E_TYPEMISMATCH))"
or in other words: "The exception that is thrown when an unrecognized HRESULT is returned from a COM method call. "
I'm not sure what this means, but Visual Studio acts like the code is simply invalid. Is this because I need to "Add Reference" for openoffice, or "Import" like you have when connecting to MS Excel?
ie. "Imports Microsoft.Office.Interop.Excel"
Anyways... I could really use some help. Thanks! |
|
| Back to top |
|
 |
oonewb General User

Joined: 24 May 2007 Posts: 6
|
Posted: Fri May 25, 2007 7:19 am Post subject: |
|
|
Well... I think I've narrowed it down a bit. This is the line that seems to be pulling this error:
oDoc = oDesk.loadComponentFromURL("private:factory/scalc", "_blank", 0, dummy)
So does anyone know what I'm missing here? Would it have anything to do with the fact that I'm running Vista?
EDIT/Update: Tried code on XP, same prob. I also tried Importing the "SO_ActiveXLib" and adding the reference, which did not work either.
Is this even possible? Does anyone have any ideas at all?! |
|
| Back to top |
|
 |
oonewb General User

Joined: 24 May 2007 Posts: 6
|
Posted: Fri May 25, 2007 1:58 pm Post subject: |
|
|
Okay... so here's the deal.
I am using Visual Studio 2005 with VB.net.
The problem is this code is written for VB6, so I am getting this error because I'm trying to run VB6 code in VB.net.
So... now I ask... Does anyone know how to automate OpenOffice with VB.net?? |
|
| Back to top |
|
 |
oonewb General User

Joined: 24 May 2007 Posts: 6
|
Posted: Fri May 25, 2007 3:04 pm Post subject: |
|
|
FIXED!!
This code now runs on VB.net. (opens a new calc sheet)
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim oSM As Object
Dim oDesk As Object
Dim oDoc As Object
oSM = CreateObject("com.sun.star.ServiceManager")
oDesk = oSM.createInstance("com.sun.star.frame.Desktop")
Dim aNoArgs(-1)
oDoc = oDesk.loadComponentFromURL("private:factory/scalc", "_blank", 0, aNoArgs)
End Sub
End Class |
|
| Back to top |
|
 |
pitonyak Administrator


Joined: 09 Mar 2004 Posts: 3618 Location: Columbus, Ohio, USA
|
Posted: Fri May 25, 2007 7:09 pm Post subject: |
|
|
Very impressive, you solved your own question! Very nice! Thanks for posting the solution.
Many people here do not have access to Visual studio, especially at home. _________________ --
Andrew Pitonyak
http://www.pitonyak.org/oo.php |
|
| Back to top |
|
 |
FrankO Newbie


Joined: 13 Jun 2007 Posts: 4
|
Posted: Wed Jun 13, 2007 5:36 am Post subject: |
|
|
Well done oonewb, you are the only person i have come across that has posted anything worthwhile on this issue. Thanks!
I have been using Excel but now switched to OOo. In my code i currently have
| Code: |
Dim objExcelApp As Excel.Application
Dim objExcelWBook As Excel.Workbook
Dim objExcelWSheet As Excel.Worksheet
Dim range As Excel.Range
|
How does this change for OOo? |
|
| Back to top |
|
 |
Azoto Newbie

Joined: 27 Jun 2007 Posts: 1
|
Posted: Wed Jun 27, 2007 6:07 am Post subject: |
|
|
| oonewb you're GREAT!! I spent my whole day trying to find a solution to the same problem you had. Till I found your post. Thank you! |
|
| Back to top |
|
 |
martonx Newbie

Joined: 13 May 2008 Posts: 1
|
Posted: Wed May 14, 2008 10:02 am Post subject: |
|
|
Thank you oonewb!
And how is going on? How can i add range, values etc...
Is anywhere some useful documentation for OpenOffice automation? |
|
| Back to top |
|
 |
hffm Power User

Joined: 22 Jul 2005 Posts: 52 Location: GERARDMER FRANCE
|
Posted: Fri May 23, 2008 9:19 pm Post subject: |
|
|
hello
I posted this solution a bit time ago...
see:http://www.oooforum.org/forum/viewtopic.phtml?t=44760
In facts if you're working with vb net via com automation it's the same as with vb6, the differences are not from oo part but from Vb parts: with vb net you have to instanciate objects and so on, so if you're trying to call a null arguments (aNoArgs) you have to delcare and to initialize it (-1).
Sorry for my english |
|
| Back to top |
|
 |
therealtoecutter General User

Joined: 01 Aug 2009 Posts: 17 Location: Brisbane, Australia
|
Posted: Mon Aug 03, 2009 1:46 pm Post subject: |
|
|
| pitonyak wrote: | Very impressive, you solved your own question! Very nice! Thanks for posting the solution.
Many people here do not have access to Visual studio, especially at home. |
Visual Basic 2008 Express is free to download along with the other express versions. Has been ever since vb 2005 was released.
I have been trying to get vb 2005 code to open a existing .ods file and write to it without any luck
| Code: |
ConnectOpenOffice()
Dim myDoc As Object, firstSheet As Object
Dim fields As Object, unoWrap As Object, sortDx As Object, row As Integer, col As Integer, i As Integer
'myDoc = StarDesktop.loadComponentFromURL("private:factory/scalc", My.Computer.FileSystem.SpecialDirectories.MyDocuments & "\MetalRoofingExpress\Template.ods", 0, dummyArray)
Dim myCalcFile As String = "file:///" & My.Computer.FileSystem.SpecialDirectories.MyDocuments & "\MetalRoofingExpress\Template.ods"
Dim args(-1)
myDoc = StarDesktop.loadComponentFromURL(myCalcFile, "_blank", 0, args)
firstSheet = myDoc.Sheets.getByIndex(0)
|
The above code opens the template.ods file but nothing is written
Maybe somone has achived this since this thread started?
regards
toe |
|
| Back to top |
|
 |
therealtoecutter General User

Joined: 01 Aug 2009 Posts: 17 Location: Brisbane, Australia
|
Posted: Mon Aug 03, 2009 9:39 pm Post subject: |
|
|
got it to work finally by conveting the file address to a url
| Code: |
sUrl = convertToURL("file:///" & My.Computer.FileSystem.SpecialDirectories.MyDocuments & "\MetalRoofingExpress\Template.ods")
myDoc = StarDesktop.loadComponentFromURL(sUrl, "_blank", 0, aNoArgs)
|
| Code: |
Function convertToURL(ByVal winAddr As String) As String
' équivalent de la fonction OOoBasic
' equivalent to the OOoBasic function
Dim slow As String, prefix As String, UTF8Addr As String
Dim x As Integer, sv As Object
Dim URLprefix() As String = {"file:///", "ftp://", "news:", "http://", "mailto:", "macro:", "private:"}
slow = LCase(winAddr)
prefix = ""
For x = 0 To UBound(URLprefix)
If InStr(1, slow, URLprefix(x), vbBinaryCompare) = 1 Then
winAddr = Mid(winAddr, Len(URLprefix(x)) + 1)
If x > 0 Then prefix = URLprefix(x) ' prefix file:/// is useless
Exit For
End If
Next
If (Len(prefix) = 0) And (InStr(1, slow, "@", vbBinaryCompare) > 0) Then
convertToURL = "mailto:" & winAddr
Else
sv = CreateUnoService("com.sun.star.ucb.FileContentProvider")
UTF8Addr = sv.getFileURLFromSystemPath("", winAddr)
If Len(UTF8Addr) = 0 Then Err.Raise(vbObjectError + OOoErrorN, "OpenOffice", OOo_convertToURLKO)
convertToURL = prefix & UTF8Addr
End If
End Function
|
|
|
| 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
|