Link to home
Start Free TrialLog in
Avatar of BAnders
BAndersFlag for Sweden

asked on

Save formatted text fram word to an Ms Access memofield

Hi,

I have an application which "read" a word document and collect sentences or paragraphs that contain a number of keywords and save it in a memo field. Today I just collect the text but I would like to collect the formatting and show it in a textbox with Rich Text property for show the formatting. I have tried to use the word .formattedText propery of sentence or paragraph object without success.

Is it possible to do in vba (manually cut and paste seems to work) ? If yes how?

Thanks in advance.

BAnders
Avatar of Dale Fye
Dale Fye
Flag of United States of America image

Memo fields have a TextFormat property, set that to Rich Text.

Then use the Text Format property of the textbox and set it to Rich Text also.

I'm not sure how you are "collecting the text", but copying text from somewhere and pasting it into a textbox is not likely to achieve the results you are looking for, unless you grab the tags at the beginning and end of formatted text.
Avatar of BAnders

ASKER

By "Collecting", today I just use the .Text property of Sentence or Paragraph object of word. As you are saying I want to grab the tags and how can I do that?
So Access and word don't format text in the same way, and don't support the same formatting. This means even if you could write a procedure, there would be things that might not come cleanly across the "bridge" between the 2 apps.

On my blog you can see an example of writing a simple parser to export access rich text into excel, converting from one format to another.
Exporting rich text  from Access to Excel
Its a big time investment, and I know of no simple workaround.
How do you
collect sentences or paragraphs that contain a number of keywords
?
Well i must admit that my involvement with RichText box was kind of minimal but this seemed like an interesting question
As mentioned above there isn't a simple straightforward solution  and it would require some work in order to get the desired result.
It all boils to getting the formatting of each every character from the text of the Word Document and reconstruct it back in Access
For start take a look at this code
Private Function GetFormattedWordContent(strFile As String) As String
Dim objDoc As Word.Document
Set objDoc = GetObject(strFile)
Dim wchar as object
For Each wchar In objDoc.range.characters
 Debug.Print wchar.Font.Name ' <-- Just to show how to get access to the individual character formatting
Next
objDoc.Close
'here we need some code to store wchar probably to a collection in order to prepare it as a big string
End Function

Open in new window

Now if you put a watch on the wchar you will see it has a ton of properties so the big task would be to read each and every character ....check its properties...map this properties to HTML tags ...enclose the actual text to this tags...construct the big string that holds the hmtl representation of the word document and store it to your memo field
Probably the above code from Anders should be helpful from a quick glance i gave it...
In order for this to work you have the field in the underlying table as "Plain Text" and the TextBox to "Rich Text"...despite the warning
Because a picture worths 1000 words here a small example
Having a Word document with a single word
User generated image.....iterating the characters you could construct a string like this ( i am a bit rusty on HTML)
<div><font face="Calibri" size=20>T</font><font face="Calibri" size=20 color=red>e<em><u>s</u></em></font><font
face="Calibri" size=20 color=aqua>t</font></div>

rendered to this
User generated image
ASKER CERTIFIED SOLUTION
Avatar of Mark Edwards
Mark Edwards
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of BAnders

ASKER

Marks code works. Still, I have to test the performance of the code.
Thanks a lot.