| View previous topic :: View next topic |
| Author |
Message |
OOdenis Newbie

Joined: 08 Sep 2009 Posts: 3
|
Posted: Tue Sep 08, 2009 1:06 pm Post subject: Search, copy and paste in new doc Macro in Writer |
|
|
I have a script that partially works.
I can search and find what I am looking for
I Can paste (Pastes whatever is in the clipboard)
I can't copy (gives errors)
I tried to use clipboard Clipboard manipulation Routines that I found but it seems to hate textcursors because I can't figure out how to tell the clipboard to copy what it found.
Any help would be appreciated!
OOdenis
| Code: | REM ***** BASIC *****
Sub Main
'Xray ThisComponent
'Dim oDoc As Object
'oThisDoc = ThisComponent
'skip TOC (HAVE NO Clue)
'Need to search for something like "[EndOfTOC][NewPage]" codes in document
'TBD
givenWord="Basic Concepts"
givenWord2="Not-so-Basic Concepts"
call SearchNcopyToClipB(givenWord, givenWord2)
'Open new document
Dim oDoc2 As Object
'Dim oDocProperties(0) as new com.sun.star.beans.PropertyValue
oDoc2 = StarDesktop.loadComponentFromURL( "private:factory/swriter", "_blank", 0, Array() ) '<<---This works!!!
oCursor2 = oDoc2.getText().createTextCursor()
' Now PASTE into Doc2.
ClipboardPaste( oDoc2 ) '<<---This works!!!
'Export Doc2 to HTML
'File->Send -> Create HTML doc (Default is Hi)
'TBD
'edit P{margin bottom: 0.08in; Margin top: 0cm} or figure out how to use CSS file to prevent it
'TBD
'Discard new doc2
'TBD
End Sub
Sub SearchNcopyToClipB(givenWord, givenWord2)
'http://www.oooforum.org/forum/viewtopic.phtml?t=86288&highlight=search
'Selecting text between 2 given words (across Paragraph boundary!) and copy to ClipBoard
oThisDoc = ThisComponent
'SnC2CB = oThisDoc.CreateReplaceDescriptor 'we are not replacing, might cause that "GetDocumentFrame called with incorrect parameter." error
SnC2CB = oThisDoc.CreateSearchDescriptor
SnC2CB.searchRegularExpression = true
'SnC2CB.setSearchString("givenWord1")
SnC2CB.setSearchString(givenWord)
W1 = oThisDoc.FindFirst(SnC2CB) 'W1 is a SwxTextCursor
If Not isNull(W1) then
SnC2CB.setSearchString("") 'Try to skip TOC in doc
'SnC2CB.setSearchString("givenWord2")
SnC2CB.setSearchString(givenWord2)
W2 = oThisDoc.FindNext(W1.End,SnC2CB)
If Not isNull(W2) then
W1.collapseToEnd
W1.gotoRange(W2.Start,true)
'W1.String is what got selected
MsgBox(W1.String) '(This works!!!!)
'Now COPY selection to the clipboard (does not work!)
' ClipboardCopy(W1.String) 'NFG. Need to feed it an object
' ClipboardCopy(W1.text) 'NFG. Need to feed it an object
' ClipboardCopy(W1) 'NFG. Need to feed it an object
' SelectAll( W1.TextFrame )
ClipboardCopy(W1.TextFrame) 'NFG. Need to feed it an object
Else Print SnC2CB.SearchString & " - Not Found!"
EndIf
Else Print SnC2CB.SearchString & " - Not Found!"
EndIF
End Sub
' Routines from
' Making the Dispatcher easier to use
' http://www.oooforum.org/forum/viewtopic.php?t=5058
'############################################################
' Clipboard manipulation
'############################################################
Sub ClipboardPaste( oDocumentFrame )
DocumentDispatch( oDocumentFrame, ".uno:Paste" )
End Sub
Sub ClipboardCopy( oDocumentFrame )
DocumentDispatch( oDocumentFrame, ".uno:Copy" )
End Sub
Sub ClipboardCut( oDocumentFrame )
DocumentDispatch( oDocumentFrame, ".uno:Cut" )
End Sub
Sub SelectAll( oDocumentFrame )
DocumentDispatch( oDocumentFrame, ".uno:SelectAll" )
' DocumentDispatch( oDocumentFrame, "slot:5723" )
End Sub
'----------
' An easy to use Dispatch on an office document.
' Arguments are similar to the args for the com.sun.star.frame.XDispatchHelper
' interface of com.sun.star.frame.DispatchHelper.
' What makes this so easy to use are two things:
' 1. The fact that the oDocumentFrame parameter can actually accept
' either the document model or one of its controllers.
' 2. The optional parameters.
' For an example of how simple this routine is to use, see
' routines such as ClipboardCopy().
'
' Parameters:
' oDocumentFrame - An office document frame.
' But wait! It could be the document controller
' or the document model. This routine will find
' the document frame from either of these.
' cURL - The dispatch URL.
' Optional:
' cTargetFrameName - Defaults to blank.
' nSearchFlags - Defaults to zero.
' aDispatchArgs - Defaults an an empty sequence.
'
Sub DocumentDispatch( ByVal oDocumentFrame As Object, ByVal cURL As String, Optional cTargetFrameName, Optional nSearchFlags, Optional aDispatchArgs )
' If they gave us the wrong parameter...
If Not HasUnoInterfaces( oDocumentFrame, "com.sun.star.frame.XFrame" ) Then
' Be sure that we've got the document frame.
' Someone might have passed us the document model or one of
' its controller's.
oDocumentFrame = GetDocumentFrame( oDocumentFrame )
EndIf
If IsMissing( cTargetFrameName ) Then
cTargetFrameName = ""
EndIf
If IsMissing( nSearchFlags ) Then
nSearchFlags = 0
EndIf
If IsMissing( aDispatchArgs ) Then
aDispatchArgs = Array()
EndIf
oDispatchHelper = createUnoService( "com.sun.star.frame.DispatchHelper" )
oDispatchHelper.executeDispatch( oDocumentFrame, cURL, cTargetFrameName, nSearchFlags, aDispatchArgs )
End Sub
'############################################################
' API navigation convenience
'http://www.oooforum.org/forum/viewtopic.phtml?t=5057
'############################################################
'----------
' This will always return the document's controller.
' Pass in any one of...
' * the document's model (subclass of com.sun.star.document.OfficeDocument)
' * the document's controller
' * the document's frame
Function GetDocumentController( oDoc As Object ) As Object
Dim oCtrl As Object
' If the caller gave us the document model...
If oDoc.supportsService( "com.sun.star.document.OfficeDocument" ) Then
' ...then get the controller from that.
oCtrl = oDoc.getCurrentController()
' If the caller gave us a document controller...
ElseIf HasUnoInterfaces( oDoc, "com.sun.star.frame.XController" ) Then
' ...thanks! That's just what we wanted!
oCtrl = oDoc
' If the caller gave us the document frame...
ElseIf HasUnoInterfaces( oDoc, "com.sun.star.frame.XFrame" ) Then
oFrame = oDoc
' ...then get the controller from the frame.
oCtrl = oFrame.getController()
Else
' The caller did not give us what we expected!
MsgBox( "GetDocController called with incorrect parameter." )
EndIf
GetDocumentController() = oCtrl
End Function
'----------
' This will always return the document's frame.
' Pass in any one of...
' * the document's model (subclass of com.sun.star.document.OfficeDocument)
' * the document's controller
' * the document's frame
Function GetDocumentFrame( oDoc As Object ) As Object
Dim oFrame As Object
' If the caller gave us the document model...
If oDoc.supportsService( "com.sun.star.document.OfficeDocument" ) Then
' ...then get the controller from that.
oCtrl = oDoc.getCurrentController()
' ...then get the frame from the controller.
oFrame = oCtrl.getFrame()
' If the caller gave us a document controller...
ElseIf HasUnoInterfaces( oDoc, "com.sun.star.frame.XController" ) Then
oCtrl = oDoc
' ...then get the frame from the controller.
oFrame = oCtrl.getFrame()
' If the caller gave us the document frame...
ElseIf HasUnoInterfaces( oDoc, "com.sun.star.frame.XFrame" ) Then
' ...thanks! That's just what we wanted!
oFrame = oDoc
Else
' The caller did not give us what we expected!
MsgBox( "GetDocumentFrame called with incorrect parameter." )
EndIf
GetDocumentFrame() = oFrame
End Function
'----------
' This will always return the document's model.
' Pass in any one of...
' * the document's model (subclass of com.sun.star.document.OfficeDocument)
' * the document's controller
' * the document's frame
Function GetDocumentModel( oDoc As Object ) As Object
Dim oDocModel As Object
' If the caller gave us the document model...
If oDoc.supportsService( "com.sun.star.document.OfficeDocument" ) Then
' ...thanks! That's just what we wanted!
oDocModel = oDoc
' If the caller gave us a document controller...
ElseIf HasUnoInterfaces( oDoc, "com.sun.star.frame.XController" ) Then
oCtrl = oDoc
' ...then get the model from the controller.
oDocModel = oCtrl.getModel()
' If the caller gave us the document frame...
ElseIf HasUnoInterfaces( oDoc, "com.sun.star.frame.XFrame" ) Then
oFrame = oDoc
' ...then get the controller from the frame.
oCtrl = oFrame.getController()
' ...then get the model from the controller.
oDocModel = oCtrl.getModel()
Else
' The caller did not give us what we expected!
MsgBox( "GetDocumentModel called with incorrect parameter." )
EndIf
GetDocumentModel() = oDocModel
End Function
|
|
|
| Back to top |
|
 |
JohnV Administrator

Joined: 07 Mar 2003 Posts: 8979 Location: Lexinton, Kentucky, USA
|
Posted: Tue Sep 08, 2009 4:52 pm Post subject: |
|
|
Copy works with what is selected by the View Cursor so when you find what you want with a text cursor do:
| Code: | ViewCursor = ThisComponent.CurrentController.getViewCursor
ViewCursor.gotoRange(TextCursor,false) |
|
|
| Back to top |
|
 |
OOdenis Newbie

Joined: 08 Sep 2009 Posts: 3
|
Posted: Wed Sep 09, 2009 7:07 am Post subject: |
|
|
I changed your code to work with what I got because it didn't know what "TextCursor" was (I know the variable names suck but that the way I found it):
ViewCursor = ThisComponent.CurrentController.getViewCursor
ViewCursor.gotoRange(W1,false)
I get the same error as before:
"Object variable not set" on
Sub DocumentDispatch( ByVal oDocumentFrame As Object, ByVal cURL As String, Optional cTargetFrameName, Optional nSearchFlags, Optional aDispatchArgs )
BTW: Is this whole Clipboard simplification stuff still being recommended? |
|
| 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
|