Paste as Plain Text msoControlButton (1 Viewer)

smig

Registered User.
Local time
Today, 23:27
Joined
Nov 25, 2009
Messages
2,209
I'm using a right menu click menu

Since the field is a rich text one I want to limit the users to paste a plain text only, but can't find the correct msoControlButton button ID :(
Is there another option, If there is no special ID for it ?

Thanks

Code:
' Create the PlainText menu.
Set cmbRightClick = CommandBars.Add("cmdPlainTextRightClick", msoBarPopup, False, True)
With cmbRightClick
    ' Add the Copy command.
    Set cmbControl = .Controls.Add(msoControlButton, 19, , , True)
    cmbControl.Caption = "Copy"
    ' Add the Cut command.
    Set cmbControl = .Controls.Add(msoControlButton, 21, , , True)
    cmbControl.Caption = "Cut"
    ' Add the Paste command.
    Set cmbControl = .Controls.Add(msoControlButton, 22, , , True)
'    Set cmbControl = .Controls.Add(msoControlButton, 755, , , True)
    cmbControl.Caption = "Paste"
End With
Set cmbRightClick = Nothing
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 05:27
Joined
May 7, 2009
Messages
19,169
paste first what is on the clipboard to a dummy textbox (plain text format, change the Forecolor/boarder to make an impression that it is hidden.
also Disallow Tab to this textbox)
so that it loose all formatting. then copy the content of this hidden textbox to the Target rich textbox.

use Userdefined function on your context menu for the Paste command:
Code:
    Set cmbControl = .Controls.Add
      with cmbControl
        .Caption = "Paste"
        .OnAction = "=fPaste()"
        .FaceId = 22
    end with

on a public Module add the code for the function fPaste():
Code:
Function fPaste()
    Dim f As Form
    Dim ipos As Integer
On Error Resume Next
   
    Set f = Screen.ActiveForm
   
    If f.Name = "yourSpecificFormNameHere" Then
        ipos = f("theRichTextboxName").SelStart
        With f("theUnboundPlainTextboxName")
            .SetFocus
            .value = Null
            Application.CommandBars.ExecuteMso ("Paste")
            .SelStart = 0
            .SelLength = Len(.Text)
            Application.CommandBars.ExecuteMso ("Copy")
        End With
       
        With f("theRichTextboxName")
            .SetFocus
            .SelStart = ipos
            .SelLength = 0
            Application.CommandBars.ExecuteMso ("Paste")
        End With
    Else
        Application.CommandBars.ExecuteMso ("Paste")
    End If
    Set f = Nothing
End Function
 

smig

Registered User.
Local time
Today, 23:27
Joined
Nov 25, 2009
Messages
2,209
paste first what is on the clipboard to a dummy textbox (plain text format, change the Forecolor/boarder to make an impression that it is hidden.
also Disallow Tab to this textbox)
so that it loose all formatting. then copy the content of this hidden textbox to the Target rich textbox.

use Userdefined function on your context menu for the Paste command:
Code:
    Set cmbControl = .Controls.Add
      with cmbControl
        .Caption = "Paste"
        .OnAction = "=fPaste()"
        .FaceId = 22
    end with

on a public Module add the code for the function fPaste():
Code:
Function fPaste()
    Dim f As Form
    Dim ipos As Integer
On Error Resume Next
  
    Set f = Screen.ActiveForm
  
    If f.Name = "yourSpecificFormNameHere" Then
        ipos = f("theRichTextboxName").SelStart
        With f("theUnboundPlainTextboxName")
            .SetFocus
            .value = Null
            Application.CommandBars.ExecuteMso ("Paste")
            .SelStart = 0
            .SelLength = Len(.Text)
            Application.CommandBars.ExecuteMso ("Copy")
        End With
      
        With f("theRichTextboxName")
            .SetFocus
            .SelStart = ipos
            .SelLength = 0
            Application.CommandBars.ExecuteMso ("Paste")
        End With
    Else
        Application.CommandBars.ExecuteMso ("Paste")
    End If
    Set f = Nothing
End Function
Thanks, but it's not exactly what I want
I want the user to be able to edit this textbox text, and be able to insert the text into the middle of it.
He can also do some kind of formatting there - Bold Underline and Italic

Can I make my own button in the right click menu to run a code that will paste the text as PlainText ?

Or, I did not understand you?
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 05:27
Joined
May 7, 2009
Messages
19,169
even if you succeed on pasting Plaintext to the richtextbox, it will still get formatted when you insert it, eg:

<font color=red>first text[ ] other text</font>

above is a richtextbox with color red.
if you insert the Plaintext inside [ ] it will be colored red even if it is plain.
see the point.
 

smig

Registered User.
Local time
Today, 23:27
Joined
Nov 25, 2009
Messages
2,209
paste first what is on the clipboard to a dummy textbox (plain text format, change the Forecolor/boarder to make an impression that it is hidden.
also Disallow Tab to this textbox)
so that it loose all formatting. then copy the content of this hidden textbox to the Target rich textbox.

use Userdefined function on your context menu for the Paste command:
Code:
    Set cmbControl = .Controls.Add
      with cmbControl
        .Caption = "Paste"
        .OnAction = "=fPaste()"
        .FaceId = 22
    end with

on a public Module add the code for the function fPaste():
Code:
Function fPaste()
    Dim f As Form
    Dim ipos As Integer
On Error Resume Next
  
    Set f = Screen.ActiveForm
  
    If f.Name = "yourSpecificFormNameHere" Then
        ipos = f("theRichTextboxName").SelStart
        With f("theUnboundPlainTextboxName")
            .SetFocus
            .value = Null
            Application.CommandBars.ExecuteMso ("Paste")
            .SelStart = 0
            .SelLength = Len(.Text)
            Application.CommandBars.ExecuteMso ("Copy")
        End With
      
        With f("theRichTextboxName")
            .SetFocus
            .SelStart = ipos
            .SelLength = 0
            Application.CommandBars.ExecuteMso ("Paste")
        End With
    Else
        Application.CommandBars.ExecuteMso ("Paste")
    End If
    Set f = Nothing
End Function
How do I send the form name and the control name I'm currently in to the function ?
I guess I can use something like fPaste(Me.Name)
I know I can use Me to send the frm As Form, or use Me.Name to send the Form's name
How do I send the Control name ?
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 05:27
Joined
May 7, 2009
Messages
19,169
fPaste() function already checked the Form for you:

Screen.ActiveForm

all you need to do is determine if the "form" is the one you want to work on.
 

smig

Registered User.
Local time
Today, 23:27
Joined
Nov 25, 2009
Messages
2,209
fPaste() function already checked the Form for you:

Screen.ActiveForm

all you need to do is determine if the "form" is the one you want to work on.
Yes, I saw that :)
How do I send the Me.ControlName ?
 

Users who are viewing this thread

Top Bottom