How to copy Text without affecting the clipboard

Creating a macro - Writing a Script - Using the API

How to copy Text without affecting the clipboard

Postby jp_akantha » Thu Oct 11, 2012 5:19 pm

i'm working with Writer documents
i've got a macro in OoBasic called from a VB6 GED aplication which move formated Header and footer Text into Floating Frames
the stuff is done via a clipboard cut/paste and work fine but ...

as it's deployed on a hundred of user pc it's really anyoning for them to get their clipboard modified each time they open a document

so does anyone could help me finding a way to copy a Document Text without using that damned clipboad at all :crazy:

(i've made a program to save and restore clipboard but anyway sometime that mess and clipboard is affected)

great thanks for further help


if it could help, here is a part of my code using cut and paste
Code: Select all   Expand viewCollapse view
Private Function AttRtf_TraiterHFPageStyle(ByRef poDoc As Object, ByRef poPageStyle As Object, ByVal piHeaderFooter As Integer) As Boolean
' transformer l'entete ou bas-page (selon piHeaderFooter)
'   référencé dans le style de page (poPageStyle) pour le document actif (poDoc)
    Dim oPageHFCursor As Object

    Dim oArgs() As Object
    Dim oDisp As Object
    Dim oFrame As Object
    Dim oHFText As Object
    Dim oTextCursor As Object
    Dim oTextFrame As Object
    Dim oTextFrameText As Object
    Dim oTextFrameTextCursor As Object
    Dim oCurrentController As Object
    Dim oViewCursor As Object
    Dim iPageNum As Integer, iPageCount As Integer
    Dim sPageStyle As String
    Dim bSuccess As Boolean
    Dim sMsg As String                                ' message erreur eventuel (cf Echec_Preparation)
    Dim sFileHF As String
    Dim bViewCursorCollapsed As Boolean         ' indique si la viewCursor reste vide parce qu'il peux pas se positionner sur ce header/footer
    Static bShowMsgOnce As Boolean
    Dim sTestInitial As String, sTest As String                     ' buffer texte de l'entete/baspage avant/apres collage ou insertion dans la frame pour tests
    Dim sTestAfterCut As String
    Dim lSaveStyleHF_Height As Long

    Set oDisp = gMainForm.moSM.CreateInstance("com.sun.star.frame.DispatchHelper")
    Set oCurrentController = poDoc.getCurrentController()
    Set oFrame = oCurrentController.GetFrame()
    Set oViewCursor = oCurrentController.getViewCursor()

    Set oPageHFCursor = oHFText.createTextCursor()
    ' Now set some formatting (reuse cursor object)
    oPageHFCursor.gotoStart (False)
    oPageHFCursor.gotoEnd (True)
    oCurrentController.Select oPageHFCursor
    oViewCursor.GotoRange oPageHFCursor, False
    bViewCursorCollapsed = oViewCursor.IsCollapsed()
   
    If bViewCursorCollapsed Then
        GoTo Finally
    End If

    ' selectionner le texte de la TextFrame
    oCurrentController.Select oPageHFCursor
    oViewCursor.GotoRange oPageHFCursor, False

    ' couper ce texte initial
   oDisp.executeDispatch oFrame, ".uno:Cut", "", 0, oArgs()

    Set oTextFrame = poDoc.CreateInstance("com.sun.star.text.TextFrame")
    With oTextFrame
        '.anchorType = com.sun.star.Text.TextContentAnchorType.AT_PARAGRAPH
        .anchortype = 0                               ' 0= au paragraphe, 2=page, 1=comme car, 4=au caractere
        .FrameIsAutomaticHeight = True
        .FrameWidthPercent = 100
        .TextWrap = 1                                 ' WrapTextMode.NONE = 0
        ' indiquer affichage de cette frame en "Arriere plan" par rapport au texte de la page
        .Opaque = False
        ' *** voir si ces lignes peuvent etre deplacées dans le 1er bloc with
        .HoriOrientPosition = 0
        .VertOrientPosition = 0
        .HoriOrientRelation = 8                       ' Zone de texte de page
        .VertOrientRelation = 7
        .HoriOrient = 3                               ' 0 = à gauche, 3 = à gauche
        If piHeaderFooter = ci_Header Then
            .VertOrient = 0                           ' 1 = top, 2=center, 3=bottom, 0=du haut
            ' positionne header à iMargeHeader 100eme de mm du haut de la page
            .VertOrientPosition = mOpenOfficeEtudeIni.iMargeHeader * 100
            .SetName ("Header : " & poPageStyle.getName())
        Else
            .VertOrient = 3                           ' 1 = top, 2=center, 3=bottom, 0=du haut
            .BottomMargin = mOpenOfficeEtudeIni.iMargeFooter * 100
            .SetName ("Footer : " & poPageStyle.getName())
        End If
        .IsFollowingTextFlow = False
    End With

    ' Inserer cette frame dans le TextCursor du bookmarkFrame
    Call oHFText.insertTextContent(oPageHFCursor, oTextFrame, False)
    ' creer un curseur sur le texte de cette frame
    Set oTextFrameText = oTextFrame.Text
    Set oTextFrameTextCursor = oTextFrameText.createTextCursor()

    ' selectionner le texte de la TextFrame
    oCurrentController.Select oTextFrameTextCursor
    'oViewCursor.GotoRange oTextFrameTextCursor, False
    oViewCursor.GotoRange oTextFrameTextCursor, True

    ' coller le texte de l'entete actuelle dans le curseur de la TextFrame
    [b]oDisp.executeDispatch oFrame, ".uno:Paste", "", 0, oArgs()[/b]

    ...
Finally:
    Set oDisp = Nothing
    Set oCurrentController = Nothing
    Set oFrame = Nothing
    Set oViewCursor = Nothing

End Function

OOO 3 on Win XP/Seven
jp_akantha
 
Posts: 3
Joined: Thu Oct 11, 2012 4:35 pm

Re: how to copy Text without affecting the clipboard

Postby FJCC » Thu Oct 11, 2012 6:22 pm

I don't understand entirely how the code works but I think the use of the clipboard can be eliminated fairly easily.
Code: Select all   Expand viewCollapse view
Private Function AttRtf_TraiterHFPageStyle(ByRef poDoc As Object, ByRef poPageStyle As Object, ByVal piHeaderFooter As Integer) As Boolean
' transformer l'entete ou bas-page (selon piHeaderFooter)
'   référencé dans le style de page (poPageStyle) pour le document actif (poDoc)
    Dim oPageHFCursor As Object

    Dim oArgs() As Object
    Dim oDisp As Object
    Dim oFrame As Object
    Dim oHFText As Object
    Dim oTextCursor As Object
    Dim oTextFrame As Object
    Dim oTextFrameText As Object
    Dim oTextFrameTextCursor As Object
    Dim oCurrentController As Object
    Dim oViewCursor As Object
    Dim iPageNum As Integer, iPageCount As Integer
    Dim sPageStyle As String
    Dim bSuccess As Boolean
    Dim sMsg As String                                ' message erreur eventuel (cf Echec_Preparation)
    Dim sFileHF As String
    Dim bViewCursorCollapsed As Boolean         ' indique si la viewCursor reste vide parce qu'il peux pas se positionner sur ce header/footer
    Static bShowMsgOnce As Boolean
    Dim sTestInitial As String, sTest As String                     ' buffer texte de l'entete/baspage avant/apres collage ou insertion dans la frame pour tests
    Dim sTestAfterCut As String
    Dim lSaveStyleHF_Height As Long

    Set oDisp = gMainForm.moSM.CreateInstance("com.sun.star.frame.DispatchHelper")
    Set oCurrentController = poDoc.getCurrentController()
    Set oFrame = oCurrentController.GetFrame()
    Set oViewCursor = oCurrentController.getViewCursor()

    Set oPageHFCursor = oHFText.createTextCursor()
    ' Now set some formatting (reuse cursor object)
    oPageHFCursor.gotoStart (False)
    oPageHFCursor.gotoEnd (True)
    oCurrentController.Select oPageHFCursor
    oViewCursor.GotoRange oPageHFCursor, False
    bViewCursorCollapsed = oViewCursor.IsCollapsed()
   
    If bViewCursorCollapsed Then
        GoTo Finally
    End If

    ' selectionner le texte de la TextFrame
    'oCurrentController.Select oPageHFCursor *****Eliminated by FJCC
    'oViewCursor.GotoRange oPageHFCursor, False *****Eliminated by FJCC
    sSavedString = oHFText.String           '***********Added by FJCC
    oHFText.String = ""           '*********Added by FJCC
    ' couper ce texte initial
   'oDisp.executeDispatch oFrame, ".uno:Cut", "", 0, oArgs()  *****Eliminated by FJCC

    Set oTextFrame = poDoc.CreateInstance("com.sun.star.text.TextFrame")
    With oTextFrame
        '.anchorType = com.sun.star.Text.TextContentAnchorType.AT_PARAGRAPH
        .anchortype = 0                               ' 0= au paragraphe, 2=page, 1=comme car, 4=au caractere
        .FrameIsAutomaticHeight = True
        .FrameWidthPercent = 100
        .TextWrap = 1                                 ' WrapTextMode.NONE = 0
        ' indiquer affichage de cette frame en "Arriere plan" par rapport au texte de la page
        .Opaque = False
        ' *** voir si ces lignes peuvent etre deplacées dans le 1er bloc with
        .HoriOrientPosition = 0
        .VertOrientPosition = 0
        .HoriOrientRelation = 8                       ' Zone de texte de page
        .VertOrientRelation = 7
        .HoriOrient = 3                               ' 0 = à gauche, 3 = à gauche
        If piHeaderFooter = ci_Header Then
            .VertOrient = 0                           ' 1 = top, 2=center, 3=bottom, 0=du haut
            ' positionne header à iMargeHeader 100eme de mm du haut de la page
            .VertOrientPosition = mOpenOfficeEtudeIni.iMargeHeader * 100
            .SetName ("Header : " & poPageStyle.getName())
        Else
            .VertOrient = 3                           ' 1 = top, 2=center, 3=bottom, 0=du haut
            .BottomMargin = mOpenOfficeEtudeIni.iMargeFooter * 100
            .SetName ("Footer : " & poPageStyle.getName())
        End If
        .IsFollowingTextFlow = False
    End With

    ' Inserer cette frame dans le TextCursor du bookmarkFrame
    Call oHFText.insertTextContent(oPageHFCursor, oTextFrame, False)
    ' creer un curseur sur le texte de cette frame
    Set oTextFrameText = oTextFrame.Text
    oTextFrameText.String = sSavedString   '**********Added by FJCC
    Set oTextFrameTextCursor = oTextFrameText.createTextCursor()    *****Eliminated by FJCC

    ' selectionner le texte de la TextFrame
    'oCurrentController.Select oTextFrameTextCursor        *****Eliminated by FJCC
    'oViewCursor.GotoRange oTextFrameTextCursor, False
    'oViewCursor.GotoRange oTextFrameTextCursor, True   *****Eliminated by FJCC

    ' coller le texte de l'entete actuelle dans le curseur de la TextFrame
    '[b]oDisp.executeDispatch oFrame, ".uno:Paste", "", 0, oArgs()[/b]   *****Eliminated by FJCC

    ...
Finally:
    Set oDisp = Nothing
    Set oCurrentController = Nothing
    Set oFrame = Nothing
    Set oViewCursor = Nothing

End Function


I don't program in VB, so my syntax may be wrong. Also, I don't see where oHFText gets Set, so I suspect some code is missing unless I'm simply overlooking it. It seems further simplification of the code is possible but I hesitate to make too many changes on something I don't completely understand.
Windows 10 and Linux Mint, since 2017
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
FJCC
Moderator
 
Posts: 8165
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: How to copy Text without affecting the clipboard

Postby jp_akantha » Fri Oct 12, 2012 9:27 am

thanks for your sensible suggestion
i think you well understand and it will work to carry the text but not the layout

for example a header could contain a first line centered with name and title in Arial bold 24 then address centered in Arial bold 12 then phone number left aligned and sometimes sub text frames and so on..

the great question is how to copy Text with all its layout formats
as we copy .String property
is there a .Format property which could be copied the same ?

many thanks

thanks
OOO 3 on Win XP/Seven
jp_akantha
 
Posts: 3
Joined: Thu Oct 11, 2012 4:35 pm

Re: How to copy Text without affecting the clipboard

Postby Villeroy » Fri Oct 12, 2012 10:17 am

Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
User avatar
Villeroy
Volunteer
 
Posts: 29876
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: How to copy Text without affecting the clipboard

Postby FJCC » Sat Oct 13, 2012 8:44 pm

Following Villeroy's suggestion and doing a little searching I found some code by Hanya here. I modified it slightly to copy selected text in a writer document to the end of the document. The text I selected contained parts of two paragraphs, each with a different font and between them was a Heading 2 paragraph. One strange thing about the copied text is that the Heading 2 paragraph had the style heading 2 (which didn't exist elsewhere in the document) and the indentation did not match the original of that paragraph. Other than that all the formatting was preserved.
Code: Select all   Expand viewCollapse view
'Hanya code from http://www.oooforum.org/forum/viewtopic.phtml?t=71486
Dim nData() As Integer
  Dim aMType As Object
  Dim sType As String
  sType = "text/richtext"

  ' selected range as Transferable
  oTransferable = ThisComponent.getCurrentController().getTransferable()

  If IsNull(oTransferable) Then Exit Sub

  ' get specific DataFlavor
  fs = oTransferable.getTransferDataFlavors()
  For i = 0 To UBound(fs) step 1
    If fs(i).MimeType = sType Then
      aMType = fs(i)
      Exit For
    End If
  Next

  If IsNull(aMType) Then Exit Sub

  ' nData taken from oTransferable as bytes
  If oTransferable.isDataFlavorSupported(aMType) Then
    nData = oTransferable.getTransferData(aMType)
  Else
    Exit Sub
  End If

    ' write data to a temp file
    oTemp = CreateUnoService("com.sun.star.io.TempFile")
    oOut = oTemp.getOutputStream()
    oOut.writeBytes(nData)
    oOut.flush()
   
    oIn = oTemp.getInputStream()
   
    ' insert data to a new writer document from the InputStream
    'oNewDoc = StarDesktop.loadComponentFromURL( _
     '   "private:factory/swriter", "_blank", 0, Array() )
   
    'oController = oNewDoc.getCurrentController()
    'oViewCursor = oController.getViewCursor()
    'oText = oNewDoc.getText()
   oText = ThisComponent.Text
    'oCursor = oText.createTextCursorByRange(oViewCursor.getEnd())
    oCursor = oText.createTextCursor()
    oCursor.gotoEnd(False)
    Dim args(1) As New com.sun.star.beans.PropertyValue
    args(0).Name = "InputStream"
    args(0).Value = oIn
    args(1).Name = "FilterName"
    args(1).Value = "writer_Rich_Text_Format"
   
    oCursor.insertDocumentFromURL("",args)
Windows 10 and Linux Mint, since 2017
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
FJCC
Moderator
 
Posts: 8165
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: How to copy Text without affecting the clipboard

Postby jp_akantha » Mon Oct 15, 2012 4:00 pm

thanks for contributions, i try that ...
OOO 3 on Win XP/Seven
jp_akantha
 
Posts: 3
Joined: Thu Oct 11, 2012 4:35 pm


Return to Macros and UNO API

Who is online

Users browsing this forum: No registered users and 4 guests