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

please help: how insert a .sxw in an open .sxw in vb

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





PostPosted: Tue Nov 18, 2003 3:20 am    Post subject: please help: how insert a .sxw in an open .sxw in vb Reply with quote

who can help me transform the following ooo basic code (or important parts) in vb basic?


Code:

Sub Main


 ' Get the Service Manager object -- from whence everything else comes.
    ' The biggest first difference between programming languages accessing OOo
    '  is often in how you initially obtain the ServiceManager.
    Dim oServiceManager As Object, odesktop As Object
    Dim sUrl As String
    Dim oDocument As Object ' Verweis auf OO-Dokument
    Dim mNoArgs()
   
   
    Dim oCalcDoc As Object, osheet
   
   
    Set oServiceManager = CreateObject ("com.sun.star.ServiceManager")
    ' Get the Desktop object.
    Set odesktop = oServiceManager.createInstance("com.sun.star.frame.Desktop")
    sUrl = "file:///f:/testdok.sxw"
    Set oDocument = odesktop.LoadComponentFromURL(sUrl, "_blank", 0, mNoArgs())
   
   
   
   
    Exit Sub
End Sub



sub test1
rem ----------------------------------------------------------------------
rem define variables
dim document   as object
dim dispatcher as object
rem ----------------------------------------------------------------------
rem get access to the document
document   = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")

rem ----------------------------------------------------------------------
rem dispatcher.executeDispatch(document, ".uno:Open", "", 0, Array())

rem ----------------------------------------------------------------------
dim args2(1) as new com.sun.star.beans.PropertyValue
args2(0).Name = "Name"
args2(0).Value = "file:///F:/OOTest/Text2.sxw"
args2(1).Name = "Filter"
args2(1).Value = "StarOffice XML (Writer)"

dispatcher.executeDispatch(document, ".uno:InsertDoc", "", 0, args2())

rem ----------------------------------------------------------------------
dim args3(1) as new com.sun.star.beans.PropertyValue
args3(0).Name = "URL"
args3(0).Value = "file:///F:/OOTest/Text3.sxw"
args3(1).Name = "FilterName"
args3(1).Value = "StarOffice XML (Writer)"

dispatcher.executeDispatch(document, ".uno:SaveAs", "", 0, args3())


end sub


thx 4 ur hlp Embarassed
Back to top
NaT
General User
General User


Joined: 18 Nov 2003
Posts: 9
Location: Germany / Dresden

PostPosted: Tue Nov 18, 2003 3:27 am    Post subject: Reply with quote

.... and hi again Very Happy
Back to top
View user's profile Send private message Visit poster's website
DannyB
Moderator
Moderator


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

PostPosted: Tue Nov 18, 2003 8:03 am    Post subject: Reply with quote

The "Sub Main" already looks like it is in VB.

No human mind could possibly have stooped to the depths necessary to conceive of something like the "Sub Test1", therefore, I conclude it was generated by the macro recorder. (That sounds like the thinking of a machine to me.--Morpheus)

I'm not using VB, so I can't test what I'm about to write.

First, let's look at this fragment...
Code:
document   = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")


Let's conjecture that in VB we would something like this. I'm going to take advantage of the variables you've already got in "Sub Main".
Code:
Set oDocFrame = oDocument.getCurrentController().getFrame()
Set oDispatcher = oServiceManager.createInstance( "com.sun.star.frame.DispatchHelper" )

Another possibiility might be to write it in two steps...
Code:
Set oDocView = oDocument.getCurrentController()
Set oDocFrame = oDocView.getFrame()
Set oDispatcher = oServiceManager.createInstance( "com.sun.star.frame.DispatchHelper" )


Now we've got two variables oDocFrame, and oDispatcher. oDispatcher is the dispatcher, and we will need oDocFrame as a parameter to executeDispatch().

New let's look at this....
Code:
dim args2(1) as new com.sun.star.beans.PropertyValue
args2(0).Name = "Name"
args2(0).Value = "file:///F:/OOTest/Text2.sxw"
args2(1).Name = "Filter"
args2(1).Value = "StarOffice XML (Writer)"

dispatcher.executeDispatch(document, ".uno:InsertDoc", "", 0, args2())


Let's rewrite like this.....
Code:
Dim aArgs1( 1 )
aArgs1( 0 ) = createPropertyValue( "Name", "file:///F:/OOTest/Text2.sxw" )
aArgs1( 1 ) = createPropertyValue( "Filter", "StarOffice XML (Writer)" )


I'm not so sure with VB that the Dim doesn't need to be something like...
Code:
Dim aArgs1( 1 ) As Object

or somesuch? I'll leave that to you since I presume you know VB better than I do, and especially that you have access to it.

Above, I used a new function we'll call "createPropertyValue". See these past posts to learn how that function is created. This is the real trick to rewriting this code to VB.

http://www.oooforum.org/forum/viewtopic.php?t=3453
http://www.oooforum.org/forum/viewtopic.php?p=12231
http://www.oooforum.org/forum/viewtopic.php?p=12259#12259
http://www.oooforum.org/forum/viewtopic.php?t=3510

I believe that Gibson creates a function called OOoNamedValue, which c reates a com.sun.star.beans.NamedValue which can be used pretty much anywhere a com.sun.star.beans.PropertyValue can be used. I posted about how you can use Bridge_GetStruct which is a feature of the OLE-UNO bridge that allows creating an instance of any struct, such as either
com.sun.star.beans.NamedValue
or
com.sun.star.beans.PropertyValue

All examples in the developer's guide tend to use PropertyValue, so that is what I personally stick with. Gibson's createStruct() which is based upon use of the CoreReflection is a technique I used to use in Microsoft Visual FoxPro. Now I use the Bridge_GetStruct because it is much simpler. Gibson's createStruct() function can be replaced by Bridge_GetStruct, which is a psuedo-method that can be called on any uno object as explained in one of my posts linked above.

Once you've got the trick down to create PropertyValue's, you're crossed over the mountain and it's all downhill.

Code:
dispatcher.executeDispatch(document, ".uno:InsertDoc", "", 0, args2())


is trivially rewritten (using earlier variable names)....

Code:
Call oDispatcher.executeDispatch( oDocFrame, ".uno.InsertDoc", "", 0, aArgs1() )


The other call to the dispatcher follows the same pattern.

Good luck.
_________________
Want to make OOo Drawings like the colored flower design to the left?
Back to top
View user's profile Send private message
NaT
General User
General User


Joined: 18 Nov 2003
Posts: 9
Location: Germany / Dresden

PostPosted: Thu Nov 20, 2003 2:49 am    Post subject: Reply with quote

thx danny for ur hard work, i saw ur answers at other posts and hoped u look at mine Wink

ur right, my posted code is from "a machine" Smile because, i dont found any good description of OOo objects, or the difficult important things are not in german Wink
so i create the code with the macro rec. and try 2 transform this code to vb.

i use the "createPropertyValue"- function from gibson in things like mailmerge, or search and replace.

that leads me to my next question: at mailmerge i need cells with more than one line for examble: adress field or so
the mailmerge datafields comes from a datasource which is a text file (.txt).

my solution: i create the datasource .txt without linefeed instead i used "$" for it.
examble from txt: "Anrede";"Adresse"
"Sehr geehrter Herr Meier";"Herr$Max Meier$Tolle Straße 50$01187 Dresden"

now i create the documents via mailmerge, and save it as word .doc (!!) the reason: the docfile dont save the information of the openoffice mailmerge fields.

then close ooo, open .doc again, search and replace (replace $ with linefeed).

but THIS long way cant be the ideal solution!! (i dont want to save an ooo sxw to a word doc) a additional question to mailmerge: how can i avoide that the output document contained numbers? (in my code: "ErstelltesDok0")

thx for ur hlp

ps: sry 4 my english :>


my vb code:

