Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

mail merge with vba to input own merge document location?

Posted on 2004-08-03
2
Medium Priority
?
1,011 Views
Last Modified: 2008-01-09
Hi everyone I have a command button which when clicked, a mail merge using a word template occurs, the only thing is the code which is set for the command button has the default path of were the location of the word mail merge doc is,however if for sum reason the location was changed it would not find it, is there anyway it can look up a value in a text box and use that value? also I would need it to have liek a history list of the locations?

here is the code for the command button:

Public Sub sbMergeIt()

    Dim strPath As String

    'Create the object variables need
    'Make sure there is a reference to MS Word
    Dim objWordDoc As Word.Document
    Dim objWordApp As Word.Application

    'Set the variable that holds the path to the files used in the procedure
    'Change it to match your path \\Stealth\DB_BACKEND\
    'strPath = "C:\Documents and Settings\Administrator\My Documents\Access Databases\"
    strPath = "\\Stealth\DB_BACKEND\"

    'Export the data from the selected query.  Notice the file name is not the same as the word document we are trying to merge it with
    DoCmd.TransferText acExportMerge, , Me.cboSelectMailOut, strPath & "MailOutMergeData.txt", True
   
   'Create our object variable
    Set objWordApp = CreateObject("Word.Application")
   
    'Open the word document
    Set objWordDoc = objWordApp.Documents.Open(strPath & "MailOutList.doc")
   
   
    objWordApp.ActiveDocument.MailMerge.OpenDataSource Name:=strPath & "MailOutMergeData.txt", _
        ConfirmConversions:=False, ReadOnly:=False, LinkToSource:=True, _
        AddToRecentFiles:=False, PasswordDocument:="", PasswordTemplate:="", _
        WritePasswordDocument:="", WritePasswordTemplate:="", Revert:=False, _
        Format:=wdOpenFormatAuto, Connection:="", SQLStatement:="", SQLStatement1:=""

    'Used to refresh the data in the document
    'We have seen that word would remember the previous information
    'and if we didn't toggle you could get the data from the
    'previous letter
    objWordApp.ActiveDocument.MailMerge.ViewMailMergeFieldCodes = wdToggle
    objWordApp.ActiveDocument.MailMerge.ViewMailMergeFieldCodes = wdToggle
   
    'Send the letters to the printers merged with the records
    objWordApp.ActiveDocument.MailMerge.Destination = wdSendToPrinter
   
    'If you want it to be automatic, then uncomment the next 3 lines and comment the visible line
    objWordApp.Visible = True
    objWordApp.ActiveDocument.MailMerge.Execute
    objWordApp.Quit wdDoNotSaveChanges
    Set objWordApp = Nothing
   
End Sub

Private Sub cmd_PrevMail_Click()

    Call sbMergeIt
    MsgBox "List exported to printer"
   
End Sub

this is the line were it gets the location:

    strPath = "\\Stealth\DB_BACKEND\"

please let me know thanks.
0
Comment
Question by:eazy123
[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
2 Comments
 
LVL 34

Expert Comment

by:flavo
ID: 11701448
You can do a test to see if it does exist

Function pathExists(sPath As String) As Boolean

If Len(Dir(sPath)) > 0 Then
     'it exists
    pathExists = True
Else
     'it doesn't exist
    pathExists = False
End If

End Function

then maybe you could use this (it will look for the directory using the function above, if it doesnt exist, it will do a pop-up box asking for it)

If Not pathExists(strPath) Then
    strPath = cStr(InputBox("Enter the path"))
End if

Dave
0
 
LVL 26

Accepted Solution

by:
dannywareham earned 750 total points
ID: 11701544
If you want a history, you could also simply bind a combobox to a table with a list of locations.
When your code returns the location, this populates the box and is stored in the table.
Then, rather than using the inputbox, you can simply select a value from teh dropdown combo or enter a new one..

:-)
0

Featured Post

Technology Partners: 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!

Question has a verified solution.

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

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.
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
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…

722 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