Copy/Paste RTF from table to textbox without codes div <br> etc.

bignose2

Registered User.
Local time
Today, 17:28
Joined
May 2, 2010
Messages
248
Hi,

I have a table with rft formatted memo fields OK.
All the forms & text boxes are RTF.

I lookup text in a table, copy via VBA and paste manually using ctrl v.

However the pasted test I guess is copied inc. all the html formatting /div <br> etc.

I assume I need to ClipBoard_SetText with rtf formatting intact.
I can't see how to do this, there is plenty of examples where it is taken from text boxes with...
Dim str AsString = RichTextBox1.SelectedRtf
Clipboard.SetText(str, TextDataFormat.Rtf)

but not from a string itself or in my case a dlookup.

AddNote = DLookup("[Start]", "eResponses", "[ID] = " & Me!eResponseCombo & "")

ClipBoard_SetText (AddNote)

' Clipboard.SetText(AddNote, TextDataFormat.Rtf) - not work as lots of other things I have tried.

I guess I could copy to a textbox but seems an unecessary step.

Any advise

thanks I/a
 
hope this simple function will help you.

Code:
Public Function fnRemoveHTMLTags(ByVal s As String) As String

    Dim i As Long
    Dim lngLength As Long
    Dim sStr As String
    Dim bolStart As Boolean
    
    lngLength = Len(s)
    
    If lngLength > 0 Then
        s = Replace$(s, "<br>", vbCrLf)
    
        lngLength = Len(s)
        For i = 1 To lngLength
            sStr = Mid(s, i, 1)
            If sStr = "<" Then
                bolStart = True
            End If
            If bolStart Then
                s = left(s, i - 1) & Chr(255) & Mid(s, i + 1)
                If sStr = ">" Then bolStart = False
            End If
        Next i
        s = Replace(s, Chr(255), "")
        
    End If
    fnRemoveHTMLTags = s
            
End Function

to call:
AddNote = "" & DLookup("[Start]", "eResponses", "[ID] = " & Me!eResponseCombo & "")

AddNote=fnRemoveHTMLTags(AddNote)
 
Thanks for that, I was rather hoping I might keep some of the formatting, bold for e.g.

it looked to me (though not tested) if copied from textbox it would keep the rtf as long as it was specified.

I might try sending the table data to a rtf text box & then copying from there, unless someone does have a direct way.

thanks again.
 
if you only need to remove <div> and <br>, just use Replace function:

AddNote = Replace$(AddNote, "<div>","")
AddNote = Replace$(AddNote, "</div>","")
AddNote = Replace$(AddNote, "<br>",vbcrlf)
 
I thought I would try using a textbox from code I found...

Dim textContents As String = myRichTextBox.Rtf

' Copy the text to the clipboard
Clipboard.SetText(textContents, TextDataFormat.Rtf)

It seems this code must be for something other than access as it does not compile at all.

Looks like I will have to for removal of the html tags.

shame as I would like to retain bold, italics etc. but still be able to paste.
 
In the end found simple docmd copy keeps it all the formatting fine, over complicated things as usual.



Me.TempCopyPaste = AddNote
Me.TempCopyPaste.SetFocus
DoCmd.RunCommand acCmdCopy
 
Hi,

Following on from my original post,

Got all working well with the docmd copy & paste

Now, is there a way to DIM a text string as HTML?

All this is for emailing so now a few more small issues with replies & re-import.
Basically I am having to strip out Tabs & CR's & extra spaces on replies, not sure why I am getting them, especially spaces but spent hours trying to reason in the end found someone had a little routine that fixed so guess something that does happen.

The email is imported into a rtf text box, Use to do this direct

strSource = Me!Contents
strSource = Replace(strSource, vbTab, "")

' convert all CRLFs to spaces
strSource = Replace(strSource, vbCrLf, "")
' Find and replace any occurences of multiple spaces
Do While (InStr(strSource, " "))
' if true, the string still contains double spaces,
' replace with single space
strSource = Replace(strSource, " ", " ")
Loop
' ------ If I don't do the copy paste below me!contents includes all the <div/> ... html tags etc the NEXT time I try to reply.
Me.TempCopyPaste = strSource
Me.TempCopyPaste.SetFocus
DoCmd.RunCommand acCmdCopy
Me.Contents.SetFocus
DoCmd.RunCommand acCmdPaste

' Me!Contents = strSource - use to use this

I think as soon as strSource takes the data it turns all the html into <div> & <div /> etc. and is visible when back into me!contents even though this is rtf memo field.

THis does fix it & works fine But wondered if there is a way to DIM strSource as a html/rtf text.

sorry long winded..

thanks i/a.
 
I am not expert in this, but a basic vba string is just that. A string of characters.

the text processor treats some characters in a special way - chr(13) and chr(10) as a CR, and chr(8) as a tab.

HTML processes other characters in a special way

I doubt if a string can be treated as 2 different things at the same time, which is what you would like.

Assuming the data goes into a table, maybe you could store the HTML string in the table, and then massage the text display before it shows in the textbox control. You would not be able to edit it, though in the textbox.
 

Users who are viewing this thread

Back
Top Bottom