Solved Paste plain text into a rich text field (1 Viewer)

zelarra821

Registered User.
Local time
Today, 22:31
Joined
Jan 14, 2019
Messages
813
Ok, I just managed to copy it to the end of the text box as plain text, adding a paragraph break.

Now my question is how to add this code to a button, referencing the field you are in.
 

Attachments

  • Prueba.accdb
    680 KB · Views: 70

zelarra821

Registered User.
Local time
Today, 22:31
Joined
Jan 14, 2019
Messages
813
OK, perfect. I have created a ribbon with two tabs, one with the default group of Paragraph Formatting, and another tab with a button for Plain Paste. Everything works correctly, but the behavior needs to be fine-tuned.

Code:
<?xml version="1.0" encoding="utf-8"?>
<customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui">
  <ribbon startFromScratch="true">
<tabs>
<tab id="T001" label="Formato de texto">
<group idMso="GroupTextFormatting">
</group>
<group id="PegadoEspecial" label="PegadoEspecial">
<button id="PegarTextoSinFormato" label="PegarTextoSinFormato" imageMso="MasterViewClose" size="large" supertip="Pegar texto sin formato" onAction="rbPegarSinFormato" />
</group>
</tab>
</tabs>
</ribbon>
</customUI>

Code:
Sub rbPegarSinFormato(Control As IRibbonControl)
  Screen.ActiveControl = Screen.ActiveControl + vbLf + Application.PlainText(Clipboard)
End Sub

Function Clipboard(Optional StoreText As String) As String
'PURPOSE: Read/Write to Clipboard
'Source: ExcelHero.com (Daniel Ferry)

Dim x As Variant
'Store as variant for 64-bit VBA support
  x = StoreText
'Create HTMLFile Object
  With CreateObject("htmlfile")
    With .parentWindow.clipboardData
      Select Case True
        Case Len(StoreText)
          'Write to the clipboard
            .setData "text", x
        Case Else
          'Read from the clipboard (no variable passed through)
            Clipboard = .GetData("text")
      End Select
    End With
  End With
End Function

I detail what happens:

1. When I paste the text, it ends up selecting all the text, and I want it to stay at the beginning or at the end, it doesn't matter to me.
2. When there is no text in the field, having put a paragraph break, it adds a paragraph and then the text. Here we should put a conditional that says that if the field is empty, that it only adds the copied text without formatting; but if the field is filled, it adds a paragraph mark (I've put a constant, but it doesn't add a paragraph) and the plain text.
3. In the ribbon, I want to add an image to the custom button, but I don't know how it is. Can you explain me?

Thank you very much.
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 16:31
Joined
May 21, 2018
Messages
8,533
1. When I paste the text, it ends up selecting all the text, and I want it to stay at the beginning or at the end, it doesn't matter to me.
Try after the paste
Code:
dim ctrl as access.control
set ctrl = me.activecontrol
ctrl.selstart = len(ctrl.text)
ctrl.selLength = 1

2. When there is no text in the field, having put a paragraph break, it adds a paragraph and then the text. Here we should put a conditional that says that if the field is empty, that it only adds the copied text without formatting; but if the field is filled, it adds a paragraph mark (I've put a constant, but it doesn't add a paragraph) and the plain text.
Code:
if (me.activecontrol.text & "") = "" then
  me.ActiveControl.text & Application.PlainText(Clipboard)
else
  Me.ActiveControl.text & vbLf & Application.PlainText(Clipboard)
end if

3. In the ribbon, I want to add an image to the custom button, but I don't know how it is. Can you explain me?

I would ask in a separate thread. I am not good at Ribbons but there are others on the site that are very good.
 

zelarra821

Registered User.
Local time
Today, 22:31
Joined
Jan 14, 2019
Messages
813
From the image, it was only to change it in what I have happened to you: I have put paste and it has already taken the one I wanted.

One last question: I'm trying to get it to create a separate new line for me, and there's no way. I have tried with vbCrLf, vbCr, vbLf and vbNewLine, but none creates a separate line for me
 

zelarra821

Registered User.
Local time
Today, 22:31
Joined
Jan 14, 2019
Messages
813
The answer to the last question is this:

Code:
Sub rbPegarSinFormato(Control As IRibbonControl)
  
If (Screen.ActiveControl & "") = "" Then
  Screen.ActiveControl = Screen.ActiveControl + Application.PlainText(Clipboard)
Else
  Screen.ActiveControl = Application.HtmlEncode(Application.PlainText(Screen.ActiveControl & "<br>" & Clipboard))
End If
  
  Dim ctrl As Access.Control
Set ctrl = Screen.ActiveControl
ctrl.SelStart = Len(ctrl.Text)
End Sub
 

Users who are viewing this thread

Top Bottom