Solved

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

Posted on 2006-07-23
6
384 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
In earlier versions of Windows (XP and before), you could drag a database to the taskbar, where it would appear as a taskbar icon to open that database.  This article shows how to recreate this functionality in Windows 7 through 10.
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

749 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