简体   繁体   English

如何将Word文档中的RTF插入访问表字段

[英]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. 在我的代码中,我从MS Access中打开一个Word文档,并读出该文档的特定部分。 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). 根据我在网上阅读的内容,Access 2007及更高版本可以轻松存储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. 我曾尝试同时使用.Text.FormattedText但均.FormattedText 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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