Pass control name to Word

P

Peter Stone

Access 2003, XP
I am on a subform that opens a document in MS Word and renames it.

When Word is closing, I want to save the text that was entered in Word into
a memo field in Access.

I don't know how to pass the control name to MS Word and then save the text
to that control.

I don't want to use the mainform name so that the subform can be used in
multiple main forms.

***Here's the control:

frmMain!fsubPub1.Form!txtPub1

***Here's the code on the command button on the subform:

Private Sub cmdOpenPub1_Click()
On Error GoTo Err_cmdOpenPub1_Click

Dim oWord As Object
Dim oDoc As Object

'Create the Word application object
Set oWord = CreateObject("Word.Application")

If Me!txtPubLnk1 = "C:\t2hDoc\StartHere.Doc" Then
'Open a new document
Set oDoc = oWord.Documents.Add("C:\t2hDoc\StartHere.doc")

'Rename and save the new document
oWord.ActiveDocument.SaveAs Filename:="C:\t2hDoc\Folder\Another
Folder\etc.\" _
& Me.Parent!txtHd.Value & "_" & "Main".doc"

'Store the link to the new document
Me!txtPubLnk1 = "C:\t2hDoc\Folder\Another Folder\etc.\"
& Me.Parent!txtHd.Value & "_" & "Main".doc"
Me.Requery
Else
'Open the existing document
Set oDoc = oWord.Documents.Open(Me!txtPubLnk1.Value)
End If

'Display MS-Word and release the document object
oWord.Visible = True
Set oWord = Nothing

Exit_cmdOpenPub1_Click:
Exit Sub

Err_cmdOpenPub1_Click:
MsgBox Err.Description
Resume Exit_cmdOpenPub1_Click

End Sub

***Here's the code in MS Word that selects and copies the text:

Private Sub Document_Close()

Selection.WholeStory
Selection.Copy

End Sub

Thank you
Peter
 
P

Peter Stone

PieterLinden via AccessMonster.com said:
If you pass in the Word file you're opening, you can assign the value of
Selection.WholeStory to a String variable using a function...

Function GrabContents(byval doc as Word.document)
'open the word doc here
GrabContents = Selection.WholeStory
' close the doc
docWord.Close False

' then clean up word reference/close word if necessary
If boolWasAppRunning("Word") = False
appWord.Quit
End If
set appWord = nothing
End Function

Thanks Pieter

Sorry, I didn't make myself clear. After opening the Word document, the user
will enter or add text to the document. I want the text to be saved in Access
as they close Word.
 

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