Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2006-07-23
6
Medium Priority
?
392 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 2000 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
Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

 
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

The Windows Phone Theme Colours is a tight, powerful, and well balanced palette. This tiny Access application makes it a snap to select and pick a value. And it doubles as an intro to implementing WithEvents, one of Access' hidden gems.
This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
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…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…
Suggested Courses

810 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