Creating Word documents from Excel VBA

dhancy

Board Regular
Joined
Jul 10, 2013
Messages
123
Office Version
  1. 365
Platform
  1. Windows
Hello,

I have an Excel VBA script that creates multiple Word documents. I've been using it for a few years and it's been working great. I have roughly 350 files. The script used to create the Word documents and save them as PDFs in about 45 minutes.

In the last few months, it's slowed down considerably. Now it takes more than two hours.

I'm at a loss as to what changed. The only thing I can think of is that our organization upgraded to Office 2016 from Office 2013 over the summer. Could that be causing the problem?

Here is the code I am using to set this up:

Code:
    Dim wrdApp As Word.Application
    Dim wrdDoc As Word.Document


    Set wrdApp = CreateObject("Word.Application")
    wrdApp.Visible = False                                      
            
    Set wrdDoc = wrdApp.Documents.Add(Template:=folderTemp & "\DD4.dotx")

Is there anything in there that needs to change for 2016? Or can you think of something else that might be causing the slow down? I appreciate any insight on this.

Thanks!

Dennis
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
To my knowledge this has nothing to do with your version but free RAM.

You can export a range or a sheet directly to pdf which I believe is quicker (quite instantenuous)

Code:
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= "C:\Users\YourName\Documents\Name_of_the_file.pdf", Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:= False
 
Last edited:
Upvote 0
Office 2016 is much slower than 2013. It takes a longggg time to load. If U can, just open Word once and leave it loaded until your done using the wb. HTH. Dave
 
Upvote 0
Interesting.. thanks!

Looking at my code, I do see that I am doing "CreateObject("Word.Application")" in the middle of the loop that's going through all 350+ files.

I am leaving for the day soon, but I will definitely look into changing the logic so that it only opens Word once.

Thanks for the idea. i will let you know how this goes.


Dennis
 
Upvote 0
Looking at my code, I do see that I am doing "CreateObject("Word.Application")" in the middle of the loop that's going through all 350+ files.
In which case you're creating 350+ instances of Word. Aside from the fact you don't need Word for what you're doing, as Kamolga says, with your present code you should instantiate Word (wrdApp) once - outside the loop. Indeed, depending on what you're doing with the Word document(s) inside the loop, you may even be able to instantiate wrdDoc once outside the loop also.
 
Upvote 0
So, I made the change in my code so that I am only instantiating wrdApp once. I then did a before and after comparison on a sample of five Word documents.

Before: 15 minutes, 19 seconds
After: 9 minutes, 6 seconds

If my math is correct, that is 40% faster. This is awesome!

I can't believe I didn't see this before. Thanks to everyone who responded.


Dennis
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,175
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top