String as dynamic value [MailMerge]

Mike2502

Board Regular
Joined
Jan 19, 2020
Messages
139
Office Version
  1. 2010
Hi All,

I'm trying to have a variable for Column D and Row X however I'm unsure of how to go about this. As the value in Column D Row X will be based on each row it can be either ABC or XYZ.

Dependent on the value in each column the mail merge will use a different document for each rows mail merge.

At the moment I have the below but unsure how to get it as dynamic :/ !

VBA Code:
    'Mailmerge for Sheet1
    Dim wdc
    Dim xc
    Dim ContractType As Range
    Dim RowTarget1 As Long

    Dim X as integer

    Dim Worksheet As Worksheet
    Set Worksheet= ActiveSheet

    Set ContractType = ActiveSheet.Cells(x, 4)
    X = 2
    
    Set wdc = CreateObject("word.application")
    wdc.Application.documents.Open "\Templates\" & ContractType & ".docm"
    
    wdc.Application.Visible = False
    Application.ScreenUpdating = False
    wdc.Application.Run "Template1"
    Set wdc = Nothing
    
    MsgBox ("Mail Merge Completed")
    Application.ScreenUpdating = True
Any help is appreciated - thanks
 

Some videos you may like

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)

Mike2502

Board Regular
Joined
Jan 19, 2020
Messages
139
Office Version
  1. 2010
UPDATE - I keep getting an error I believe this would be workable?

VBA Code:
Sub MailMerge()
    'Mailmerge for Sheet1
    Dim wdc
    Dim xc
    Dim ContractType As Range
    Dim wsWorksheet As Worksheet
    Set wsWorksheet = Sheets("Data")
    
    With wsWorksheet
    
    Set ContractType = Range("D" & Rows.Count).End(xlUp).Select

    Set wdc = CreateObject("word.application")
    wdc.Application.documents.Open "Location" & ContractType & ".docm"
    
    wdc.Application.Visible = False
    Application.ScreenUpdating = False
    wdc.Application.Run "Template"
    Set wdc = Nothing
    
    MsgBox ("Mail Merge Completed")
    Application.ScreenUpdating = True
    End With
End Sub
 

Watch MrExcel Video

Forum statistics

Threads
1,127,318
Messages
5,623,974
Members
416,002
Latest member
Neshx

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top