Question

I created a VBA form and macro that will generate auto-numbered documents for my company. Depending on the cost-center selected it will choose a Word document to complete the merge. It basically generates an alpha-numeric sequence in Column A under the header 'FirstName' (FirstName played nicely with Word as a 'source' field).

Everything seemed to be working fine until I went to produce a sequence that had a leading zero. Every leading zero after my 'manual' (starting number) entry from the form got auto-removed. I did some reading and ended up just checking the Len() of my string and re-inserted the 0 if it was less than the number of digits that the input (starting) number had. Hope you're still following me here. :)

Here is what I think happened... I had tried some other things in the beginning, including having the macro write out the sequence with a single quote ('), but it kept removing those leading zeros before that point. I highlighted the entire column and formatted as text (so, technically 64,000+ rows?) but again, same issue. The work-around in my last paragraph solved the issue, but now the merge is attempting to merge ALL rows in the column.

The answer may be simple, but what am I looking for to get it to STOP when the sequences stop? I have one routine that generates the numbers, and it only generates as many as I want:

For i = 1 To strFormNums
    If Len(strStartVal) < 4 Then
        rngFormNumbers(i).Value = strCampus & "0" & strStartVal 'Appends a single 0 to show before the starting numbers (making 0800 instead of 800). Otherwise Excel will lose the leading zero after the first number.
    Else
        rngFormNumbers(i).Value = strCampus & strStartVal
    End If

    strStartVal = strStartVal + 1
Next i

I have tested this. It doesn't generate more than, say, 200 or 500 lines. Here is a snippet from the mail merge, which I'm thinking is grabbing ALL lines now for some reason (it didn't before):

strWorkbookName = ThisWorkbook.Path & "\" & ThisWorkbook.Name

wdocSource.MailMerge.MainDocumentType = wdFormLetters

wdocSource.MailMerge.OpenDataSource _
        Name:=strWorkbookName, _
        AddToRecentFiles:=False, _
        Revert:=False, _
        Format:=wdOpenFormatAuto, _
        Connection:="Data Source=" & strWorkbookName & ";Mode=Read", _
        SQLStatement:="SELECT * FROM `Numbers$`"

With wdocSource.MailMerge
    .Destination = wdSendToNewDocument
    .SuppressBlankLines = True
    With .DataSource
        .FirstRecord = wdDefaultFirstRecord
        .LastRecord = wdDefaultLastRecord
    End With
    .Execute Pause:=False
End With

If I had to venture a guess I would say that the SQL statement is now 'seeing' more rows because the whole column has been formatted? I did a manual merge and sure enough there were thousands of blanks after my 200 numbers ended. I filtered them out manually in Word, but I would like to fix this because obviously my program was meant to automate this process.

If anyone needs to see more code I will be happy to provide it. Thanks in advance.

Was it helpful?

Solution

After some experimenting, I decided to remove all formatting from Column A using Clear > Clear Formats (Excel 2010). The mail merge is now terminating at the first blank line instead of running through the entire column.

It seems that highlighting a column and formatting as Text (for instance) can really muck up a mail merge. If I'm mistaken please feel free to correct me, but this is what seems to have solved my issue.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top