Sunday, January 31, 2016

Macro to Split the Mail Merged Letters

Recently I came across this issue in my project. In our testing project, we follow a different Defect Management tool and the Development comapny using a different defect management system. Our tool can give all the defects in a Excel sheet. But the other party needs each individual defect in a single word file in their template.


So we got their template. And I have checked whether we have all the necessary fields in our excel file to fit in their Word Template. Except one field almost every items like Defect Description, Tester Name, Testd Date etc. So i used the mail merge option in Word and used the excel file as data source. Fitted in the necessary fields from Excel in the Word file. Used the mail merge wizard and generated all the defects in their template as a continuous document. But the developers need them in a separate files as incident logs. So i was searching for the options to split the files in MS Word itself. But there is no way. So i wanted to try the macro program. Fortunately i got my solution in Expers-Exchange.com The wollowing macro written by someone saved me a lot of time. I have created a new macro to my consolidated letters document and pasted the below macro program and tinkered a little bit to change the folder name (where to store the splitted files) and File name (name for my splitted files). Then I ran the macro, it did the magic of separating the consolidated files to separated files... Try it when u face this sort of solution.... Thanks to Expert-Exchange.com

Sub Split()
Dim mask As String
Selection.EndKey Unit:=wdStory
Letters = Selection.Information(wdActiveEndSectionNumber)
mask = "ddMMyy"
Selection.HomeKey Unit:=wdStory
Counter = 1
While Counter <> DocName = "C:\" & Format(Date, mask) & "_Letter_" & LTrim$(Str$(Counter)) & ".doc"
ActiveDocument.Sections.First.Range.Cut
Documents.Add
With Selection
.Paste
.EndKey
Unit:=wdStory
.MoveLeft
Unit:=wdCharacter, Count:=1
.Delete Unit:=wdCharacter, Count:=1
End With
ActiveDocument.SaveAs FileName:=DocName, FileFormat:=wdFormatDocument
ActiveWindow.Close
Counter = Counter + 1
Wend
End Sub


Source: http://techgurukul.blogspot.in/

2 comments:

Blogger Tips And Tricks|Latest Tips For Bloggers Free Backlinks