Issue 40280 - createReplaceDescriptor does not work with OLE Automation
Summary: createReplaceDescriptor does not work with OLE Automation
Status: CLOSED NOT_AN_OOO_ISSUE
Alias: None
Product: App Dev
Classification: Unclassified
Component: api (show other issues)
Version: 3.3.0 or older (OOo)
Hardware: PC All
: P3 Trivial
Target Milestone: ---
Assignee: joachim.lingner
QA Contact: issues@api
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2005-01-10 12:53 UTC by finngruwierlarsen
Modified: 2013-02-24 21:07 UTC (History)
1 user (show)

See Also:
Issue Type: DEFECT
Latest Confirmation in: ---
Developer Difficulty: ---


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description finngruwierlarsen 2005-01-10 12:53:12 UTC
I want to make a VBA macro in MS Word that makes OOo make some changes in a Calc
file with the search and replace function. This should be possible using Windows
OLE Automation, but obviously it doesn't work. My code looks like this (in
Words' VBA editor):

Sub Example()

Dim oServiceManager As Object
Set oServiceManager = CreateObject("com.sun.star.ServiceManager")
Dim oDesktop as Object
Set oDesktop = oServiceManager.createInstance("com.sun.star.frame.Desktop")

'Open a new document based on a template.

Dim sTemplatePath As String
Dim oDocument As Object
Dim oSheet As Object

oTemplatePath = "file:///C:/SomePath/Example.stc"
Set oDocument = oDesktop.loadComponentFromURL(sTemplatePath, "_blank", 0, args)
Set oSheet = oDocument.Sheets(0)

If oSheet.supportsService("com.sun.star.util.createReplaceDescriptor") Then
    MsgBox "The service is supported." 'No msgbox is shown.
End If

'Modify document content
Dim oReplace As Object
Set oReplace = oSheet.createReplaceDescriptor 'Fails!
Set oReplace.SearchString = "Old text"
Set oReplace.ReplaceString = "New text"
oSheet.ReplaceAll (oReplace)

End Sub

When I make the coresponding macro in OOo itself, it works fine.
Comment 1 stephan.wunderlich 2005-01-11 11:00:58 UTC
sw->jl: as far as I know the OLE-automation is your area. I could reproduce the
behaviour with src680_m68
Comment 2 stephan.wunderlich 2005-01-11 14:00:40 UTC
The corresponding OOo Basic macro, which works without Error message would be

Dim sTemplatePath As String
Dim oDocument As Object
Dim oSheet As Object

sTemplatePath = "file:///C:/SomePath/Example.stc"
oDocument = StarDesktop.loadComponentFromURL(sTemplatePath, "_blank", 0, dimArray())
oSheet = oDocument.Sheets(0)

If oSheet.supportsService("com.sun.star.util.createReplaceDescriptor") Then
    MsgBox "The service is supported." 'No msgbox is shown.
End If

'Modify document content
Dim oReplace As Object
oReplace = oSheet.createReplaceDescriptor
oReplace.SearchString = "Old text"
oReplace.ReplaceString = "New text"
oSheet.ReplaceAll (oReplace)
Comment 3 joachim.lingner 2005-01-12 09:10:03 UTC
.
Comment 4 joachim.lingner 2005-01-12 11:51:24 UTC
StarBasic is no Visual Basic. There are subtle differences. Try this VB code:

Sub Main()
Dim oServiceManager As Object
Set oServiceManager = CreateObject("com.sun.star.ServiceManager")
Dim oDesktop As Object
Set oDesktop = oServiceManager.createInstance("com.sun.star.frame.Desktop")

'Open a new document based on a template.

Dim sTemplatePath As String
Dim oDocument As Object
Dim oSheet As Object

oTemplatePath = "file:///D:/Example.stc"
' args was not declared
Dim args() As Object
'typo: you used sTemplatePath
Set oDocument = oDesktop.loadComponentFromURL(oTemplatePath, "_blank", 0, args)
' see comments after code
Set oSheets = oDocument.getSheets
Set oSheet = oSheets.getByIndex(0)

If oSheet.supportsService("com.sun.star.util.createReplaceDescriptor") Then
    MsgBox "The service is supported." 'No msgbox is shown.
End If

'Modify document content
Dim oReplace As Object
Set oReplace = oSheet.createReplaceDescriptor 'Fails!
'Set must not be used for assigning strings
oReplace.SearchString = "Old text"
oReplace.ReplaceString = "New text"
' When calling a function and discarding the return value you must not use
brakets or you must use "call"
oSheet.ReplaceAll oReplace

End Sub

When writing VB code please always look at the idl descriptions of the
interfaces. Also read about how this idl stuff is mapped to the coresponding
Automation types at 
http://api.openoffice.org/docs/DevelopersGuide/ProfUNO/ProfUNO.htm#1+4+UNO+Language+Bindings
chapter 3.4.4 Automation Bridge

To get the collection of sheets you need to call 

XSpreadsheetDocument.getSheets

see
http://api.openoffice.org/docs/common/ref/com/sun/star/sheet/XSpreadsheetDocument.html

The returned object supports the 
com::sun::star::container::XIndexAccess on which the function getByIndex can be
called.

StarBasic knows about these relations but VB does not.  To find out what
interfaces are implemented you can use the dbg_supportedInterfaces property in
StarBasic: 

msgbox oDocument.dbg_supportedInterfaces

This will open a message box containing all supported interfaces.
Comment 5 joachim.lingner 2005-01-12 11:54:14 UTC
.
Comment 6 finngruwierlarsen 2005-01-14 14:02:36 UTC
OK. Thank you.