Hi Dean,
I notice you start a new thread for the second question, I have post the
answer there. For your convenience, I also list them here in this threads.
If you have any concerns or questions, please feel free to reply to your
new started threads.
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]>
| X-Tomcat-NG: microsoft.public.access.formscoding
|
| That worked great. Thanks for the code sample.
| Now that the word document shows, I would like to have it show the top of
| the document, instead of the bottom. The .MoveStart line below does not
| work. Many MoveUp lines reposition the cursor, but that's too crude. Not
| sure what else to try, since that would seem to be the obvious statement
to
| use.
| With wrdApp.Selection
|
| .TypeText strCrisisPlan
|
| .InsertParagraph
|
| .MoveStart
|
| End With
|
| Second question: after modifying the word document, it would be nice to
| update the memo field from whence the Word text body came. The line below
| does that, but the text is smashed together, not formatted like in Word,
(or
| if I cut and paste from Word into the memo field). Any thoughts on how to
| make that happen? Destination is a plain Access text box.
|
| memCrisisPlan = wrdApp.ActiveDocument.Content
|
| Thanks,
|
| Dean Slindee
|
| | > Good Afternoon!
| >
| > There are many ways to do this. One example is below. Make
| > sure you have the reference to the Word Object Library
| > turned on.
| >
| > Dim wrdApp As Word.Application
| > Dim wrdDoc As Word.Document
| >
| > Set wrdApp = New Word.Application
| > Set wrdDoc = wrdApp.Documents.Add
| >
| > wrdApp.Visible = True
| > wrdApp.Activate
| > wrdApp.WindowState = wdWindowStateMaximize
| >
| > With wrdApp.Selection
| > .TypeText "Hello!"
| > .InsertParagraph
| > .MoveDown
| > .TypeText "Hello Again!"
| > End With
| >
| > Set wrdDoc = Nothing
| > Set wrdApp = Nothing
| >
| >
| >
| > >-----Original Message-----
| > >I would like the user to be able to push a command button
| > that launches Word
| > >and then have the text of the Word document be filled
| > with the text present
| > >in a memo text box on the form. Something like this:
| > > Dim oApp As Object
| > >
| > > Set oApp = CreateObject("Word.Application")
| > >
| > > oApp.Visible = True
| > >
| > > oApp.Text = memCrisisPlan
| > >
| > > oApp.Body = memCrisisPlan
| > >
| > >However, neither of the last two statements work. Any
| > thoughts?
| > >
| > >Thanks,
| > >
| > >Dean Slindee
| > >
| > >
| > >.
| > >
|
|
|