How to display Word text in text box with Word formatting

D

Dean Slindee

If I cut and paste a Word document into a text box, the Word spacing and
formatting is preserved. However, if I move the same Word text into the
textbox by code, the text is displayed without the Word formatting (smashed
together with control chars visible).
Is there any way to preserve the Word text formatting after the text move by
code?

Thanks,
Dean Slindee
 
A

Alick [MSFT]

Hi Dean,

To copy and paste the RFT format text from Word into rich text box control
on Microsoft Access Form, there is a little more job we need to do. We may
need to call Windows APIs since for some situation DoCmd.RunCommand method
doesn't' available, calling Windows APIs would be common/general resolution.

The normal/correct process for retrieving RTF format text from clipboard is
as follows:

1. RegisterClipboardFormat to register the "Rich Text Format" 2.
OpenClipboard 3. GetCliboardData. Use the format identifier returned by
RegisterClipboardFormat. Return will be RTF in the form of a string. 4.
CloseClipboard 5. Save the data in Access.
I have included an article and developed a sample for your reference.

Article
====
ACC2000: How to Retrieve Information from the Clipboard
http://support.microsoft.com/?id=210213

Sample
=====

1. Create a new form, add one rich text box control and command button.
2. On the button click event, copy and past the following code:


Dim wdapp As Word.Application

Set wdapp = CreateObject("Word.Application")

'Launch Word

wdapp.Documents.Add

'Make it visible

wdapp.Visible = True

'Allow user to enter some text and format it

MsgBox "Add text to the Document ,format it and then press OK"

'Select the entire document

wdapp.Selection.WholeStory

'Copy the selection

wdapp.Selection.Copy

'Get the contents of the Clipboard in RTF format
RichTextBox1.TextRTF = ClipBoard_GetData()

3. Create a new module, copy and paste the following code:

Option Compare Database

Declare Function OpenClipboard Lib "user32" (ByVal hwnd As Long) _
As Long
Declare Function CloseClipboard Lib "user32" () As Long
Declare Function GetClipboardData Lib "user32" (ByVal wFormat As _
Long) As Long
Declare Function GlobalAlloc Lib "kernel32" (ByVal wFlags&, ByVal _
dwBytes As Long) As Long
Declare Function GlobalLock Lib "kernel32" (ByVal hMem As Long) _
As Long
Declare Function GlobalUnlock Lib "kernel32" (ByVal hMem As Long) _
As Long
Declare Function GlobalSize Lib "kernel32" (ByVal hMem As Long) _
As Long
Declare Function lstrcpy Lib "kernel32" (ByVal lpString1 As Any, _
ByVal lpString2 As Any) As Long
Declare Function RegisterClipboardFormat Lib "user32" Alias _
"RegisterClipboardFormatA" (ByVal lpString As String) As Long

Public Const GHND = &H42
Public Const CF_TEXT = 1
'#define CF_LOCALE 16
Public Const CF_LOCALE = 16
Public Const MAXSIZE = 4096

Function ClipBoard_GetData()
Dim hClipMemory As Long
Dim lpClipMemory As Long
Dim MyString As String
Dim RetVal As Long
Dim lRTF As Long

lRTF = RegisterClipboardFormat("Rich Text Format")

If OpenClipboard(0&) = 0 Then
MsgBox "Cannot open Clipboard. Another app. may have it open"
Exit Function
End If


' Obtain the handle to the global memory
' block that is referencing the text.

'hClipMemory = GetClipboardData(CF_TEXT)
hClipMemory = GetClipboardData(lRTF)
'CF_LOCALE
If IsNull(hClipMemory) Then
MsgBox "Could not allocate memory"
GoTo OutOfHere
End If

' Lock Clipboard memory so we can reference
' the actual data string.
lpClipMemory = GlobalLock(hClipMemory)

If Not IsNull(lpClipMemory) Then
MyString = Space$(MAXSIZE)
RetVal = lstrcpy(MyString, lpClipMemory)
RetVal = GlobalUnlock(hClipMemory)

' Peel off the null terminating character.
MyString = Mid(MyString, 1, InStr(1, MyString, Chr$(0), 0) - 1)
Debug.Print MyString

Else
MsgBox "Could not lock memory to copy string from."
End If

OutOfHere:

RetVal = CloseClipboard()
ClipBoard_GetData = MyString

End Function

4. Save the changes, run the form.
5. Click the button, a Word window appears, enter and format text in the
word. Switch back to the Access applications, click OK.
6. The RTF format text is pasted into rich text box control.

Please feel free to let me know if you have any concerns or questions.




Sincerely,

Alick Ye, MCSD
Product Support Services
Microsoft Corporation
Get Secure! - <www.microsoft.com/security>

