indieman
03-15-2012, 09:31 AM
Hi,
I have the following code but its running very slow. I lost the original code I created years ago so had to create it again. Im not sure of the method I used last time but this time its definitely about twice as slow as it was. any advice on where im going wrong?
The document is splitting a mail-merged document into individual documents where the filenames are defined from a specific ID number field from within each document in the mail merge.
I start by opening the mail-merge document that contains all 2000 letters one after the other, and then run the code below.
any advice is very welcomed.
thanks,
Sub splitMailMerge()
Dim i As Long
With ActiveDocument
Application.ScreenUpdating = False
For i = 1 To .Sections.Count - 1
'copy individual mail merge sections
.Sections.Item(i).Range.Copy
'open template and paste data
Documents.Open "D:\template.doc"
Selection.PasteAndFormat wdPasteDefault
'remove blank last page by finding the last words in the document and then
'deleting the blank page immediately after
ActiveDocument.Select
With Selection.Find
.Text = "Thank you"
.Execute
End With
Selection.MoveRight Unit:=wdCharacter, Count:=2
Selection.Delete Unit:=wdCharacter, Count:=2
'find the text in the document that the individual files will be named after
ActiveDocument.Select
With Selection.Find
.Text = "ID number "
.Execute
End With
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.MoveRight Unit:=wdWord, Count:=1, Extend:=wdExtend
'save individual document
ActiveDocument.SaveAs FileName:="D:\files\" & Selection.Text & ".doc"
ActiveDocument.Close False
'update status bar with progress
Application.ScreenUpdating = True
StatusBar = i & "/" & .Sections.Count
Application.ScreenUpdating = False
Next i
Application.ScreenUpdating = True
End With
End Sub
I have the following code but its running very slow. I lost the original code I created years ago so had to create it again. Im not sure of the method I used last time but this time its definitely about twice as slow as it was. any advice on where im going wrong?
The document is splitting a mail-merged document into individual documents where the filenames are defined from a specific ID number field from within each document in the mail merge.
I start by opening the mail-merge document that contains all 2000 letters one after the other, and then run the code below.
any advice is very welcomed.
thanks,
Sub splitMailMerge()
Dim i As Long
With ActiveDocument
Application.ScreenUpdating = False
For i = 1 To .Sections.Count - 1
'copy individual mail merge sections
.Sections.Item(i).Range.Copy
'open template and paste data
Documents.Open "D:\template.doc"
Selection.PasteAndFormat wdPasteDefault
'remove blank last page by finding the last words in the document and then
'deleting the blank page immediately after
ActiveDocument.Select
With Selection.Find
.Text = "Thank you"
.Execute
End With
Selection.MoveRight Unit:=wdCharacter, Count:=2
Selection.Delete Unit:=wdCharacter, Count:=2
'find the text in the document that the individual files will be named after
ActiveDocument.Select
With Selection.Find
.Text = "ID number "
.Execute
End With
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.MoveRight Unit:=wdWord, Count:=1, Extend:=wdExtend
'save individual document
ActiveDocument.SaveAs FileName:="D:\files\" & Selection.Text & ".doc"
ActiveDocument.Close False
'update status bar with progress
Application.ScreenUpdating = True
StatusBar = i & "/" & .Sections.Count
Application.ScreenUpdating = False
Next i
Application.ScreenUpdating = True
End With
End Sub