Results 1 to 14 of 14
  1. #1
    Join Date
    Jan 2021
    Posts
    23

    Select separate rows in table and write into Word File via Bookmarks

    Hello guys,



    I am relatively new to Access and VBA. With the following code, I managed to write data from a table named "Daten" to a Word File via Bookmarks and save the new created file at the path in the code.
    Here is my problem: When I run the code, it does it for ALL users that are in the table but I would like to have the option to select users and do it separetely with them.
    The table consists of several attributes like Name, FirstName, Birthday, and so forth. And I managed to create new users with a form(ular).
    How can I sort of select the table row and manipulate the code so that I can select the users I want to do it with?

    I did a google search and found Select, DoCmd Go To. Can anyone help? Thank you for everything!

    Greetings,
    FinisherProgrammer21

    Code:
    Option Compare Database
    
    
    
    
    Public Sub ExportNameToWord()
        Dim wApp As Word.Application
        Dim wDoc As Word.Document
        Dim rs As DAO.Recordset
        
        
        Set wApp = New Word.Application
        Set wDoc = wApp.Documents.Open("C:\Users\Desktop\Testdocument.docx")
        Set rs = CurrentDb.OpenRecordset("tbl_Daten")
        
      
        If Not rs.EOF Then rs.MoveFirst
        
        Do Until rs.EOF
            wDoc.Bookmarks("Name").Range.Text = Nz(rs!Nachname, "")
            wDoc.Bookmarks("Firstname").Range.Text = Nz(rs!Firstname, "")
            wDoc.Bookmarks("Birthday").Range.Text = Nz(rs!Birthday, "")
           
            wDoc.SaveAs2 "C:\Users\Desktop\" & rs!Name & rs!Firstname & "_Testdocument.docx"
            
            wDoc.Bookmarks("Name").Range.Delete wdCharacter, Len(Nz(rs!Name, ""))
            wDoc.Bookmarks("Firstname").Range.Delete wdCharacter, Len(Nz(rs!Firstname, ""))
            wDoc.Bookmarks("Birthday").Range.Delete wdCharacter, Len(Nz(rs!Birthday, ""))
            
            
            rs.MoveNext
        Loop
        
        wDoc.Close False
        wApp.Quit
        
        Set wDoc = Nothing
        Set wApp = Nothing
        Set rs = Nothing
    End Sub

  2. #2
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,651
    Instead of using a table in your open recordset, use a query with a where clause.

    Code:
    StrSql = "Select * from tbl_Daten where PersonID = " & ? 
    Set rs = CurrentDb.OpenRecordset(strSql),dbFailOnError
    Adjust strSql as needed
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  3. #3
    orange's Avatar
    orange is offline Moderator
    Windows 10 Access 2016
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,722
    One way would be to have a form with a combobox and a command button.
    The combobox would have as its rowsource the data you want from your tbl_daten.
    The button when clicked would process the selected record and write it to your wordfile.

    You would modify your code to be a function.

  4. #4
    Join Date
    Jan 2021
    Posts
    23
    Hello moke123,

    I can't quite follow. With the where Clause, you would just define the criteria of the results of the query, right? So like range > xy but in my case, it's just arbitrarily select a person in case there needs to be an output.

    In your solution, there is no option to single handedly select a person, if I'm right?

    I don't mean to offend you, I'm just a newbie and ask if I understood correctly. Thanks!

  5. #5
    Join Date
    Jan 2021
    Posts
    23
    Hello orange,

    if I implement the button, do I immediately have to assign VBA Code to the button in Access? I'm asking because I have never done that so far.

    What steps do I need to do in order to do what you suggested?

    Thanks!

  6. #6
    orange's Avatar
    orange is offline Moderator
    Windows 10 Access 2016
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,722
    I'll be away most of the day so do not have time to work through a solution.

    Research the terms in blue that I provided in my other post #3. Look for youtube videos on
    form with combobox.
    You could also use the after update event of the combobox to invoke your function.
    As moke said you should use a query as the recordset in your data selection. You would select the record from tbl_daten that matches the selection in the combobox.

    I'm sure others will offer advice or examples.
    You may also find info in the Similar Threads at th bottom of this and related pages.

  7. #7
    Gicu's Avatar
    Gicu is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,112
    You could add a boolean (Yes\No) field to your Daten table named DatenSelect or similar, add it to your form (as a check-box), select your records then do your Word export using a query like this: "SELECT Daten.* FROM Daten WHERE DatenSelect=True;".

    Cheers,
    Vlad
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  8. #8
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,651
    Quote Originally Posted by Gicu View Post
    You could add a boolean (Yes\No) field to your Daten table named DatenSelect or similar, add it to your form (as a check-box), select your records then do your Word export using a query like this: "SELECT Daten.* FROM Daten WHERE DatenSelect=True;".

    Cheers,
    Vlad
    The one caveat to that method is if it's a shared database. Should 2 users be making selections at the same time they would cross with each other. I would use a secondary local database with a table to make selections.
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  9. #9
    Join Date
    Jan 2021
    Posts
    23
    Quote Originally Posted by orange View Post
    I'll be away most of the day so do not have time to work through a solution.

    Research the terms in blue that I provided in my other post #3. Look for youtube videos on
    form with combobox.
    You could also use the after update event of the combobox to invoke your function.
    As moke said you should use a query as the recordset in your data selection. You would select the record from tbl_daten that matches the selection in the combobox.

    I'm sure others will offer advice or examples.
    You may also find info in the Similar Threads at th bottom of this and related pages.
    ============================

    Hello orange,

    I did use your suggested way and I now managed to insert a SubForm into my Form called "Data Search by Combobox". In the Form Head, I managed to implement a ComboBox named "cboVorname" and with the following code, I managed to get the search function going:

    Code:
     
    private Sub cboVorname_AfterUpdate()
    Dim Person as String
    Person = "Select * from tbl_Stammdaten where ([ID] = " & Me.cboVorname & ")"
    Me.Dateneingabe_Subform.Form.RecordSource = Person
    Me.Dateneingabe_Subform.Form.Requery
    Now, here is my last problem to all this:

    In the detail section of the form "Date Search by Combobox", I managed to implement a button called "Word Export".
    How do I adjust my code from above to make the button create a Word-File only for what the combobox filters and shows in the detail section?

    Thank you guys! I would be very very grateful if you could help me on this one.
    I really tried, for several hours but I just can't get the code going.

    Greetings,
    FinisherProgrammer21

  10. #10
    orange's Avatar
    orange is offline Moderator
    Windows 10 Access 2016
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,722
    I think the following will work, BUT I'm not a word vba person, nor have I seen your database.

    1.- Adjust the sub you showed earlier to become
    Code:
    Public Sub ExportNameToWord(sSelectedPerson as String)
        Dim wApp As Word.Application
        Dim wDoc As Word.Document
        Dim rs As DAO.Recordset
        
        
        Set wApp = New Word.Application
        Set wDoc = wApp.Documents.Open("C:\Users\Desktop\Testdocument.docx")
        'Set rs = CurrentDb.OpenRecordset("tbl_Daten") '''' THIS LINE IS REPLACED WITH THE NEXT LINE
        Set rs = CurrentDb.OpenRecordset(sSelectedPerson)
    'sSelectedPerson is thevalue you selected from the combobox and your related SQL (Person)
      
        If Not rs.EOF Then rs.MoveFirst
        
        Do Until rs.EOF
            wDoc.Bookmarks("Name").Range.Text = Nz(rs!Nachname, "")
            wDoc.Bookmarks("Firstname").Range.Text = Nz(rs!Firstname, "")
            wDoc.Bookmarks("Birthday").Range.Text = Nz(rs!Birthday, "")
           
            wDoc.SaveAs2 "C:\Users\Desktop\" & rs!Name & rs!Firstname & "_Testdocument.docx"
            
            wDoc.Bookmarks("Name").Range.Delete wdCharacter, Len(Nz(rs!Name, ""))
            wDoc.Bookmarks("Firstname").Range.Delete wdCharacter, Len(Nz(rs!Firstname, ""))
            wDoc.Bookmarks("Birthday").Range.Delete wdCharacter, Len(Nz(rs!Birthday, ""))
            
            
            rs.MoveNext
        Loop
        
        wDoc.Close False
        wApp.Quit
        
        Set wDoc = Nothing
        Set wApp = Nothing
        Set rs = Nothing
    End Sub
    2. Add the last line below to your AfterUpdate to invoke the Export to word.
    Code:
    private Sub cboVorname_AfterUpdate()
    Dim Person as String
    Person = "Select * from tbl_Stammdaten where ([ID] = " & Me.cboVorname & ")"
    Me.Dateneingabe_Subform.Form.RecordSource = Person
    Me.Dateneingabe_Subform.Form.Requery
    Call ExportNameToWord(Person)  'Add this line to invoke the Export to word !!!!!!!!!!
    Note: Try this on a copy of your database. Do a backup first -just in case I've misunderstood.

    It appears that you are only selecting 1 person at a time --is this correct?

  11. #11
    Join Date
    Jan 2021
    Posts
    23
    Quote Originally Posted by orange View Post
    I think the following will work, BUT I'm not a word vba person, nor have I seen your database.

    1.- Adjust the sub you showed earlier to become
    Code:
    Public Sub ExportNameToWord(sSelectedPerson as String)
        Dim wApp As Word.Application
        Dim wDoc As Word.Document
        Dim rs As DAO.Recordset
        
        
        Set wApp = New Word.Application
        Set wDoc = wApp.Documents.Open("C:\Users\Desktop\Testdocument.docx")
        'Set rs = CurrentDb.OpenRecordset("tbl_Daten") '''' THIS LINE IS REPLACED WITH THE NEXT LINE
        Set rs = CurrentDb.OpenRecordset(sSelectedPerson)
    'sSelectedPerson is thevalue you selected from the combobox and your related SQL (Person)
      
        If Not rs.EOF Then rs.MoveFirst
        
        Do Until rs.EOF
            wDoc.Bookmarks("Name").Range.Text = Nz(rs!Nachname, "")
            wDoc.Bookmarks("Firstname").Range.Text = Nz(rs!Firstname, "")
            wDoc.Bookmarks("Birthday").Range.Text = Nz(rs!Birthday, "")
           
            wDoc.SaveAs2 "C:\Users\Desktop\" & rs!Name & rs!Firstname & "_Testdocument.docx"
            
            wDoc.Bookmarks("Name").Range.Delete wdCharacter, Len(Nz(rs!Name, ""))
            wDoc.Bookmarks("Firstname").Range.Delete wdCharacter, Len(Nz(rs!Firstname, ""))
            wDoc.Bookmarks("Birthday").Range.Delete wdCharacter, Len(Nz(rs!Birthday, ""))
            
            
            rs.MoveNext
        Loop
        
        wDoc.Close False
        wApp.Quit
        
        Set wDoc = Nothing
        Set wApp = Nothing
        Set rs = Nothing
    End Sub
    2. Add the last line below to your AfterUpdate to invoke the Export to word.
    Code:
    private Sub cboVorname_AfterUpdate()
    Dim Person as String
    Person = "Select * from tbl_Stammdaten where ([ID] = " & Me.cboVorname & ")"
    Me.Dateneingabe_Subform.Form.RecordSource = Person
    Me.Dateneingabe_Subform.Form.Requery
    Call ExportNameToWord(Person)  'Add this line to invoke the Export to word !!!!!!!!!!
    Note: Try this on a copy of your database. Do a backup first -just in case I've misunderstood.

    It appears that you are only selecting 1 person at a time --is this correct?













    ==================



    Hey orange, I did as you suggested and it gets me the error:

    "Sub or Function not defined"

    What could be the reason?

    Greetings,
    FinisherProgrammer21

  12. #12
    orange's Avatar
    orange is offline Moderator
    Windows 10 Access 2016
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,722
    Did you change the name of the Sub?

    Is the Sub located in a standard module?

    What is
    tbl_Stammdaten vs tbl_Daten ?

    Attached is a mockup
    Attached Files Attached Files

  13. #13
    Join Date
    Jan 2021
    Posts
    23

    Red face

    Quote Originally Posted by orange View Post
    Did you change the name of the Sub?

    Is the Sub located in a standard module?

    What is
    tbl_Stammdaten vs tbl_Daten ?

    Attached is a mockup


    Hello orange, many thanks for the endless support! I solved it with your and the other guys' help
    So glad there are people working cohesively and are willing to contribute to other people's problems.
    Don't wanted to be rude and say thank you in a genuine way, even if a little late. I was completely focused on another task. Sorry!

    This is one solved

  14. #14
    orange's Avatar
    orange is offline Moderator
    Windows 10 Access 2016
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,722

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

Similar Threads

  1. MS Word template with bookmarks
    By jonnyh in forum Import/Export Data
    Replies: 2
    Last Post: 09-07-2016, 07:42 AM
  2. Replies: 11
    Last Post: 01-26-2012, 09:28 AM
  3. Replies: 1
    Last Post: 03-18-2011, 03:22 PM
  4. Access Form to Word Bookmarks
    By markod in forum Forms
    Replies: 5
    Last Post: 12-27-2010, 05:18 PM
  5. font of word bookmarks
    By emilylu3 in forum Programming
    Replies: 1
    Last Post: 03-09-2006, 12:07 PM

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