Pass control name from Access to Word and save text in the control

P

Peter Stone

I've posted this twice in Access forms coding and had no luck. Maybe someone
here can help me. Thanks.

Novice Word 2003, Access 2003, XP Pro

I have some four subforms each with a command button that opens a document
in MS Word and renames it.

The user then enters or adds text to the Word document. When they close
Word, I want to save the text in the Word document into a memo field in
Access.

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

I don't want to use the form names so that the subforms can be used in
multiple main forms.

***Here's the controls:

frmMain!fsubPub1.Form!txtPub1
frmMain!fsubPub2.Form!txtPub2
frmMain!fsubPub3.Form!txtPub3
frmMain!fsubPub4.Form!txtPub4

***Here's the code on the command button on one 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
CODE NEEDED HERE
'Display MS-Word and release the document object
oWord.Visible = True
Set oWord = Nothing

End Sub

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

Private Sub Document_Close()

Selection.WholeStory
Selection.Copy
CODE NEEDED HERE

End Sub

Thank you
Peter
 
D

Doug Robbins - Word MVP

Replace

CODE NEEDED HERE

With

frmMain!fsubPub1.Form!txtPub1.Value = oDoc.Range.Text

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
 
P

Peter Stone

Thanks Doug
The problem is I don't know the name of my mainform.

The path to the control is maybe:

txtPub1.Parent.Parent.Name!txtPub1.Parent.Name!txtPub1
or
txtPub1.Parent.Parent.Name!fsubPub1.Form!txtPub1
 
D

Doug Robbins - Word MVP

Try

Me.txtPub1.Value = oDoc.Range.Text

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
 
P

Peter Stone

This may be a way to pass the control name to Word

Dim sForm As String
sForm = Screen.ActiveForm.Name
Debug.Print Forms(sForm).Controls(fsubTxt1).Form.Controls(txtPub).Value
 

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