Link to home
Start Free TrialLog in
Avatar of abinboston
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.Application")
    objWord.Visible = True
   
     With objWord
            ' Make the application visible.
            .Visible = True
            ' Open the document.
           .Documents.Open ("c:\mymerge.dot")
           .ActiveDocument.FormFields("FirstName").Select
            ' Move to each bookmark and insert text from the form.
            .Selection.TypeText Text:=(CStr(Forms!Employees!FirstName))
            '.ActiveDocument.FormFields("Last").Select
            ' Move to each bookmark and insert text from the form.
           ' .Selection.TypeText Text:=(CStr(Forms!Clients!LastName))
           End With
    End Sub
Avatar of abinboston
abinboston

ASKER

One more thing -I am using a template, but that is not really necessary - I can use either a .doc or a .dot - I just assumed that a template would not be able to be changed?- I was incorrect!

What is the difference between using a .dot, and a .doc in this situation?

Thanks - AB
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
ASKER CERTIFIED SOLUTION
Avatar of rockiroads
rockiroads
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Initially...    Set ObjWord = CreateObject("Word.Application")

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).
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"