Solved

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

Posted on 2006-07-23
6
386 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

Question has a verified solution.

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

Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
Access custom database properties are useful for storing miscellaneous bits of information in a format that persists through database closing and reopening.  This article shows how to create and use them.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…

696 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