This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
| From: "Dean Slindee" <[email protected]>
| Subject: How to display Word text in text box with Word formatting
| Date: Mon, 20 Oct 2003 22:07:04 -0500
| X-Tomcat-NG: microsoft.public.access.formscoding
|
| If I cut and paste a Word document into a text box, the Word spacing and
| formatting is preserved. However, if I move the same Word text into the
| textbox by code, the text is displayed without the Word formatting
(smashed
| together with control chars visible).
| Is there any way to preserve the Word text formatting after the text move
by
| code?
|
| Thanks,
| Dean Slindee
|
|
|
 
D

Dean Slindee

Alick,



Thank you for the code sample. It looks good, and I will tuck it away for
use some day.

For this problem, there is thankfully an easier way to solve the problem.

One statement can do it:

rtbCrisisPlan = wrdApp.ActiveDocument.Content



However, there are a couple of other problems associated with using an rtb
in Access that

must be addressed in order to get an the form to work right. Here are the
problems encountered and the workarounds to satisfy Access.



'documentation of behavior workarounds for using an rtb in an Access
form:

'1. Access can bind an rtb to a table data column

'2 However, the bind will not work if the data column's value is null

'3. So, memCrisisPlan (a textbox) is bound to CrisisPlan (a table data
column)

'4. rtbCrisisPlan is updated with memCrisisPlan value (when not null),
otherwise blanked.

'5. above rtb update fails in Form_Current event for first db row (only)

'6. duplicate the selective rtb update code to the rtb_GotFocus event to
handle the first db row

'7. user only sees the formatted rtb content, as rtbCrisisPlan is
visible, memCrisisPlan is not



Dean Slindee
 
A

Alick [MSFT]

Hi Dean,

Thanks for sharing your resolution with us. However, are you sure the code
rtbCrisisPlan = wrdApp.ActiveDocument.Content work? My test shows it only
passes the text content to the rich text box, the formatting are missing;
for example, string "Hello World" in red in Word will be passed/assigned to
rich text box without color, in other word, the formatting color red is
lost.

I'd like to thank you for posting workarounds for other problem you
encountered; I believe that will benefit others in the Newsgroup.

If you have any concerns or questions, please feel free to reply to this
thread.



Sincerely,

Alick Ye, MCSD
Product Support Services
Microsoft Corporation
Get Secure! - <www.microsoft.com/security>

This posting is provided "AS IS" with no warranties, and confers no rights.




