Results 1 to 4 of 4
  1. #1
    Goodge12 is offline Novice
    Windows XP Access 97
    Join Date
    Sep 2008
    Posts
    25

    Using VBA to open word doc, find/replace query results in ; delimited

    Hi All-

    I need some help, please! I've got a query (qry_prod_hierarchy_list) that pulls a list of items. I need to be able to change this from the normal query list:



    Item 1
    Item 2
    Item 3
    Item 4

    to a string of delimited text in a word document:

    Item 1; Item 2; Item 3; Item 4

    I've been able to do this in the past with email addresses into the "To" box, but trying to take that code and apply it here isn't working. Here's what I've got...

    Code:
    Private Sub Command1_Click()
    
       Dim db As DAO.Database
       Dim ProdHierList As Object, count As Integer, sql As String
       
       Set db = CurrentDb
       Set ProdHierList = db.OpenRecordset("qry_prod_hierarchy_list")
    
        Dim objWord As Word.Application
        Set objWord = New Word.Application
    
    With objWord
        .Documents.Open "mypath\myworddoc.docx"
        .Documents("mypath\myworddoc.docx").Activate
        .Visible = True
    End With
    
    
    If ProdHierList.RecordCount > 0 Then
        ProdHierList.MoveLast
        ProdHierList.MoveFirst
        recount = ProdHierList.RecordCount
        For count = 1 To recount
        If Not IsNull(ProdHierList![Prod_Hierarchy]) Then ProdHierList = ProdHierList & ProdHierList![Prod_Hierarchy] & ";"
        ProdHierList.MoveNext
        Next count
        ProdHierList = Left(ProdHierList, Len(ProdHierList) - 1)
      
      
    With objWord.Selection.Find
                .ClearFormatting
                .Replacement.ClearFormatting
                .Text = "Enter Prod Hierarchy List"
                .Replacement.Text = ProdHierList
                .Forward = True
                .Wrap = wdFindContinue
                .Format = False
                .MatchCase = False
                .MatchWholeWord = False
                .MatchWildcards = False
                .MatchSoundsLike = False
                .MatchAllWordForms = False
                .Execute Replace:=2 'wdReplaceAll
    End With
    End If
    
    End Sub
    I get a Run Time Error '3001': Invalid Argument

    When I click the "Debug", it highlights "ProdHierList = ProdHierList & ProdHierList![Prod_Hierarchy] & ";"

    I'm open to suggestions!

    Thank you!

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    It appears that you're trying to use the same variable for the delimited list and the recordset. Declare a string variable for the list and use that instead:

    VariableName = VariableName & ProdHierList![Prod_Hierarchy] & ";"
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    Goodge12 is offline Novice
    Windows XP Access 97
    Join Date
    Sep 2008
    Posts
    25
    Thanks, that worked!

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Happy to help!
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 6
    Last Post: 12-22-2014, 12:29 PM
  2. VBA to find/replace text in word document
    By Goodge12 in forum Programming
    Replies: 1
    Last Post: 07-16-2014, 01:23 PM
  3. Replies: 2
    Last Post: 06-14-2013, 12:56 PM
  4. Find and Replace query from a 2nd table
    By elightbox in forum Queries
    Replies: 1
    Last Post: 09-17-2010, 05:37 PM
  5. Find and Replace Query
    By randolphoralph in forum Queries
    Replies: 4
    Last Post: 03-17-2010, 07:25 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums