Results 1 to 3 of 3

Thread: Mail Merge to Labels Not Resizing # of Labels to # of Records

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2005
    Location
    SoCal
    Posts
    54

    Mail Merge to Labels Not Resizing # of Labels to # of Records

    I created a mail merge doc for which the data set is a .csv file & the records pull into labels. However, it only keeps the same # of labels as for the original dataset, so datasets with fewer records have too many labels and datasets with more records don't pull all the records in.

    Do I need replan this so that a macro is run to create a new mail merge, or is there some way to set this that I am missing. If I do need to write a macro to create the mail merge, could someone start me out? I'm not sure where to begin to create a mail merge.

  2. #2

    Thread Starter
    Member
    Join Date
    Mar 2005
    Location
    SoCal
    Posts
    54

    Question Re: Mail Merge to Labels Not Resizing # of Labels to # of Records

    I have this thus far:


    But now at the tail end it tells me that the field codes are not valid for the data set: "This merge field is in use in the main document but does not exist in the data source." Does this have anything to do with the fact that there is no header in the data source & that it uses comma delimiters? I don't see a way to set that in the VBA code.


    VB Code:
    1. Application.MailingLabel.DefaultPrintBarCode = False
    2.     Application.MailingLabel.CreateNewDocument Name:="5161"
    3.    
    4.     ActiveDocument.MailMerge.MainDocumentType = wdMailingLabels
    5.     ActiveDocument.MailMerge.OpenDataSource Name:="O:\ProLaw\label.csv", _
    6.         ConfirmConversions:=True, ReadOnly:=False, LinkToSource:=True, _
    7.         AddToRecentFiles:=False, PasswordDocument:="", PasswordTemplate:="", _
    8.         WritePasswordDocument:="", WritePasswordTemplate:="", Revert:=False, _
    9.         Format:=wdOpenFormatAuto, Connection:= _
    10.         "Provider=Microsoft.Jet.OLEDB.4.0;Password="""";User ID=Admin;Data Source=O:\ProLaw;Mode=Read;Extended Properties=""HDR=NO;"";Jet OLEDB:System database="""";Jet OLEDB:Registry Path="""";Jet OLEDB:Database Password="""";Jet OLEDB:Engine Type=96;Jet OLEDB:Database Loc" _
    11.         , SQLStatement:="SELECT * FROM `label#csv`", SQLStatement1:="", SubType:= _
    12.         wdMergeSubTypeOLEDBText
    13.     ActiveDocument.Fields.Add Range:=Selection.Range, Type:=wdFieldMergeField _
    14.         , Text:="""F1"""
    15.     ActiveDocument.Fields.Add Range:=Selection.Range, Type:=wdFieldMergeField _
    16.         , Text:="""F2"""
    17.     ActiveDocument.Fields.Add Range:=Selection.Range, Type:=wdFieldMergeField _
    18.         , Text:="""F3"""
    19.     ActiveDocument.Fields.Add Range:=Selection.Range, Type:=wdFieldMergeField _
    20.         , Text:="""F4"""
    21.     ActiveDocument.Fields.Add Range:=Selection.Range, Type:=wdFieldMergeField _
    22.         , Text:="""F5"""
    23.     Selection.MoveLeft Unit:=wdCharacter, Count:=5, Extend:=wdExtend
    24.     Selection.Font.Name = "Arial"
    25.     Selection.Font.Size = 11
    26.     Selection.MoveLeft Unit:=wdCharacter, Count:=1
    27.     Selection.MoveRight Unit:=wdCharacter, Count:=4
    28.     Selection.ParagraphFormat.TabStops.Add Position:=InchesToPoints(3.73), _
    29.         Alignment:=wdAlignTabRight, Leader:=wdTabLeaderSpaces
    30.     Selection.ParagraphFormat.TabStops(InchesToPoints(3.73)).Position = _
    31.         InchesToPoints(3.88)
    32.     Selection.TypeText Text:=vbTab
    33.     Selection.MoveRight Unit:=wdCharacter, Count:=4
    34.     Selection.TypeParagraph
    35.     Selection.MoveRight Unit:=wdCharacter, Count:=1, Extend:=wdExtend
    36.     Selection.Font.Bold = wdToggle
    37.     Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
    38.     Selection.MoveRight Unit:=wdCharacter, Count:=1
    39.     Selection.TypeParagraph
    40.     Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft
    41.     Selection.TypeParagraph
    42.     Selection.MoveUp Unit:=wdLine, Count:=2
    43.     Selection.TypeParagraph
    44.     Selection.MoveDown Unit:=wdLine, Count:=2
    45.     Selection.MoveLeft Unit:=wdCharacter, Count:=4
    46.     Selection.TypeText Text:=vbTab
    47.     Selection.MoveRight Unit:=wdCharacter, Count:=1, Extend:=wdExtend
    48.     Selection.Font.Size = 9
    49.     Selection.MoveRight Unit:=wdCharacter, Count:=1
    50.     WordBasic.MailMergePropagateLabel
    51.     With ActiveDocument.MailMerge
    52.         .Destination = wdSendToNewDocument
    53.         .SuppressBlankLines = True
    54.         With .DataSource
    55.             .FirstRecord = wdDefaultFirstRecord
    56.             .LastRecord = wdDefaultLastRecord
    57.         End With
    58.         .Execute Pause:=False
    59.     End With
    60. End Sub

    But now at the tail end it tells me that the field codes are not valid for the data set: "This merge field is in use in the main document but does not exist in the data source." Does this have anything to do with the fact that there is no header in the data source & that it uses comma delimiters? I don't see a way to set that in the VBA code.

  3. #3

    Thread Starter
    Member
    Join Date
    Mar 2005
    Location
    SoCal
    Posts
    54

    Re: Mail Merge to Labels Not Resizing # of Labels to # of Records

    OK. I worked this one out by inserting the data into a Word table, adding a row, inserting text into each cell of that row to act as headers, and then using that table for my datasource.

    VB Code:
    1. Sub TestTableLabels()
    2. '
    3. ' TestTableLabels Macro
    4. ' Macro recorded 7/30/2005 by User
    5.  
    6. 'Create Word table from dataset so header rows can be inserted
    7. Dim DataTable As Table
    8. Dim FirstRow As Row
    9. Dim CurCell As Cell
    10. Dim i As Integer
    11.  
    12. Dim Doc1 As Document
    13. Dim Doc2 As Document
    14.  
    15. Set Doc1 = ActiveDocument
    16.  
    17. ActiveDocument.Select
    18.  
    19. With Selection
    20.     .Collapse Direction:=wdCollapseEnd
    21.     .Range.InsertDatabase _
    22.         Format:=wdTableFormatSimple2, Style:=191, _
    23.         LinkToSource:=False, Connection:="Entire Spreadsheet", _
    24.         DataSource:="C:\Me\Reference\Work\labels.csv"
    25. End With
    26.  
    27. Set DataTable = ActiveDocument.Tables(1)
    28.  
    29. Set FirstRow = DataTable.Rows.Add(BeforeRow:=DataTable.Rows(1))
    30.  
    31. For Each CurCell In FirstRow.Cells
    32.  
    33.     CurCell.Range.InsertAfter Text:="Cell " & i
    34.     i = i + 1
    35.    
    36. Next CurCell
    37.  
    38.  
    39. ActiveDocument.SaveAs FileName:="C:\Me\Reference\Work\LabelTable.doc", _
    40.     FileFormat:=wdFormatDocument
    41.  
    42. 'ActiveDocument.Close
    43.  
    44. 'Create the mailmerge
    45. Application.MailingLabel.DefaultPrintBarCode = False
    46. Application.MailingLabel.CreateNewDocument Name:="5161"
    47.  
    48.     ActiveDocument.MailMerge.MainDocumentType = wdMailingLabels
    49.     ActiveDocument.MailMerge.OpenDataSource Name:= _
    50.         "C:\Me\Reference\Work\LabelTable.doc", ConfirmConversions:=False, _
    51.         ReadOnly:=False, LinkToSource:=True, AddToRecentFiles:=False, _
    52.         PasswordDocument:="", PasswordTemplate:="", WritePasswordDocument:="", _
    53.         WritePasswordTemplate:="", Revert:=False, Format:=wdOpenFormatAuto, _
    54.         Connection:="", SQLStatement:="", SQLStatement1:="", SubType:= _
    55.         wdMergeSubTypeOther
    56.     ActiveDocument.Fields.Add Range:=Selection.Range, Type:=wdFieldMergeField _
    57.         , Text:="""Cell_0"""
    58.     ActiveDocument.Fields.Add Range:=Selection.Range, Type:=wdFieldMergeField _
    59.         , Text:="""Cell_1"""
    60.     ActiveDocument.Fields.Add Range:=Selection.Range, Type:=wdFieldMergeField _
    61.         , Text:="""Cell_2"""
    62.     ActiveDocument.Fields.Add Range:=Selection.Range, Type:=wdFieldMergeField _
    63.         , Text:="""Cell_3"""
    64.     ActiveDocument.Fields.Add Range:=Selection.Range, Type:=wdFieldMergeField _
    65.         , Text:="""Cell_4"""
    66.     Selection.MoveLeft Unit:=wdCharacter, Count:=5, Extend:=wdExtend
    67.     Selection.Font.Size = 11
    68.     Selection.MoveLeft Unit:=wdCharacter, Count:=1
    69.     Selection.MoveRight Unit:=wdCharacter, Count:=8
    70.     Selection.ParagraphFormat.TabStops.Add Position:=InchesToPoints(3.76), _
    71.         Alignment:=wdAlignTabRight, Leader:=wdTabLeaderSpaces
    72.     Selection.ParagraphFormat.TabStops(InchesToPoints(3.76)).Position = _
    73.         InchesToPoints(3.88)
    74.     Selection.TypeText Text:=vbTab
    75.     Selection.MoveRight Unit:=wdCharacter, Count:=8
    76.     Selection.TypeParagraph
    77.     Selection.TypeParagraph
    78.     Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
    79.     Selection.MoveRight Unit:=wdCharacter, Count:=1, Extend:=wdExtend
    80.     Selection.Font.Bold = wdToggle
    81.     Selection.MoveRight Unit:=wdCharacter, Count:=1
    82.     Selection.TypeParagraph
    83.     Selection.TypeParagraph
    84.     Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft
    85.     Selection.MoveRight Unit:=wdCharacter, Count:=8
    86.     Selection.TypeText Text:=vbTab
    87.     Selection.MoveRight Unit:=wdCharacter, Count:=1, Extend:=wdExtend
    88.     Selection.Font.Size = 9
    89.     Selection.MoveRight Unit:=wdCharacter, Count:=1
    90.     WordBasic.MailMergePropagateLabel
    91.    
    92. Set Doc2 = ActiveDocument
    93.    
    94.     With ActiveDocument.MailMerge
    95.    
    96.         .Destination = wdSendToNewDocument
    97.         .SuppressBlankLines = True
    98.        
    99.         With .DataSource
    100.        
    101.             .FirstRecord = wdDefaultFirstRecord
    102.             .LastRecord = wdDefaultLastRecord
    103.            
    104.         End With
    105.        
    106.         .Execute Pause:=False
    107.        
    108.     End With
    109.  
    110. Doc1.Close
    111. Doc2.Close (wdDoNotSaveChanges)
    112.    
    113. End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width