Solved

Memo field, RTF, paste only the text

Posted on 2013-05-26
8
574 Views
Last Modified: 2013-05-27
Experts,

I have a memo field formatted as RTF.
I have this memo field on a form that opens as modal.
I want to paste the text as plain text (and then format it)
I hit paste special<Text and it pastes as RTF.  

How can I paste the text as plain?  
I do not want to paste to notepad and then paste into the memo field (too many steps).  

thank you.
0
Comment
Question by:pdvsa
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 29

Assisted Solution

by:IrogSinta
IrogSinta earned 150 total points
ID: 39197942
Use the PlainText function:
=PlainText(NameOfMemoField)
0
 

Author Comment

by:pdvsa
ID: 39197990
OK but how do I use this?  I mean I want to be able to use it possibly as a rt click or some other simple way.
0
 
LVL 20

Expert Comment

by:GrahamMandeno
ID: 39198714
As far as I know, there is no way in the UI to "Copy as Plain Text".  However, it can be done through code with a command button.  Copy the following code and paste it into a new standard module:
Option Compare Database
Option Explicit

Private Declare Function GlobalUnlock _
  Lib "kernel32" ( _
    ByVal hMem As Long _
  ) As Long

Private Declare Function GlobalLock _
  Lib "kernel32" ( _
    ByVal hMem As Long _
  ) As Long

Private Declare Function GlobalAlloc _
  Lib "kernel32" ( _
    ByVal wFlags As Long, _
    ByVal dwBytes As Long _
  ) As Long

Private Declare Function GlobalSize _
  Lib "kernel32" ( _
    ByVal hMem As Long _
  ) As Long

Private Declare Function lstrcpy _
  Lib "kernel32" ( _
    ByVal lpString1 As Any, _
    ByVal lpString2 As Any _
  ) As Long

Private Declare Function CloseClipboard _
  Lib "user32" ( _
  ) As Long

Private Declare Function OpenClipboard _
  Lib "user32" ( _
    ByVal hWnd As Long _
  ) As Long

Private Declare Function EmptyClipboard _
  Lib "user32" ( _
  ) As Long

Private Declare Function GetClipboardData _
  Lib "user32" ( _
    ByVal wFormat As Long _
  ) As Long

Private Declare Function SetClipboardData _
  Lib "user32" ( _
    ByVal wFormat As Long, _
    ByVal hMem As Long _
  ) As Long

Private Const GHND = &H42
Private Const CF_TEXT = 1

Public Function ClipBoard_SetText(strCopyString As String) As Boolean
  Dim hGlobalMemory As Long
  Dim lpGlobalMemory As Long
  Dim hClipMemory As Long

  ' Allocate moveable global memory.
  hGlobalMemory = GlobalAlloc(GHND, Len(strCopyString) + 1)

  ' Lock the block to get a far pointer to this memory.
  lpGlobalMemory = GlobalLock(hGlobalMemory)

  ' Copy the string to this global memory.
  lpGlobalMemory = lstrcpy(lpGlobalMemory, strCopyString)

  ' Unlock the memory and then copy to the clipboard
  If GlobalUnlock(hGlobalMemory) = 0 Then
    If OpenClipboard(0&) <> 0 Then
      Call EmptyClipboard
      hClipMemory = SetClipboardData(CF_TEXT, hGlobalMemory)
      ClipBoard_SetText = CBool(CloseClipboard)
    End If
  End If
End Function

Public Function ClipBoard_GetText() As String
  Dim hClipMemory As Long
  Dim lpClipMemory As Long
  Dim strCBText As String
  Dim lngSize As Long
  If OpenClipboard(0&) <> 0 Then
    ' Obtain the handle to the global memory
    ' block that is referencing the text.
    hClipMemory = GetClipboardData(CF_TEXT)
    If hClipMemory <> 0 Then
      ' Lock Clipboard memory so we can reference
      ' the actual data string.
      lpClipMemory = GlobalLock(hClipMemory)
      If lpClipMemory <> 0 Then
        lngSize = GlobalSize(lpClipMemory)
        strCBText = SPACE$(lngSize)
        Call lstrcpy(strCBText, lpClipMemory)
        Call GlobalUnlock(hClipMemory)
        ' Peel off the null terminating character.
        strCBText = Left(strCBText, InStr(strCBText, vbNullChar) - 1)
      End If
    End If
    Call CloseClipboard
  End If
  ClipBoard_GetText = strCBText
End Function

Open in new window

Save the module as "mdlClipBoard"

Now, add a command button to your form with the caption "Copy as Plain Text" (or whatever else you like).  Name the button cmdCopyPlainText and assign it this event procedure:
Private Sub cmdCopyPlainText_Click()
On Error Resume Next
    ClipBoard_SetText Application.PlainText(Screen.PreviousControl.Value)
    If Err Then
        MsgBox "Cannot copy text" & vbCrLf & Err.Description
        Err.Clear
    End If
End Sub

Open in new window

Clicking this button will set the contents of the clipboard to the plain text version of the current control (which has just become the PreviousControl, because you have clicked the command button).

Best wishes,
Graham Mandeno [Access MVP 1996-2013]
0
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 

Author Comment

by:pdvsa
ID: 39199656
Graham:  thank you for the response.  

I have to ask a follow up as I am not certain if I am using it as I should.
I have made the module and saved it.
I put a button on the form and named it as instructed and pasted the open form code as well.

to use it I open the form, go to ms word and copy something, go back to the form, hit the command button and the text is not pasted.  

I see the command button is named Copy text and I am under the assumption the button is used to Paste the text.    

I am not sure if I am suppose to initially paste the RTF text into the field with control V and then select it again, hit the command button to copy and and then use control V to repaste as plain text.
Please let me know what to do next.
==>this method seems to remove the RTF though.  

thank you once again....
0
 
LVL 20

Expert Comment

by:GrahamMandeno
ID: 39199709
I understood that you wanted to copy from the memo textbox as plain text so that you could paste the plain text contents somewhere else.  I now see that you want to copy some formatted text from Word, and paste the plain text into your memo textbox.  My apologies for the misunderstanding.

Assign the following code to your command button's Click event:

Me![name of your memo control] = Application.PlainText(ClipBoard_GetText)

The event procedure should contain only this single line.

Good luck :-)
Graham
0
 

Author Comment

by:pdvsa
ID: 39199826
Ok that worked perfectly.   I suppose IrogSinta had essentially the same response but I am not an expert and not sure if they are one in the same.  Not certain if Irog's suggestion was to side step any module.  

Using Irog's method I sorta combined what you had mentioned but used the plaintext:
    Me!txtComments = PlainText(txtComments)
==>however, text was not pasted after copying from word.
0
 
LVL 20

Accepted Solution

by:
GrahamMandeno earned 250 total points
ID: 39199884
Me!txtComments = PlainText(txtComments)

This will simply take the rich text contents of the textbox and replace it with the plain text version.  To get it there from Word, you will need to paste in the textbox and then click the button to remove the formatting.

If you want a one-step method, then use the line I gave you:
Me!txtComments = Application.PlainText(ClipBoard_GetText)

Open in new window


This will take whatever is already in the clipboard (copied from Word), strip the formatting, and place the plain text version in your textbox.

-- Graham
0
 

Author Closing Comment

by:pdvsa
ID: 39199986
I awarded points to both because while IrogSinta had a quick solution that worked great, I did not know how to implement it and Graham taught me how to do just that and by using a module I have one less step.  

thank you both for the help.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
This article describes two methods for creating a combo box that can be used to add new items to the row source -- one for simple lookup tables, and one for a more complex row source where the new item needs data for several fields.
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question