简体   繁体   中英

How to insert RTF from Word Doc into Access Table Field

In my code I open a word document from MS Access and read out a certain section of the document. If I was doing this and would only have to store the plain text, this would be easy enough... but I need to keep all the formatting.

From what I've read on the web, Access 2007 and up can easily store Rich Text Formatt (RTF). I adjusted my Access Table to have the specified field defined as "Memo" and "Rich Text". So the field itself is set up and working properly.

Copying and pasting some data manually gets stored as it should.

My question to which I can't seem to find an answer: How do you do it using Code???

Here is the relevant code snippet for what I have so far:

Set doc = New Word.Application
doc.Visible = True
Set dcmt = doc.Documents.Open(sPathTemplate)
Set sectn = dcmt.Sections(2)

Dim x As String
sAnalystText = sectn.Range.Tables(1).cell(1, 1).Range.FormattedText

rsComments.AddNew
rsComments![ISIN] = "Fake_ISIN"
rsComments![Fund_Selection] = 1
rsComments![Long Comment Exec] = sAnalystText
rsComments.Update

I have tried using both .Text and .FormattedText but neither works. Any help much appreciated!

Dim rs As New ADODB.Recordset
Dim A() As Byte
Dim myrtf As Variant
Dim doact As Document
Set doact = ActiveDocument
myrtf = Null
ActiveDocument.Range(0, 100).Copy

Dim dc As New Document

dc.Activate


dc.Range(0, 0).PasteAndFormat wdFormatOriginalFormatting

dc.SaveAs2 FileName:="C:\x.rtf", FileFormat:=wdFormatRTF

dc.Close

   doact.Activate
  Open "C:\x.rtf" For Binary As #1
  ReDim A(LOF(1))
  While Not (EOF(1))
        Get #1, , A(i)
        i = i + 1

  Wend
   Close #1
    myrtf = A
conn.Open "Driver={Microsoft Access Driver (*.mdb, *.accdb)};Dbq=C:\test.accdb"

strsql = "Insert into table ( rftfield) values ('" & myrtf & "')"

conn.Execute strsql

conn.Close

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM