Export data from Access to Word with VBA (1 Viewer)

Leo_Polla_Psemata

Registered User.
Local time
Today, 03:12
Joined
Mar 24, 2014
Messages
364
Hi there, with the code i display below, we can export data from access to excel by using VBA.
In this link, someone can find the sample database.

I need to do the same with MS Word. I need to export data from Access to word.
At this moment I use the "Word merge" method and i get the desired result, however,
i must use and keep in a safe place this word template, if for some reason, someone removes this template,
then i must create a new one from scratch.
Could we create a word document with VBA ? No need of keeping objects outside access? (I wish all done in just one application only)

When we build the excel, we type the below and VBA knows where to stick the data, when Word ? Is there any way ?
.Range("A1").Value = "ID"
.Range("B1").Value = "BK"

Code:
Private Sub Btn1_Click()
On Error GoTo SubError

    Dim xlApp As Excel.Application
    Dim xlBook As Excel.Workbook
    Dim xlSheet As Excel.Worksheet
    Dim SQL As String
    Dim rsBS As DAO.Recordset
    Dim i As Integer

    'Show user work is being performed
    DoCmd.Hourglass (True)

    '*********************************************
    '              RETRIEVE DATA
    '*********************************************
    'SQL statement to retrieve data from database

     SQL = "SELECT Forma1.IDM, Forma1.bk, Forma1.freight, Forma1.curre " & _
        "FROM Forma1;"

   'Execute query and populate recordset
    Set rsBS = CurrentDb.OpenRecordset(SQL, dbOpenSnapshot)


    'If no data, don't bother opening Excel, just quit
    If rsBS.RecordCount = 0 Then
        MsgBox "No data selected for export", vbInformation + vbOKOnly, "No data exported"
        GoTo SubExit
    End If
    '*********************************************
    '             BUILD SPREADSHEET
    '*********************************************
    'Create an instance of Excel and start building a spreadsheet

    'Early Binding
    Set xlApp = Excel.Application

    xlApp.Visible = False
    Set xlBook = xlApp.Workbooks.Add
    Set xlSheet = xlBook.Worksheets(1)
    Range("A4").Select
    ActiveWindow.FreezePanes = True
' Here I try to remove grid
    xlSheet.Activate
    ActiveWindow.DisplayGridlines = False

With xlSheet
        .Name = "IMPORT BLss"
        .Cells.Font.Name = "Calibri"
        .Cells.Font.Size = 10

        'Format Labels
        .Range("A1").Value = "ID"
        .Range("B1").Value = "BK"
        .Range("C1").Value = "FREIGHT"
        .Range("D1").Value = "CURRENCY"

        'provide initial value to row counter
        i = 2
        'Loop through recordset and copy data from recordset to sheet
        Do While Not rsBS.EOF

    .Range("A" & i).Value = Nz(rsBS!IDM, "")
    .Range("B" & i).Value = Nz(rsBS!bk, "")
    .Range("C" & i).Value = Nz(rsBS!freight, "")
    .Range("D" & i).Value = Nz(rsBS!curre, "")

            i = i + 1
            rsBS.MoveNext

            Loop

End With

SubExit:
On Error Resume Next
    DoCmd.Hourglass False
    xlApp.Visible = True
    rsBS.Close
    Set rsBS = Nothing
    Exit Sub

SubError:
    MsgBox "Error Number: " & Err.Number & "= " & Err.Description, vbCritical + vbOKOnly, _
        "An error occurred"
    GoTo SubExit

End Sub
 

Gasman

Enthusiastic Amateur
Local time
Today, 11:12
Joined
Sep 21, 2011
Messages
14,301
Just backup the template?
 

Leo_Polla_Psemata

Registered User.
Local time
Today, 03:12
Joined
Mar 24, 2014
Messages
364
Just backup the template?
I have back up, however, is there any way we use just ONE application?
I may move the application to another driver, for example.
 

Gasman

Enthusiastic Amateur
Local time
Today, 11:12
Joined
Sep 21, 2011
Messages
14,301
Well using Word is not keeping it within the application is it?
You could look at creating a report that would mimic the word document? That would then keep it within the application.
 

Leo_Polla_Psemata

Registered User.
Local time
Today, 03:12
Joined
Mar 24, 2014
Messages
364
Well using Word is not keeping it within the application is it?
You could look at creating a report that would mimic the word document? That would then keep it within the application.
Yes, but this will allow export as RTF document and several report properties will be gone, the document won't look the same
 

Minty

AWF VIP
Local time
Today, 11:12
Joined
Jul 26, 2013
Messages
10,371
If you print/export your report to PDF it will remain exactly as on screen, unless you need your users to edit it further.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 06:12
Joined
Feb 19, 2002
Messages
43,275
is there any way we use just ONE application?
What are you asking? Are you asking how to create a Word document with Access? You can't. But if you want to, you can create a text file. You can directly create an .rtf because it is a plain text file. I've created them from COBOL. It is just like writing to a .txt file except you have to include formatting. The .rtf format is pretty flexible but the built in Access to .rtf doesn't support all the formatting codes which makes this export less than desirable.

I don't know if there is a newer spec. I haven't done this in a while.
 

Attachments

  • RTF spec for 2003-----222118993.pdf
    1.3 MB · Views: 213
  • RTF Pocket Guide.zip
    1.1 MB · Views: 134

theDBguy

I’m here to help
Staff member
Local time
Today, 03:12
Joined
Oct 29, 2018
Messages
21,473
Hi. Pardon me for jumping in, but have you also considered storing the master template in an Attachment field in your Access database? You can extract it from your database every time you need to use it. So, no need to store the Word template anywhere else to use your db.

Just a thought...
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 06:12
Joined
May 21, 2018
Messages
8,529
 

strive4peace

AWF VIP
Local time
Today, 05:12
Joined
Apr 3, 2020
Messages
1,004
@Leo_Polla_Psemata, I did a presentation about creating Word documents from Access without a template. Links to the download files are in the video description. There is lots of code you can reference and a sample database with more code.

Word Automation from Access VBA Deep Dive by Crystal Long (strive4peace) (52:28)
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 18:12
Joined
May 7, 2009
Messages
19,243
i must use and keep in a safe place this word template, if for some reason, someone removes this template,
then i must create a new one from scratch.
you can save your "template" in an Attachment field.
extract the template anytime you want to export your recordset to word.
 

Leo_Polla_Psemata

Registered User.
Local time
Today, 03:12
Joined
Mar 24, 2014
Messages
364
@Leo_Polla_Psemata, I did a presentation about creating Word documents from Access without a template. Links to the download files are in the video description. There is lots of code you can reference and a sample database with more code.

Word Automation from Access VBA Deep Dive by Crystal Long (strive4peace) (52:28)
Hi, i will go through this one later today, if one hour long, i need a weekend .
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 18:12
Joined
May 7, 2009
Messages
19,243
leo, you can also experiment with this one.
 

Attachments

  • Format2(1).accdb
    800 KB · Views: 156

Users who are viewing this thread

Top Bottom