View Single Post
 
Old 05-04-2020, 05:52 AM
kuankailok kuankailok is offline Windows 10 Office 2016
Novice
 
Join Date: Apr 2020
Posts: 2
kuankailok is on a distinguished road
Default

Code:
Dim WithEvents wdapp As Application

Dim EMAIL_SUBJECT As String

Dim FIRST_RECORD As Boolean





Private Sub Document_Open()



    Set wdapp = Application

    ThisDocument.MailMerge.ShowWizard 1

   

End Sub



Private Sub Document_Close()



    Set wdapp = Nothing

    

End Sub





Private Sub wdapp_MailMergeBeforeRecordMerge(ByVal Doc As Document, Cancel As Boolean)

    Dim i As Integer

    

    With ActiveDocument.MailMerge

    

        If FIRST_RECORD = True Then 

            EMAIL_SUBJECT = .MailSubject

            FIRST_RECORD = False

        Else .MailSubject = EMAIL_SUBJECT

        End If



        i = .DataSource.DataFields.Count

        

        Do While i > 0

            .MailSubject = Replace(.MailSubject, "<" & .DataSource.DataFields(i).Name & ">", .DataSource.DataFields(i).Value, , , vbTextCompare)

            i = i - 1

        Loop



    End With

    

     

End Sub





Private Sub wdapp_MailMergeBeforeMerge(ByVal Doc As Document, ByVal StartRecord As Long, ByVal EndRecord As Long, Cancel As Boolean)



    FIRST_RECORD = True

    

End Sub



Private Sub wdapp_MailMergeAfterMerge(ByVal Doc As Document, ByVal DocResult As Document)



    ActiveDocument.MailMerge.MailSubject = EMAIL_SUBJECT

     

End Sub
This is the code i'm using to obtain a dynamic subject, however, looks like i cannot put it in the right position? Could you please help?

Additionally, do i need to change anything in your provided code (as i saw there is a ".Cell(j,i)".

Thanks for your help anyway

*** if needed, i could put my sample doc here for your kind checking.... my purpose is to send out email by mail merge with a dynamic subject while copying the right personnel in each individual email, i know that's crazy but wanna complete this in a very short time....typing out each email takes time............

Last edited by kuankailok; 05-04-2020 at 05:54 AM. Reason: added some more comments
Reply With Quote