Export specific data to word files

YasserKhalil
PlatinumLounger
Posts: 4913
Joined: 31 Aug 2016, 09:02

Export specific data to word files

Post by YasserKhalil »

Hello everyone
I have excel file "Main Workbook" with some data and I need to export data according to the "City" field to word files .. A word file for each city
And I have a word document "Template" which I need to copy and fill the fields according to the excel file ...
I don't know about mail merge so I need a code that do that task .. if possible
Or if it is easier to do the task using Mail Merge, please guide me as a novice to do that task

Thanks advanced for great help
Regards
You do not have the required permissions to view the files attached to this post.

YasserKhalil
PlatinumLounger
Posts: 4913
Joined: 31 Aug 2016, 09:02

Re: Export specific data to word files

Post by YasserKhalil »

Hello
I have searched and found that code that may be a start point .. the code copies specific range and paste into the word document

Code: Select all

Sub Test()
    Dim WdObj As Object, fname As String

    fname = "Word"
    Set WdObj = CreateObject("Word.Application")
    WdObj.Visible = False
    Range("A1:B8").Copy
    WdObj.Documents.Open Filename:=ThisWorkbook.Path & "\Template.docx"
    WdObj.Selection.PasteSpecial Link:=False, DataType:=wdPasteText, Placement:=wdInLine, DisplayAsIcon:=False
    Application.CutCopyMode = False
    If fname <> "" Then
        With WdObj
            .ChangeFileOpenDirectory ThisWorkbook.Path & "\"
            .ActiveDocument.SaveAs Filename:=fname & ".docx"
        End With
    Else:
        MsgBox ("File Not Saved, Naming Range Was Botched, Guess Again.")
    End If

    With WdObj
        .ActiveDocument.Close
        .Quit
    End With
    Set WdObj = Nothing
End Sub
How can adapt this code to transfer specific fields to specific locations in word file? ...

User avatar
HansV
Administrator
Posts: 78474
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Export specific data to word files

Post by HansV »

You can use Graham Mayor's Word add-in Mail Merge Many-to-One for this.
Best wishes,
Hans

YasserKhalil
PlatinumLounger
Posts: 4913
Joined: 31 Aug 2016, 09:02

Re: Export specific data to word files

Post by YasserKhalil »

Thanks for the add-in
I have faced problems installing it as my antivirus see it is maleware and refused completely to continue ...
What is the other option can I take in that case?

User avatar
macropod
4StarLounger
Posts: 508
Joined: 17 Dec 2010, 03:14

Re: Export specific data to word files

Post by macropod »

You can use Word's Catalogue/Directory Mailmerge facility for this (the terminology depends on the Word version). To see how to do so with any mailmerge data source supported by Word, check out my Microsoft Word Catalogue/Directory Mailmerge Tutorial at:
http://windowssecrets.com/forums/showth ... e-Tutorial" onclick="window.open(this.href);return false;
or:
http://www.gmayor.com/Zips/Catalogue%20Mailmerge.zip" onclick="window.open(this.href);return false;
The tutorial covers everything from list creation to the insertion & calculation of values in multi-record tables in letters. Do read the tutorial before trying to use the mailmerge document included with it.

The field coding for this is complex. However, since the tutorial document includes working field codes for all of its examples, most of the hard work has already been done for you - you should be able to do little more than copy/paste the relevant field codes into your own mailmerge main document, substitute/insert your own field names and adjust the formatting to get the results you desire. For some worked examples, see the attachments to the posts at:
http://www.msofficeforums.com/mail-merg ... #post23345" onclick="window.open(this.href);return false;
http://www.msofficeforums.com/mail-merg ... #post30327" onclick="window.open(this.href);return false;
http://windowssecrets.com/forums/showth ... post928391" onclick="window.open(this.href);return false;

Another option would be to use a DATABASE field in a normal ‘letter’ mailmerge main document and a macro to drive the process. An example of this approach can be found at: http://answers.microsoft.com/en-us/offi ... 96c14dca5d" onclick="window.open(this.href);return false;
The DATABASE field can even be used without recourse to a mailmerge. An example of such usage can be found at: http://www.msofficeforums.com/mail-merg ... #post67097" onclick="window.open(this.href);return false;
Paul Edstein
[Fmr MS MVP - Word]

YasserKhalil
PlatinumLounger
Posts: 4913
Joined: 31 Aug 2016, 09:02

Re: Export specific data to word files

Post by YasserKhalil »

Thank you very much
I will try to study these links to see what I can do in that issue
Best Regards

User avatar
macropod
4StarLounger
Posts: 508
Joined: 17 Dec 2010, 03:14

Re: Export specific data to word files

Post by macropod »

Cross-posted at: http://www.msofficeforums.com/mail-merg ... y-one.html" onclick="window.open(this.href);return false;
For cross-posting etiquette, please read: http://www.excelguru.ca/content.php?184" onclick="window.open(this.href);return false;
Paul Edstein
[Fmr MS MVP - Word]