--------------------
| From: "Dean Slindee" <[email protected]>
| References: <[email protected]> <aLkNw1GmDHA.2148
| X-Tomcat-NG: microsoft.public.access.formscoding
|
| Alick,
|
|
|
| Thank you for the code sample. It looks good, and I will tuck it away for
| use some day.
|
| For this problem, there is thankfully an easier way to solve the problem.
|
| One statement can do it:
|
| rtbCrisisPlan = wrdApp.ActiveDocument.Content
|
|
|
| However, there are a couple of other problems associated with using an rtb
| in Access that
|
| must be addressed in order to get an the form to work right. Here are the
| problems encountered and the workarounds to satisfy Access.
|
|
|
| 'documentation of behavior workarounds for using an rtb in an Access
| form:
|
| '1. Access can bind an rtb to a table data column
|
| '2 However, the bind will not work if the data column's value is null
|
| '3. So, memCrisisPlan (a textbox) is bound to CrisisPlan (a table data
| column)
|
| '4. rtbCrisisPlan is updated with memCrisisPlan value (when not null),
| otherwise blanked.
|
| '5. above rtb update fails in Form_Current event for first db row
(only)
|
| '6. duplicate the selective rtb update code to the rtb_GotFocus event
to
| handle the first db row
|
| '7. user only sees the formatted rtb content, as rtbCrisisPlan is
| visible, memCrisisPlan is not
|
|
|
| Dean Slindee
|
| | > Hi Dean,
| >
| > To copy and paste the RFT format text from Word into rich text box
control
| > on Microsoft Access Form, there is a little more job we need to do. We
may
| > need to call Windows APIs since for some situation DoCmd.RunCommand
method
| > doesn't' available, calling Windows APIs would be common/general
| resolution.
| >
| > The normal/correct process for retrieving RTF format text from clipboard
| is
| > as follows:
| >
| > 1. RegisterClipboardFormat to register the "Rich Text Format" 2.
| > OpenClipboard 3. GetCliboardData. Use the format identifier returned by
| > RegisterClipboardFormat. Return will be RTF in the form of a string. 4.
| > CloseClipboard 5. Save the data in Access.
| > I have included an article and developed a sample for your reference.
| >
| > Article
| > ====
| > ACC2000: How to Retrieve Information from the Clipboard
| > http://support.microsoft.com/?id=210213
| >
| > Sample
| > =====
| >
| > 1. Create a new form, add one rich text box control and command button.
| > 2. On the button click event, copy and past the following code:
| >
| >
| > Dim wdapp As Word.Application
| >
| > Set wdapp = CreateObject("Word.Application")
| >
| > 'Launch Word
| >
| > wdapp.Documents.Add
| >
| > 'Make it visible
| >
| > wdapp.Visible = True
| >
| > 'Allow user to enter some text and format it
| >
| > MsgBox "Add text to the Document ,format it and then press OK"
| >
| > 'Select the entire document
| >
| > wdapp.Selection.WholeStory
| >
| > 'Copy the selection
| >
| > wdapp.Selection.Copy
| >
| > 'Get the contents of the Clipboard in RTF format
| > RichTextBox1.TextRTF = ClipBoard_GetData()
| >
| > 3. Create a new module, copy and paste the following code:
| >
| > Option Compare Database
| >
| > Declare Function OpenClipboard Lib "user32" (ByVal hwnd As Long) _
| > As Long
| > Declare Function CloseClipboard Lib "user32" () As Long
| > Declare Function GetClipboardData Lib "user32" (ByVal wFormat As _
| > Long) As Long
| > Declare Function GlobalAlloc Lib "kernel32" (ByVal wFlags&, ByVal _
| > dwBytes As Long) As Long
| > Declare Function GlobalLock Lib "kernel32" (ByVal hMem As Long) _
| > As Long
| > Declare Function GlobalUnlock Lib "kernel32" (ByVal hMem As Long) _
| > As Long
| > Declare Function GlobalSize Lib "kernel32" (ByVal hMem As Long) _
| > As Long
| > Declare Function lstrcpy Lib "kernel32" (ByVal lpString1 As Any, _
| > ByVal lpString2 As Any) As Long
| > Declare Function RegisterClipboardFormat Lib "user32" Alias _
| > "RegisterClipboardFormatA" (ByVal lpString As String) As Long
| >
| > Public Const GHND = &H42
| > Public Const CF_TEXT = 1
| > '#define CF_LOCALE 16
| > Public Const CF_LOCALE = 16
| > Public Const MAXSIZE = 4096
| >
| > Function ClipBoard_GetData()
| > Dim hClipMemory As Long
| > Dim lpClipMemory As Long
| > Dim MyString As String
| > Dim RetVal As Long
| > Dim lRTF As Long
| >
| > lRTF = RegisterClipboardFormat("Rich Text Format")
| >
| > If OpenClipboard(0&) = 0 Then
| > MsgBox "Cannot open Clipboard. Another app. may have it open"
| > Exit Function
| > End If
| >
| >
| > ' Obtain the handle to the global memory
| > ' block that is referencing the text.
| >
| > 'hClipMemory = GetClipboardData(CF_TEXT)
| > hClipMemory = GetClipboardData(lRTF)
| > 'CF_LOCALE
| > If IsNull(hClipMemory) Then
| > MsgBox "Could not allocate memory"
| > GoTo OutOfHere
| > End If
| >
| > ' Lock Clipboard memory so we can reference
| > ' the actual data string.
| > lpClipMemory = GlobalLock(hClipMemory)
| >
| > If Not IsNull(lpClipMemory) Then
| > MyString = Space$(MAXSIZE)
| > RetVal = lstrcpy(MyString, lpClipMemory)
| > RetVal = GlobalUnlock(hClipMemory)
| >
| > ' Peel off the null terminating character.
| > MyString = Mid(MyString, 1, InStr(1, MyString, Chr$(0), 0) - 1)
| > Debug.Print MyString
| >
| > Else
| > MsgBox "Could not lock memory to copy string from."
| > End If
| >
| > OutOfHere:
| >
| > RetVal = CloseClipboard()
| > ClipBoard_GetData = MyString
| >
| > End Function
| >
| > 4. Save the changes, run the form.
| > 5. Click the button, a Word window appears, enter and format text in the
| > word. Switch back to the Access applications, click OK.
| > 6. The RTF format text is pasted into rich text box control.
| >
| > Please feel free to let me know if you have any concerns or questions.
| >
| >
| >
| >
| > Sincerely,
| >
| > Alick Ye, MCSD
| > Product Support Services
| > Microsoft Corporation
| > Get Secure! - <www.microsoft.com/security>
| >
| > This posting is provided "AS IS" with no warranties, and confers no
| rights.
| >
| > --------------------
| > | From: "Dean Slindee" <[email protected]>
| > | Subject: How to display Word text in text box with Word formatting
| > | Date: Mon, 20 Oct 2003 22:07:04 -0500
| > | X-Tomcat-NG: microsoft.public.access.formscoding
| > |
| > | If I cut and paste a Word document into a text box, the Word spacing
and
| > | formatting is preserved. However, if I move the same Word text into
the
| > | textbox by code, the text is displayed without the Word formatting
| > (smashed
| > | together with control chars visible).
| > | Is there any way to preserve the Word text formatting after the text
| move
| > by
| > | code?
| > |
| > | Thanks,
| > | Dean Slindee
| > |
| > |
| > |
| >
|
|
|
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top