Open a word doc from access that is mail merged to the database already

O

omar.norton

Dear all,

I am currently developing a database with customer details, and there
is a seperate word document that has address labels for each customer.
It is linked to the database by a standard mail merge so you can
choose which customer's address labels to display.

On the main menu form of my database, I have added a button to open
the address labels file using the following code:

Private Sub AddressLabels_Click()
Dim LWordDoc As String
Dim oApp As Object

'Path to the word document
LWordDoc = "G:\Shared\Templates & Forms\Labels\Address Labels.doc"

If Dir(LWordDoc) = "" Then
MsgBox "Document not found."

Else
'Create an instance of MS Word
Set oApp = CreateObject(Class:="Word.Application")
oApp.Visible = True

'Open the Document
oApp.Documents.Open FileName:=LWordDoc
End If

End Sub

This opens the document fine, but you can only view one mail merge
entry and all the mail merge controls on the word toolbar are greyed
out so you can't scroll through entries.

If you go into windows explorer and open the word file it works
absolutely fine, even if the database is running.

Does anybody know how to fix this problem? Thanks

(P.S My VBA knowledge is not huge!)
 
G

Guest

Omar,

Why not just use an Access Report rather than use a Word document for this
purpose? If you create a new report, the New Report dialog box gives you an
option for the "Label Wizard", select that option and you are on a roll.

One of the things I like about using the Access report is that, with some
additional coding, you can actually identify the first label you want to
print to, so you can use partial sheets of labels that have already been
used. I also find it easier to format the label using Access than using Word.

Dale
 
K

krissco

Dear all,

I am currently developing a database with customer details, and there
is a seperate word document that has address labels for each customer.
It is linked to the database by a standard mail merge so you can
choose which customer's address labels to display.

On the main menu form of my database, I have added a button to open
the address labels file using the following code:

Private Sub AddressLabels_Click()
Dim LWordDoc As String
Dim oApp As Object

'Path to the word document
LWordDoc = "G:\Shared\Templates & Forms\Labels\Address Labels.doc"

If Dir(LWordDoc) = "" Then
MsgBox "Document not found."

Else
'Create an instance of MS Word
Set oApp = CreateObject(Class:="Word.Application")
oApp.Visible = True

'Open the Document
oApp.Documents.Open FileName:=LWordDoc
End If

End Sub

This opens the document fine, but you can only view one mail merge
entry and all the mail merge controls on the word toolbar are greyed
out so you can't scroll through entries.

If you go into windows explorer and open the word file it works
absolutely fine, even if the database is running.

Does anybody know how to fix this problem? Thanks

(P.S My VBA knowledge is not huge!)

Try this function. It has the option of displaying ONLY the merged
document (and not the before and after like a "do-it-yourself" merge.
Some of the code is application specific, but I'm sure you'll be able
to weed it out.

'Open the awards report with the parameter name, query, and option to
hide or show original document
Public Function openAwardsReport(reportName As String, qryName As
String, Optional hideOriginal As Boolean = True)
On Error GoTo ExitError

'Display status to user
SysCmd acSysCmdSetStatus, "Automating Microsoft Word . . ."
DoCmd.Hourglass True

'Declare variables
Dim wApp As New Word.Application
Dim wDoc As Word.Document
Dim wMerge As Word.MailMerge

'Open the word document
Set wDoc = wApp.Documents.Open(getAwardsPath() & reportName &
".doc", , True, False, , , True, , , , , True)

'Modify merge properties to this database, using the query
specified.
Set wMerge = wDoc.MailMerge
With wMerge
.OpenDataSource CurrentProject.FullName, , , False, True,
False, , , True, , , "Query " & qryName
.Execute
End With

'There is no need to view the original merge document, close it
without saving
If hideOriginal Then wDoc.Close wdDoNotSaveChanges

'Make word visible
wApp.Visible = True

ExitNormal:
SysCmd acSysCmdClearStatus
DoCmd.Hourglass False
Exit Function

ExitError:
Select Case Err.Number
Case 5631 'No records - merge failed
MsgBox "No Award Winners For Report " & reportName & ".",
vbExclamation, "No Winners"
wApp.Quit wdDoNotSaveChanges
Set wApp = Nothing
Case Else
MsgBox Err.Number & " unhandled error " & Err.description
End Select
Resume ExitNormal
End Function

'The place where we store our .doc files
Public Function getAwardsPath() As String
getAwardsPath = CurrentProject.Path & "\AwardsReports\"
End Function

-Kris
 
K

krissco

Dim oApp As Object
Set oApp = CreateObject(Class:="Word.Application")

I forgot to mention, you will need to set a reference to the word
objects in order to use my code. You could modify it for late binding
instead. The choice is yours.

-Kris
 
O

omar.norton

Omar,

Why not just use an Access Report rather than use a Word document for this
purpose? If you create a new report, the New Report dialog box gives you an
option for the "Label Wizard", select that option and you are on a roll.

One of the things I like about using the Access report is that, with some
additional coding, you can actually identify the first label you want to
print to, so you can use partial sheets of labels that have already been
used. I also find it easier to format the label using Access than using Word.

Dale
--
Email address is not valid.
Please reply to newsgroup only.

















- Show quoted text -

Thanks everyone - I went for the easy option in the end and created a
report - why did I never think of that? The easiest option is always
the one you miss first!
 
G

Guest

Hi, I was researching a problem and found your code here.

My database is accessed through a shortcut that uses a .mdw file to open a
..mde file. How do I incorporate the security workgroup and password stuff
into the .OpenDataSource code line?

Thanks,
Lesli
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top