abinboston
asked on
Access Data to MS Word .dot - Losing Bookmarks/Form Fields
I am using this code to insert data from an Acces form to a Word template
It works - but, when closing Word, the user is prompted with
Do you want to save the changes you made to the Word document
If yes is selected - the next time the Word document is opened, I get an error because the Bookmarks to the form fields are gone...
I know that if you do not save the changes, or save the document as another name, it will work..but users are not so great at following rules...
How do I prevent this? And Keep the Bookmarks/Form Fields
Thanks -AB
Dim objWord As Object
Set objWord = CreateObject("Word.Applica tion")
objWord.Visible = True
With objWord
' Make the application visible.
.Visible = True
' Open the document.
.Documents.Open ("c:\mymerge.dot")
.ActiveDocument.FormFields ("FirstNam e").Select
' Move to each bookmark and insert text from the form.
.Selection.TypeText Text:=(CStr(Forms!Employee s!FirstNam e))
'.ActiveDocument.FormField s("Last"). Select
' Move to each bookmark and insert text from the form.
' .Selection.TypeText Text:=(CStr(Forms!Clients! LastName))
End With
End Sub
It works - but, when closing Word, the user is prompted with
Do you want to save the changes you made to the Word document
If yes is selected - the next time the Word document is opened, I get an error because the Bookmarks to the form fields are gone...
I know that if you do not save the changes, or save the document as another name, it will work..but users are not so great at following rules...
How do I prevent this? And Keep the Bookmarks/Form Fields
Thanks -AB
Dim objWord As Object
Set objWord = CreateObject("Word.Applica
objWord.Visible = True
With objWord
' Make the application visible.
.Visible = True
' Open the document.
.Documents.Open ("c:\mymerge.dot")
.ActiveDocument.FormFields
' Move to each bookmark and insert text from the form.
.Selection.TypeText Text:=(CStr(Forms!Employee
'.ActiveDocument.FormField
' Move to each bookmark and insert text from the form.
' .Selection.TypeText Text:=(CStr(Forms!Clients!
End With
End Sub
ASKER
Oops- there is one more comment -
Once the data is written to the Bookmarks, I need to leave Word Open so the User can add more text to the document if desired. the User can save/print as desired
I don't know if this matters or not...
Thanks! - AB
Once the data is written to the Bookmarks, I need to leave Word Open so the User can add more text to the document if desired. the User can save/print as desired
I don't know if this matters or not...
Thanks! - AB
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Initially... Set ObjWord = CreateObject("Word.Applica tion")
Try using the Documents.Add( {filename of template here} )
Then at the end, disconnect Word from Access so that the user can review it...
ObjWord.Visible = True
If ObjWord.WindowState <> wdWindowStateMaximize Then
ObjWord.WindowState = wdWindowStateMaximize
End If
Set ObjWord = Nothing
Try using the Documents.Add( {filename of template here} )
Then at the end, disconnect Word from Access so that the user can review it...
ObjWord.Visible = True
If ObjWord.WindowState <> wdWindowStateMaximize Then
ObjWord.WindowState = wdWindowStateMaximize
End If
Set ObjWord = Nothing
...as I think doing the .OPEN move just opens up the TEMPLATE, whereas the .ADD uses the TEMPLATE to create the DOCUMENT.
You should see that it is a DOC in the titlebar instead of a DOT (as you don't want to change your templates).
You should see that it is a DOC in the titlebar instead of a DOT (as you don't want to change your templates).
so may ways to do this ... I'll add another ...
I make a copy of the doc and open the new copy for populating ...
FileCopy "C:\Templates\MyDoc.doc", CurrentProject.Path & "\MyNewDoc" & Format(Now(), "yyyymmddhhnnss") & ".doc"
I make a copy of the doc and open the new copy for populating ...
FileCopy "C:\Templates\MyDoc.doc", CurrentProject.Path & "\MyNewDoc" & Format(Now(), "yyyymmddhhnnss") & ".doc"
ASKER
What is the difference between using a .dot, and a .doc in this situation?
Thanks - AB