PDA

View Full Version : Extracting data from excel to word



hemdeep
01-22-2016, 01:11 AM
Hi Guys,

I have created a very simple macro, which copies each line from Sheet: Data to Sheet:Template, one at a time.
The macro then copies all the data from Sheet:Template to word document.
The macro runs fine on certain occasions - (refer "desired output" word file) and does not run fine on some other occasions: refer "undesired output " word file. I desired output almost 50 % of the time that i run this macro
Can you help me to ensure that i get the desired output everytime i run this macro.

Any help will be appreciated.

Hem

gmayor
01-22-2016, 06:49 AM
I don't see why you don't use mail merge. The results will then be as good as the data. The attached document used as a directory merge document with your data attached should produce the required results if you merge to a new document.

hemdeep
01-24-2016, 10:57 PM
Thanks Graham.
Sure i can try doing mail merge. Meanwhile can you please tell me whats wrong with my current macro code?

gmayor
01-25-2016, 02:59 AM
Where do I begin? You have two different Word applications declared, several undeclared variables and an assortment of copy and paste exercises that are neither necessary nor desirable, when you can simply reproduce ranges directly. It is also not clear what the filename 'incrementation' is supposed to achieve. See attached.

Your code also requires a reference to Word 8. The attached will work with whatever Word version is installed and does not require a VBA reference to Word.
The filenames will now increment properly (in the same folder as the workbook, though you can change that in the code by changing the value of strPath).

hemdeep
01-25-2016, 04:22 AM
Thank you so much Graham. I have tested this and this works absolutely perfectly. :)