Code:


    ' --- Mailmerge ---
    Set objmailMerge = oServiceManager.createInstance("com.sun.star.text.MailMerge")
       Dim args(0 To 6)
       Set args(1) = OOoNamedValue("DocumentURL", "file:///f:/OOTest/Vorlage.sxw")
       Set args(0) = OOoNamedValue("DataSourceName", "TestDQuelle")
       Set args(2) = OOoNamedValue("CommandType", 0)
       Set args(3) = OOoNamedValue("Command", "test")
       Set args(4) = OOoNamedValue("OutputType", 2)
       Set args(5) = OOoNamedValue("OutputURL", "file:///f:/OOTest/MMDok/")
       Set args(6) = OOoNamedValue("FileNamePrefix", "ErstelltesDok")
    objmailMerge.Execute args()


   
    ' --- save .sxw in .doc ---
    sURL = "file:///f:/OOTest/MMDok/ErstelltesDok0.sxw"
    Set oDocument = odesktop.LoadComponentFromURL(sURL, "_blank", 0, mNoArgs())
   
    Set aSaveArgs(0) = OOoNamedValue("FilterName", "MS WinWord 6.0")
    oDocument.storeToURL "file:///f:/OOTest/MMDok/ErstelltesDok0.doc",     Array(MakePropertyValue("FilterName", "MS WinWord 6.0"))
    oDocument.Close (0)



    ' --- open .doc ---
    sURL = "file:///f:/OOTest/MMDok/ErstelltesDok0.doc"
    Set oDocument = odesktop.LoadComponentFromURL(sURL, "_blank", 0, mNoArgs())
   

 
    ' --- search and replace ---
    Set oSearch = oDocument.createReplaceDescriptor
    oSearch.searchString = "$"
    ' Suchwort = $
    oSearch.replaceString = vbLf
    ' Wort durch welches ersetzt wird = "neue Zeile" (new line)
    oDocument.replaceall oSearch
    ' Alle gefundenen Wörter werden ersetzt - all words replaced

   
    ' --- save .doc in .sxw ; del .doc ... ---
   
Back to top
View user's profile Send private message Visit poster's website
DannyB
Moderator
Moderator


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

PostPosted: Thu Nov 20, 2003 3:35 pm    Post subject: Reply with quote

Hey, if it works, that's hard to argue with. Smile Getting something to work correctly is more significant than something that is elegant but doesn't work.

Unfortunantly, I don't know anything about mail merge.

Is there any reason you can't just put CHR(13) into your text instead of dollar sign? Or what if instead of a text file, you use some other data source, such as a spreadsheet with multiline values in some of the cells, such as for Address?

Or combine the two ides. First open text file into a spreadsheet. Then iterate over the cells in the Address column. Grab each cell as string value, replace $ with CHR(13) then put back into the cell. Then repeat for every other address cell. Now use spreadsheet as your data source.

If the data must come from text with $ as the delimiter, then ultimately no matter what solution you use, you will end up searching for $ and replacing with a line break. Either before the mail merge, or after.

If you end up with the solution you've got (even if the $ substitution with line break happens prior to merge) then maybe you should use a more unique delimiter than $. What if line breaks were indicated by something like a three character delimiter, like |+| or |=| or something fairly unlikely to ever appear in the text? Even the text <BR> appearing in the text could indicate a line break.
_________________
Want to make OOo Drawings like the colored flower design to the left?
Back to top
View user's profile Send private message
NaT
General User
General User


Joined: 18 Nov 2003
Posts: 9
Location: Germany / Dresden

PostPosted: Fri Nov 21, 2003 1:43 am    Post subject: Reply with quote

danny...

i need 1 h to find the difference between this wrong code

Call oDispatcher.executeDispatch( oDocFrame, ".uno.InsertDoc", "", 0, aArgs1() )

and this right code

Call oDispatcher.executeDispatch( oDocFrame, ".uno:InsertDoc", "", 0, aArgs1() )

but anyway -still thx 4 ur hlp...now it works. :p

i will try ur mailmerge ideas but chr(13) dont work if u use this u get strage symbol at the screen, chr(10) is a better solution (like in unix), i think with mailmerge word >>> ooo, i hope of v1.2.0 Smile

thx and have a nice weekend Wink

cya NaT

ps: i tried (?bad english?) ur ooo power draw tools ... not bad, i painted this kind of flowers with a pen as i was a child.
Back to top
View user's profile Send private message Visit poster's website
NaT
General User
General User


Joined: 18 Nov 2003
Posts: 9
Location: Germany / Dresden

PostPosted: Fri Nov 21, 2003 1:51 am    Post subject: Reply with quote

if anyone like to see the complete code of my ooo test program in vb, ask me via pm
i dont want 2 spam :>
Back to top
View user's profile Send private message Visit poster's website
DannyB
Moderator
Moderator


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

PostPosted: Fri Nov 21, 2003 9:43 am    Post subject: Reply with quote

NaT wrote:
i need 1 h to find the difference between this wrong code


Ooops. I can't believe I made such an error. Substitute colon for period.

I tend to type so incredibly fast and furious that I later often notice little typos that I made. This is bad in code.

Hope you like the draw power tools. Sometime there will be a new version that doesn't use dialog boxes.
_________________
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