using vba to copy RTF field into word (2016) (2 Viewers)

phill.oflynn

New member
Local time
Today, 00:37
Joined
May 23, 2019
Messages
1
I need to figure out a way to insert an RTF field into word table.


I have googled high and low. found a lot of info bu there seems to be nothing conclusive.


Using the "dataobject" seems to insert plain text including the HTML tags rather than formatted text. No matter what I do.


The closest I've come is to manually copy the "RTF" (which really isn't RTF these days but HTML) and use.
Code:
table.Cell(table.Rows.Count - 2, 2).Range.PasteSpecial , , , , WdPasteDataType.wdPasteRTF
to paste. into word. This bit works.


So it would seem that the trick is to properly put the data onto the clipboard. So I have found a few web pages on using the windows api in a module.



However, I'm not sure how to register the datatype owing to the fact that in later versions of access, RTF uses HTML but isn't really a HTML format.


Appreciate the help. Thanks


Phill
 

JHB

Have been here a while
Local time
Today, 09:37
Joined
Jun 17, 2012
Messages
7,732
You could try the following for copy, (I haven't tested it).
Code:
      Me.TheFieldWithTheRTFContent.SetFocus
      DoCmd.RunCommand acCmdCopy
      DoEvents
      'Here your code for pasting into the Word table.
 

sxschech

Registered User.
Local time
Today, 00:37
Joined
Mar 2, 2010
Messages
792
I suppose in worst case scenario, you could format the text after the fact.

This code will unbold text, so is a matter of identifying the type of formatting needed to adjust the code to reflect that (italic, colours, etc.). I call it from another function so that it can be used in other places too. For some reason the code I had for cell data (referencing row/col) did not seem to modify formatting, so ended up using below to do formatting.

To call it:
UnboldText WordDoc, stExtractTitle


Code:
Sub UnboldText(WordDoc As Object, BoldText As String)
'based on msReplaceCellData 
''https://www.access-programmers.co.uk/forums/showthread.php?t=303378
'20190123 and code from
'RangeRedefinedDemoI
'https://gregmaxey.com/word_tip_pages/words_fickle_vba_find_property.html
'As this code is dependent on being called from another sub/function
'the WordDoc is already defined and opened, so no need to include that
'code here.  Downside, the code can't run stand alone.
'Tried to code it more like msReplaceCellData, but for some reason
'that method would not unbold, so this appears to search entire
'document.  In most cases should be okay as the title probably
'won't be bolded in multiple locations.  However, if this code is
'eventually used for other text phrases, may be something to be
'aware of.
'20190516
    Dim WordRange As Object
    'Define range.
    Set WordRange = WordDoc.Content
    'Evaluate WordRange start and end points.
    'Debug.Print WordRange.Start & " " & WordRange.End
    With WordRange.Find
       .ClearFormatting
       .Replacement.ClearFormatting
       .Text = ""
       .Replacement.Text = ""
       .Forward = True
       .Wrap = 0   'wdFindStop
       .Format = False
       .MatchCase = False
       .MatchWholeWord = False
       .MatchWildcards = False
       .MatchSoundsLike = False
       .MatchAllWordForms = False
       .Execute
     End With
    With WordRange.Find
        .Text = BoldText 
        While .Execute
            'Evaluate WordRange start and end points.
            'Debug.Print WordRange.Start & " " & WordRange.End
            WordRange.Bold = False
        Wend
    End With
End Sub
 

Users who are viewing this thread

Top Bottom