Link to home
Start Free TrialLog in
Avatar of ROM
ROMFlag for United Kingdom of Great Britain and Northern Ireland

asked on

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
Avatar of ROM
ROM
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

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
Avatar of Subodh Tiwari (Neeraj)
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

ASKER CERTIFIED SOLUTION
Avatar of ROM
ROM
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial