Hi,
I have a macro which splits a mailmerged document into its constituant
letters. I want this macro to name each file according to a specific
name so I have created a variable Yipname. I cant understand why i get
a subscript out of range error with the following code. I'm pretty new
to VBA so sorry if I'm missing something fairly obvious. You can assume
that there are only 10 letters in the mail merge document.
Sub Splitter()
' splitter Macro
' saves each letter created by a mailmerge as a separate file.
Dim Yipname() As String
Selection.EndKey Unit:=wdStory
Letters = Selection.Information(wdActiveEndSectionNumber)
Yipname(1) = "Barking and Dagenham"
Yipname(2) = "Barrow-in-Furness "
Yipname(3) = "Birmingham Kingstanding"
Yipname(4) = "Birmingham Shard End"
Yipname(5) = "Birmingham Washwood Heath"
Yipname(6) = "Blackburn Mill Hill"
Yipname(7) = "Bolton"
Yipname(8) = "Bournemouth"
Yipname(9) = "Bradford NDC"
Yipname(10) = "Bradford Newlands"
Selection.HomeKey Unit:=wdStory
counter = 1
While counter < Letters
DocName = "c:\feedback\" & Yipname(counter)
ActiveDocument.Sections.First.Range.Cut
Documents.Add
Selection.Paste
ActiveDocument.SaveAs FileName:=DocName, FileFormat:=wdFormatDocument
ActiveWindow.Close
counter = counter + 1
Wend
End Sub
many thanks for any help.
David Sisson - 26 Aug 2005 18:27 GMT
The simpliest answer is:
You have to specify the number of elements in the statement,
Dim Yipname() As String
to
Dim Yipname(10) As String
But this causes another problem. VB starts counting elements at 0. So
you have to use,
Dim Yipname(11) As String
But, then your data, Yipname(0) would be empty. (Not a problem really,
it just uses more memory. Not good programming technique.)
Or you can start you data at 0.
Or you can use Option Base 1 before the Dim statement. This tells VB
to start counting elements at 1, instead of 0.
Or you can use Redim. Look in help for further explanation.
David
richard_stockley@hotmail.com - 31 Aug 2005 11:20 GMT
Thanks for that. I thought if you didn't put a number in the brackets
it would just count until it ran out of variables.
Thanks again.