Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Images not pasting correctly in Word 2007 using Macro

Status
Not open for further replies.

MarcusESP79

Programmer
Mar 30, 2007
3
GB
Hi, this problem is driving me to despair.

I have a macro which picks up an individual Word document from a bespoke database system, it then copies the selection and pastes into a new document, then picks up the next Word doc and pastes to the end of the new doc and so on and so forth.

If I make the objword.visible = False then it flies through, BUT my images aren't appearing on the large document (just a box outline with a message saying that there are memory issues).

However, if I set Word to be visible it works for about half the images on the large doc (obviously depending how large the doc and/or image(s) is/are).

The only way I've found around this is to make the macro wait as follows:
For intfn1 = 1 To 400000
DoEvents
Next intfn1

This works perfectly, but takes an absolute age. About 5-6 hours to paste 350 documents together is unacceptable.

Does anyone have any idea how I can get around this problem?? i.e. Make it works quickly AND efficiently?

Many thanks.

PS. the reason for the large document is to produce a large pdf for printing.
 
Hi Marcus,

One thing that will dramatically speed up performance is turning off screen updating while the macro is executing.

You might also get better results by using the macro to create INCLUDETEXT fields pointing to each of the source documents, with the field code display toggled 'on' during the link creation process, and only turning it off once all the links are created. Once such a document is created, any changes to the source documents will be incorporated into it without having to re-run the macro. You'd only need to change the target document if any existing source documents were deleted (not replaced) or new ones were created.

Here's a couple of macros I call at the start and end of any complex document-updating:
Code:
Option Explicit
Dim SBar As Boolean           ' Status Bar flag
Dim TrkStatus As Boolean      ' Track Changes flag

Private Sub MacroEntry()
' Store current Status Bar status, then switch on
SBar = Application.DisplayStatusBar
Application.DisplayStatusBar = True
' Store current Track Changes status, then switch off
With ActiveDocument
    TrkStatus = .TrackRevisions
    .TrackRevisions = False
End With
' Turn Off Screen Updating
Application.ScreenUpdating = False
End Sub

Private Sub MacroExit()
' Clear the Status Bar
Application.StatusBar = False
' Restore original Status Bar status
Application.DisplayStatusBar = SBar
' Restore original Track Changes status
ActiveDocument.TrackRevisions = TrkStatus
' Restore Screen Updating
Application.ScreenUpdating = True
End Sub
The statusbar references are useful if you use the status bar to give feedback on your code's progress.

Cheers

[MS MVP - Word]
 
The main issue regarding performance is this:

"it then copies the selection and pastes into a new document, "

My bolding.

Using Selection uses GUI system resources. Further, using Copy/Paste uses resources moving stuff in and out of the clipboard.

Do not copy the Selection and paste it. You can do what you want using a Range, and as you are doing (as far as I can see) an append, then there is no need to copy or paste at all.

faq219-2884

Gerry
My paintings and sculpture
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top