Wow, thank you very much for your interest and suggestions. I unsestannd that there is a lot to be learned.
Yes, I tried with CopyFromRecordset, In my limited understanding, this does not copy the header row, so you have to insert in "manually"
Code:
With rst
If .RecordCount >= 1 Then
For iCols = 0 To rst.Fields.Count - 1
oExcelWrSht.Cells(1, iCols + 1).Value = rst.Fields(iCols).Name
Next
oExcelWrSht.Range("A2").CopyFromRecordset rst
End If
but I can be wrong, of course.
I am intrigued by the report suggestion. What my code does is to fill a welcome letter to new members of our little "elder club", and talking about "letter" what comes to mind is Word, and I did not think of access report. Is really possible to turn the "letter" into a report? Itr is true that all the mailmerge stuff is eliminated, but the formatting?
madpiet: I found this code from dr. Kalla. Is this you are referring to?
Code:
Function RidesMergeWord(strDocName As String, _
strDataDir As String, _
Optional strOutDocName As String)
' This code takes a word document that has been setup as a MERGE document.
' This merge document is opened, then mailmerge is executed. The original
' document is then closed. The result is a raw word document with no connectons
' to the merge.txt (a csv source data file).
'Parms:
' strDocName - full path name of word doc (.doc)
' strDataDir - dir (full path) where docuemnts and the merge.888 file is placed
' strOutDocName - full path name of merged document (saved).
'
' The above parms are suppled by other routines. You likey should not need to call this
' routine directly. See the sub called MergeNoPrompts.
' Albert D. Kallal (c) 2001
' kalla@msn.com
'
Dim WordApp As Object ' running instance of word
Dim WordDoc As Object ' one instance of a word doc
Dim strActiveDoc As String ' doc name (no path)
Dim lngWordDest As Long ' const for dest, 0 = new doc, 1 = printer
Dim MyPbar As New clsRidesPBar ' create a instance of our Progress bar.
MyPbar.ShowProgress
MyPbar.TextMsg = "Launching Word...please wait..."
MyPbar.Pmax = 4 ' 4 steps to inc
MyPbar.IncOne ' step 1....start!
On Error GoTo CreateWordApp
Set WordApp = GetObject(, "Word.Application")
On Error Resume Next
MyPbar.IncOne ' step 2, word is loaded.
Set WordDoc = WordApp.Documents.Open(strDocName)
MyPbar.IncOne ' step 3, doc is loaded
strActiveDoc = WordApp.ActiveDocument.Name
WordDoc.MailMerge.OpenDataSource _
Name:=strDataDir & TextMerge, _
ConfirmConversions:=False, _
ReadOnly:=False, LinkToSource:=True, AddToRecentFiles:=False, _
PasswordDocument:="", PasswordTemplate:="", WritePasswordDocument:="", _
WritePasswordTemplate:="", Revert:=False, Format:=0, _
Connection:="", SQLStatement:="", SQLStatement1:=""
With WordDoc.MailMerge
.Destination = 0 ' 0 = new doc
.MailAsAttachment = False
.MailAddressFieldName = ""
.MailSubject = ""
.SuppressBlankLines = True
With .datasource
.FirstRecord = 1
' .LastRecord = 1
End With
.Execute Pause:=True
End With
MyPbar.IncOne ' step 4, doc is merged
WordDoc.Close (False)
WordApp.Visible = True
WordApp.Windows(WordApp.Windows.Count).Activate
If strOutDocName <> "" Then
WordApp.ActiveDocument.SaveAs strOutDocName
End If
MyPbar.HideProgress
' AppActivate "Microsoft Word"
WordApp.Activate
WordApp.WindowState = 0 'wdWindowStateRestore
Set WordApp = Nothing
Set WordDoc = Nothing
Set MyPbar = Nothing
DoEvents
' If bolShowMerge = True Then
' WordApp.Dialogs(676).Show 'wdDialogMailMerge
' End If
Exit Function
CreateWordApp:
' this code is here to use the EXISTING copy of
' ms-access running. If getobject fails, then
' ms-word was NOT running. The below will then
' launch word
Set WordApp = CreateObject("Word.Application")
Resume Next
End Function
Thank you again to all.