Opening an MSWord template from MSAccess.

THAWK

New member
Local time
Today, 02:43
Joined
Jul 2, 2008
Messages
6
I need to create a new document from an exisiting MSWord template but I want to access it via MSAccess.

I have tried the below code and also in desperation tried a hyperlink to the address, but both open the template file itself instaed of creating a new document from the file.

Can anyone advise how I do this.

:confused:

Private Sub More_Info_Letter_Click()
Dim MoreInfoLetter As String
Dim oApp As Object

MoreInfoLetter = "G:\Extra Info Letters.dot"

Set oApp = CreateObject(Class:="Word.Application")
oApp.Visible = True
oApp.Documents.OPEN FileName:=MoreInfoLetter

End Sub
 
I need to create a new document from an exisiting MSWord template but I want to access it via MSAccess.

I have tried the below code and also in desperation tried a hyperlink to the address, but both open the template file itself instaed of creating a new document from the file.

Can anyone advise how I do this.

:confused:

Private Sub More_Info_Letter_Click()
Dim MoreInfoLetter As String
Dim oApp As Object

MoreInfoLetter = "G:\Extra Info Letters.dot"

Set oApp = CreateObject(Class:="Word.Application")
oApp.Visible = True
oApp.Documents.OPEN FileName:=MoreInfoLetter

End Sub
You need code in the document_open routine to run the template, or I think you can also save the dot in the Word template location - although I haven't tried that

I do this in 2007, I do a merge then copy to a new doc.
Good luck
 
look up super easy word

this is a mailmerge fuction and does this - easy to handle easy to add new templates ..

any problems ping the forum -someone will have the link

note - the merge works off 1table/form so you may have to create a table/form with the data from other tables (temp tables)

hth
 
Fantastic solution thanks.

Especially useful as I have a number of users and I can use your solution to create different templates.
:)
 
One of my old databases allowed users to open a template in Word, which would then auto-populate Word bookmarks, based on the information in the form.

You're more than welcome to poach my old code if it will suit your needs. You just need to alter the bits in red to your individual requirements. If you don't need bookmark replacement, just chop out everything from the With WordApp.Selection to the End With section.

Code:
' Create a Word document from template.
   Dim WordApp As Word.Application
   Dim strTemplateLocation As String
  
   ' Specify location of template
   strTemplateLocation = "[COLOR="Red"]WHERE YOUR TEMPLATE IS LOCATED\template.dot[/COLOR]"
    
    
   On Error Resume Next
   Set WordApp = GetObject(, "Word.Application")
   If Err.Number <> 0 Then
     Set WordApp = CreateObject("Word.Application")
   End If
   On Error GoTo ErrHandler
   
   
   WordApp.Visible = True
   WordApp.WindowState = wdWindowStateMaximize
   WordApp.Documents.Add Template:=strTemplateLocation, NewTemplate:=False
    
   ' Replace each bookmark with field contents.
   With WordApp.Selection

     .GoTo what:=wdGoToBookmark, Name:="[COLOR="Red"]NAME OF YOUR WORD TEMPLATE BOOKMARK[/COLOR]"
     If IsNull([COLOR="Red"]NAME OF DATABASE FIELD TO POPULATE BOOKMARK WITH[/COLOR]) Then
       .TypeText "N/A"
     Else
       .TypeText [[COLOR="Red"]NAME OF DATABASE FIELD TO POPULATE BOOKMARK WITH[/COLOR]]
     End If
   
   End With
    
   DoEvents
   WordApp.Activate
    
   Set WordApp = Nothing

ErrHandler:
Set WordApp = Nothing

I also have a slightly expanded version elsewhere in that same database, that does the same auto-population, but then auto-saves the Word document that has just been created. Let me know if you need it.
 

Users who are viewing this thread

Back
Top Bottom