Solved

Access Data to MS Word .dot - Losing Bookmarks/Form Fields

Posted on 2006-07-23
6
380 Views
Last Modified: 2012-06-21
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
0
Comment
Question by:abinboston
6 Comments
 

Author Comment

by:abinboston
ID: 17165027
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
0
 

Author Comment

by:abinboston
ID: 17165036
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
0
 
LVL 65

Accepted Solution

by:
rockiroads earned 500 total points
ID: 17165570
Why, when u first open the word doc

Documents.Open ("c:\mymerge.dot")


perform a save then. Use a temp windows filename
here is code to do that, simply pass in suffix

Function MakeTempFileName(Extension As String) As String

   Dim Isfile As Integer, FHandle As Integer, Cntr As Integer
   Dim WinTemp As String, TF As String

   Isfile = False
   FHandle = FreeFile

   WinTemp = Environ("TEMP") & "\"
   For Cntr = 1 To 8
      WinTemp = WinTemp & Mid(LTrim(Str(CInt(Rnd * 10))), 1, 1)
   Next Cntr

   TF = Trim(WinTemp$) & "." & Extension

   Open TF For Output As #Fhandle
   Debug.Print TF
   Print #FHandle, "This is a Temp file"
     
   Close #FHandle
   MakeTempFileName = TF

End Function



dim sTmpFile as string

sTmpFile = MakeTempFileName("DOC")

Now u can save to sTmpFile

Now continue with the rest of your code

All changes are now made to this temp doc
I dont know if mailmerge.dot is still open but I guess u could close that
When u exit now, it should prompt to save but will overwrite temp one. Users will then learn to do SaveAs


0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 2

Expert Comment

by:rogerhammond
ID: 17165698
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
0
 
LVL 2

Expert Comment

by:rogerhammond
ID: 17165703
...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).
0
 
LVL 39

Expert Comment

by:stevbe
ID: 17167074
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"
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

The first two articles in this short series — Using a Criteria Form to Filter Records (http://www.experts-exchange.com/A_6069.html) and Building a Custom Filter (http://www.experts-exchange.com/A_6070.html) — discuss in some detail how a form can be…
In Debugging – Part 1, you learned the basics of the debugging process. You learned how to avoid bugs, as well as how to utilize the Immediate window in the debugging process. This article takes things to the next level by showing you how you can us…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now