Results 1 to 2 of 2

Thread: Insertdatabase Does Not Work with Just 1 Record

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2005
    Location
    SoCal
    Posts
    54

    Insertdatabase Does Not Work with Just 1 Record

    I created a macro that propagates labels from a .csv file. I used this technique because the report could generate anywhere from 1 - 3000 labels, and just creating a mail merge doc that is linked to the dataset was only generating the same amount of labels as used in the original dataset. The dataset also has no header row, so I had to bring it into a table so that I could insert a header row for mail merge fields.

    However, it turns out that Word cannot open the database when there is only one record.

    Any suggestions on a work around for this eventuality?

    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

  2. #2

    Thread Starter
    Member
    Join Date
    Mar 2005
    Location
    SoCal
    Posts
    54

    Re: Insertdatabase Does Not Work with Just 1 Record

    I found that using InsertFile followed by ConvertToTable allows the labels to be propagated with just one record. However, it does not propagate the correct # of labels, just one page. Any ideas on why, or how to combine the 2 to make it work for any # of records?

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