How to load Word document with form text

  • Thread starter Thread starter Dean Slindee
  • Start date Start date
D

Dean Slindee

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
 
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
 
Dean Slindee said:
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?

Your first three lines of code are fine. They create an object oApp as a
running copy of Word.

But Word (like Access) has its own set of methods & properties. ".Text" and
".Body" are not valid method or property names for the Word object.

So you need to read the Word VBA online help to understand the Word object
model.

I think (from memory) that what you need is this - but I do not have Word
here to check:

oApp.activedocument.inserttext memCrisisPlan

You do not need a referece to the Word object library, to do what you want.

HTH,
TC
 
Hi Sean,

Dan posted a great automation sample, if you have anything unclear or
question, please feel free to rely to the threads.

For more about automation, we can download the help file from the support
site, the link is in the article below.

OFF2000: Microsoft Office 2000 Automation Help File Available [of20]
http://support.microsoft.com/support/kb/articles/q260/4/10.asp



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 load Word document with form text
|
| 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
|
|
|
 
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
 
First question is answered by using this line of code instead of
..InsertParagraph:
wrdApp.ActiveDocument.Content = strCrisisPlan
Cursor is placed at the start of the document.
 
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
| > >
| > >
| > >.
| > >
|
|
|
 
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,

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.

By the way, since you have started a new thread for this question, I will
follow up any question in the new thread to make the discussion more
clearly.

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
Newsgroups: microsoft.public.access.formscoding

If you have any concerns or questions, please feel free to reply to the new
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.
 
Back
Top