Ho bisogno di salvare il testo formattato da Word in un database di Access.copia testo formattato in accesso utilizzando vba
Finora sono riuscito a capire come memorizzare il testo formattato in un campo di accesso (creare un campo Memo in una tabella e impostare il formato di testo come testo RTF). Ricerca SO Non ho ancora trovato una soluzione su come trasportare il testo da Word in Access.
So che è possibile, perché è possibile farlo semplicemente copiando e incollando le informazioni se lo si sta facendo manualmente.
La mia domanda, come posso copiare il testo formattato da una parola in un campo in una tabella utilizzando VBA?
Sperimentalmente ho creato quanto segue per testarlo. Finora senza successo ...
Sub GetComments()
'Imports Analyst Comments from Excel files als OLE Objects.
'---------------------------------
'Access Variables
Dim dbsFundDB As DAO.Database
Dim rsComments As DAO.Recordset
Set dbsFundDB = CurrentDb
Set rsComments = dbsFundDB.OpenRecordset("tblFunds")
'Word Variables
Dim doc As Word.Application
Dim dcmt As Word.Document
Dim sectn As Word.Section
Dim obCommentText As Object
Dim sAnalystText As String
'Open New Word File
Set doc = New Word.Application
doc.Visible = True
Set dcmt = doc.Documents.Open(sPathTemplate)
Set sectn = dcmt.Sections(1)
sectn.Range.Select
Selection.InsertFile FileName:="myfile.rtf", Range:="", _
ConfirmConversions:=False, Link:=False, Attachment:=False
sAnalystText = sectn.Range.Tables(1).cell(1, 1).Range.FormattedText
rsComments.AddNew
rsComments![Long Comment Exec] = sAnalystText
rsComments.Update
sectn.Range.Select
dcmt.Close savechanges:=False
doc.Quit
End Sub
UPDATE ho provato attuare la risposta di Matt Hall. Mentre il testo è infatti copiato nel database, non è ancora a mantenere la formattazione:
Qui è la mia realizzazione come un semplice test:
Option Explicit
Public Const sPathTemplate As String = "W:\L\BDTP\Products\FundResearchTool\Advisory.docx"
Option Compare Database
Sub GetComments()
'Imports Comments from word and save in DB
'Test soubroutine
'---------------------------------
'Word Variables
Dim obCommentText As Variant
Dim strSQL As String
obCommentText = GetWordContent(sPathTemplate)
strSQL = "insert into [tblText]([TestField]) values('" & obCommentText & "')"
DoCmd.RunSQL strSQL
MsgBox "Import Successful", vbInformation Or vbOKOnly
End Sub
Private Function GetWordContent(strFile As String) As Variant
' This function takes the path obtained to the MS-Word Document selected in
' the FileToOpen function and then uses that to open that MS-Word Document
' and retrieve its text contents
Dim objDoc As Word.Document
Set objDoc = GetObject(strFile)
GetWordContent = CVar(objDoc.Sections(1).Range.Text)
objDoc.Close
End Function
Io in realtà non hanno esperienza con quello che stai facendo . Tuttavia sono stato in grado di trovare questo post che sembra avere alcuni suggerimenti decenti: http://stackoverflow.com/questions/14316916/copy-text-from-table-in-word-and-retaing-formatting – Newd