Word Macro - Merge, Protect, Email, Save

Hello everyone,

I'm currently trying to piece together a Word macro to achieve the following.

I have an Excel spreadsheet that contains my merge data and a Word document which is a single page form that the data merges into.

Step by step, record by record I would like to:

1. Merge a record into the form

2. Password protect the entire document to prevent editing

3. Allow certain areas of the document to be edited by using pre-defined bookmarks on the document

4. Email the form as an attachment using an email address from the current record but be able to add some pre-defined text into the body of the email

5. Save the form into a folder

6. Loop the above until all records have been merged, protected, emailed and saved

So far I have a macro that does everything perfectly aside from step 4, I'd be grateful if anyone could assist me with adding the email attachment process defined in step 4 :

Sub ProtectSaveEmail()
'
' MergeProtectEmailSave Macro
'
'
Dim i As Long
i = ActiveDocument.MailMerge.DataSource.RecordCount
ActiveDocument.MailMerge.DataSource.ActiveRecord = wdFirstRecord
Do While intCounter < i
intCounter = intCounter + 1
        ' Copy the active record
        ActiveDocument.Range.Copy
        ' Create a new document and paste the record
        Documents.Add
        Selection.Paste
        ' Set the open directory where the file is to be saved
        ChangeFileOpenDirectory _
        "D:\Merged Forms"
        ' Remove the mail merge from the document
        ActiveDocument.MailMerge.MainDocumentType = wdNotAMergeDocument
        ' Select all and replace all fields with their text values
        Selection.WholeStory
        Selection.Fields.Unlink
        ' Restrict editing of the document
        ActiveDocument.Protect Password:="a-password-goes-here", NoReset:=False, Type:= _
        wdAllowOnlyReading, UseIRM:=False, EnforceStyleLock:=False
        ' Allow editing of 6 fields in the document by using pre-defined bookmarks
        ActiveDocument.Bookmarks("EDIT1").Select
        Selection.Range.Editors.Add wdEditorEveryone
        ActiveDocument.Bookmarks("EDIT2").Select
        Selection.Range.Editors.Add wdEditorEveryone
        ActiveDocument.Bookmarks("EDIT3").Select
        Selection.Range.Editors.Add wdEditorEveryone
        ActiveDocument.Bookmarks("EDIT4").Select
        Selection.Range.Editors.Add wdEditorEveryone
        ActiveDocument.Bookmarks("EDIT5").Select
        Selection.Range.Editors.Add wdEditorEveryone
        ActiveDocument.Bookmarks("EDIT6").Select
        Selection.Range.Editors.Add wdEditorEveryone

        ' Save the file using bookmark names within the file name
        ActiveDocument.SaveAs FileName:="Requirements - " & _
        ActiveDocument.Bookmarks("PERSON").Range.Text & " - " & _
        ActiveDocument.Bookmarks("COMPANY").Range.Text & ".docx"
        ' Close the document
        ActiveDocument.Close

ActiveDocument.MailMerge.DataSource.ActiveRecord = wdNextRecord

Loop
End Sub

Many thanks in advance,

Ben

You could use the Many To One mail merge add-in. One of its functions is to combine protected legacy form fields with merge data and it will merge to e-mail with or without a personalized covering message.
Graham Mayor (Microsoft Word MVP 2002-2019)
For more Word tips and downloads visit my web site
https://www.gmayor.com/Word_pages.htm

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

Hi Graham,

Thanks for the speedy response!

I did initially look at Many To One but seemed to hit a few issues, this is going back a month or so now so I can't quite recall what they were but I seem to remember thinking that it might have been caused by using Windows 7 x64 and Office 2013 x64 ...

Maybe I missed something though, should Many To One work with that configuration? If so I'll revisit its use. 

All the best,

Ben

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

It has not been tested with the 64 bit version of Office, but lots of add-ins don't work with that version and Microsoft has suggested that for most users the 32 bit version is more appropriate.
Graham Mayor (Microsoft Word MVP 2002-2019)
For more Word tips and downloads visit my web site
https://www.gmayor.com/Word_pages.htm

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

Hi Graham,

I shall see if I can get the 64 bit version of Office replaced with the 32 bit version.

One last thing, I'm afraid I can't ascertain from the Many To One instructions if the resulting document will be completely locked/protected or if I'll still be able to lock/protect the document aside from the 6 fields where I'd like the recipient to enter freeform data before returning the document to me. Just want to check this is possible before uninstalling/reinstalling Office.

On another note, I see from your website that you live on the stunningly beautiful island of Cyprus! I'm very envious, I have some Cypriot friends who live just outside Limassol and have been to visit them many times over the years, on my last trip I visited a winery which I think is close to where you live, Vouni Panayia, I still have a bottle of Plakota from there which I must get around to enjoying! I also got a taste for Soutzoukos during my last visit and now regularly receive small packages of it from my Cypriot friends, delicious stuff!

All the best,

Ben

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

The finished merge documents will obey the same rules as any other protected form. If you put the form fields in a separate section from the free text area of the document and protect only the section containing the form fields, when you complete the merge the protected section will be locked the rest will not. Add the form fields first, set the sections as required, then unlock the form and add the data source and set the merge type to letters. With the form still unlocked run the add-in, which will prompt you for the password.

Cyprus is indeed a pleasant place to live. I have been here on and off for 25 years. The winery you mention is not that close, but the owners have a shop in town :). Enjoy your next visit.

Graham Mayor (Microsoft Word MVP 2002-2019)
For more Word tips and downloads visit my web site
https://www.gmayor.com/Word_pages.htm

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

 
 

Question Info


Last updated October 5, 2021 Views 1,298 Applies to: