Change datasource of MS Word document on load and on button VBA.

I need to set the datasource for a set of mail merge documents to deploy to users on and off the LAN and may have updates each month.

To save remoting and resetting up datasources etc... (and also guessing the location) I want to set the datasource location on load or via button.. probably both.

Will use a specifically named file in the downloads directory of the current user.

Please supply a routine that will change the datasource. Please put XYZ as the path and I will generate etc...

The experience is:

User opens the document... it does it then and then merges... AND .. can be done via button to do an update.

OK.. seems as though this is a starting point:

    With ActiveDocument.MailMerge

        .OpenDataSource Name:="c:\mydocs\contracts\MyCont_R.CSV"
        .Execute
    End With

Open in new window

And . Execute if you want to mail merge as part of the routine

Any additional suggestions?

R
LVL 7
RomoloIT Pro, Projects, Mentor, TrainerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

RomoloIT Pro, Projects, Mentor, TrainerAuthor Commented:
OK.. seems as though this works:

    With ActiveDocument.MailMerge

        .OpenDataSource Name:="c:\mydocs\contracts\MyCont_R.CSV"
        .Execute
    End With

Open in new window

And . Execute if you want to mail merge as part of the routine

Any advances?

R
Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
You may try placing the following code on the ThisDocument Module...

Private Sub Document_Open()
Dim strPath As String, strFileName As String

'Get the downloads folder path
strPath = Environ("UserProfile") & "\Downloads\"

'CSV file name
strFileName = "MyCont_R.CSV"

With ActiveDocument.MailMerge
    .OpenDataSource Name:=strPath & strFileName
    .Execute
End With
End Sub

Open in new window

RomoloIT Pro, Projects, Mentor, TrainerAuthor Commented:
With ActiveDocument.MailMerge

        .OpenDataSource Name:="c:\mydocs\contracts\MyCont_R.CSV"
        .Execute
    End With

As above.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VBA

From novice to tech pro — start learning today.