Results 1 to 2 of 2
  1. #1
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    9,434
    Thanks
    372
    Thanked 1,457 Times in 1,326 Posts

    SOLVED: Works in Word not in Access!

    Hello All,

    I'm stumped!
    This code works just fine in Word.
    Code:
    Sub CreateConcordanceDocument()
    '
    ' CreateConcordanceDocument Macro
    '
    '
        Dim zWDindex(3) As String
        Dim iCntr       As Integer
        
        zWDindex(0) = "Docks"
        zWDindex(1) = "Owners"
        zWDindex(2) = "Lots"
    
        ActiveDocument.Tables.Add Range:=Selection.Range, _
                       NumRows:=1, NumColumns:=2
    
        For iCntr = 0 To UBound(zWDindex) - 1
           With Selection
               .TypeText Text:=zWDindex(iCntr)
               .MoveRight Unit:=wdCell
               .Collapse direction:=wdCollapseEnd
               .Fields.Add Range:=.Range, Type:=wdFieldIndexEntry, Text:=zWDindex(iCntr), _
                           PreserveFormatting:=False
               If iCntr < UBound(zWDindex) - 1 Then .MoveRight Unit:=wdCell
           End With
        Next iCntr
        
    End Sub
    See attached document if you wish to test.

    However, when I transfer the code into Access VBA working with a Word Object it will create column 1 of the Concordance table but not the XE entries in column 2. For the life of me I can't figure it out.
    Code:
    Sub CreateConcordance(zDestFolder As String, zFName As String)
    
       Dim iCntr As Integer
       Dim iLast As Integer
       
       With oWordApp
           .Documents.Add
           .ActiveDocument.Tables.Add Range:=Selection.Range, _
                                      NumRows:=1, NumColumns:=2
           .ActiveDocument.Range.NoProofing = True
           
           iLast = UBound(zWDIndex) - 1
           
           For iCntr = 0 To iLast
              With Selection
                  .TypeText Text:=zWDIndex(iCntr)
                  .MoveRight Unit:=wdCell
                  .Collapse Direction:=wdCollapseEnd
                  .Fields.Add Range:=.Range, _
                     Type:=wdFieldIndexEntry, Text:=zWDIndex(iCntr), _
                     PreserveFormatting:=False
                  If iCntr < iLast Then .MoveRight Unit:=wdCell
              End With  'Selection
              
           Next iCntr
           
           .DisplayAlerts = False
           
           With .ActiveDocument
               .SaveAs FileName:=zDestFolder & "\" & zFName & " Concordance"
               .Close
           End With
              
           
       End With      'oWordApp
    Notes: zWDIndex is a Module level variable and is in Scope as shown by Column 1 being filled in the Concordance output.

    Any advice gratefully welcomed.
    Attached Files Attached Files
    May the Forces of good computing be with you!

    RG

    PowerShell & VBA Rule!

    My Systems: Desktop Specs
    Laptop Specs

  2. #2
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    9,434
    Thanks
    372
    Thanked 1,457 Times in 1,326 Posts
    Solved!

    Still don't know why the code above doesn't work but using my friend Google I found some code that does work.

    Code:
    For iCntr = 0 To iLast  
        With Selection 
              .TypeText Text:=zWDIndex(iCntr) 
              .MoveRight Unit:=wdCell 
              ActiveDocument.Indexes.MarkEntry Range:=.Range, Entry:= zWDIndex(iCntr)
              If iCntr < iLast Then .MoveRight Unit:=wdCell
        End With   'Selection
    Next iCntr
    May the Forces of good computing be with you!

    RG

    PowerShell & VBA Rule!

    My Systems: Desktop Specs
    Laptop Specs

Posting Permissions

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