Group:  Microsoft Access ยป microsoft.public.access.modulescoding
Thread: Working with Excel from Access both in Office 2003

Working with Excel from Access both in Office 2003
PR 12/30/2008 6:29:01 PM
I am trying to delete, open, save and transfer data to excel I am using the
following code but I am getting some errors... could someone suggest what is
wrong with the code...

Many Regards - PR
My Code:
Function export_aga()
On Error GoTo export_aga_Err

Dim db As Database
Dim objApp As Excel.Application
Dim ObjBook As Excel.Workbook
Dim objSheet As Excel.Worksheet
Dim Path As String
Dim xlsfname As String
Dim xltfname As String

Set db = CurrentDb()
Set objApp = New Excel.Application
Set Path = "C:\Documents and Settings\PR\My Documents\Downloads\AWSA Database"
Set xlsfname = aga_export.xls
Set xltfname = aga_export.xlt

'delete the spreadsheet
Kill Path & xlsfname

' create a workbook from the template
Set ObjBook = objApp.Workbook.Open(Path & xltfname)

' save a template as a workbook
objXLBook.SaveAs (Path & xlsfname)
objXLBook.Close

' transfer data to new wookbook
DoCmd.SetWarnings False
DoCmd.TransferSpreadsheet acExport, 8, "qry_AGA_Corps", Path &
xlsfname, ""
DoCmd.SetWarnings True

Set db = Nothing
Set objApp = Nothing
Set ObjBook = Nothing
Set objSheet = Nothing

export_aga_Exit:
Exit Function

export_aga_Err:
MsgBox Error$
Resume export_aga_Exit

End Function
Re: Working with Excel from Access both in Office 2003
"Douglas J. Steele" <NOSPAM_djsteele[ at ]NOSPAM_gmail.com> 12/30/2008 6:35:48 PM
It would help to know what the errors you're getting are...

Some things that jump out at me are:

- you don't need the Set keyword when defining Path, xlsfname and xltfname
- you need a slash at the end of the definition of Path.
- you need quotes around the values for xlsfname and xltfname
- check first whether Path & xlsfname exists before issuing the Kill
command:

Path = "C:\Documents and Settings\PR\My Documents\Downloads\AWSA
Database\"
Set xlsfname = "aga_export.xls"
Set xltfname = "aga_export.xlt"

If Len(Dir(Path & xlsfname)) > 0 Then
Kill Path & xlsfname
End If


--
Doug Steele, Microsoft Access MVP
http://I.Am/DougSteele
(no private e-mails, please)


"PR" <PR[ at ]discussions.microsoft.com> wrote in message
news:FDFE2C5C-3D92-4E4C-9349-F23E991D488B[ at ]microsoft.com...
[Quoted Text]
>I am trying to delete, open, save and transfer data to excel I am using the
> following code but I am getting some errors... could someone suggest what
> is
> wrong with the code...
>
> Many Regards - PR
> My Code:
> Function export_aga()
> On Error GoTo export_aga_Err
>
> Dim db As Database
> Dim objApp As Excel.Application
> Dim ObjBook As Excel.Workbook
> Dim objSheet As Excel.Worksheet
> Dim Path As String
> Dim xlsfname As String
> Dim xltfname As String
>
> Set db = CurrentDb()
> Set objApp = New Excel.Application
> Set Path = "C:\Documents and Settings\PR\My Documents\Downloads\AWSA
> Database"
> Set xlsfname = aga_export.xls
> Set xltfname = aga_export.xlt
>
> 'delete the spreadsheet
> Kill Path & xlsfname
>
> ' create a workbook from the template
> Set ObjBook = objApp.Workbook.Open(Path & xltfname)
>
> ' save a template as a workbook
> objXLBook.SaveAs (Path & xlsfname)
> objXLBook.Close
>
> ' transfer data to new wookbook
> DoCmd.SetWarnings False
> DoCmd.TransferSpreadsheet acExport, 8, "qry_AGA_Corps", Path &
> xlsfname, ""
> DoCmd.SetWarnings True
>
> Set db = Nothing
> Set objApp = Nothing
> Set ObjBook = Nothing
> Set objSheet = Nothing
>
> export_aga_Exit:
> Exit Function
>
> export_aga_Err:
> MsgBox Error$
> Resume export_aga_Exit
>
> End Function


Re: Working with Excel from Access both in Office 2003
PR 12/30/2008 7:34:12 PM
Doug,
sorry I did get a few, which your answer as resolved...

I get one error here: Dim objApp As Excel.Application, which gives me a
compile error: user-defined type not defined...

Regards - Paul


"Douglas J. Steele" wrote:

[Quoted Text]
> It would help to know what the errors you're getting are...
>
> Some things that jump out at me are:
>
> - you don't need the Set keyword when defining Path, xlsfname and xltfname
> - you need a slash at the end of the definition of Path.
> - you need quotes around the values for xlsfname and xltfname
> - check first whether Path & xlsfname exists before issuing the Kill
> command:
>
> Path = "C:\Documents and Settings\PR\My Documents\Downloads\AWSA
> Database\"
> Set xlsfname = "aga_export.xls"
> Set xltfname = "aga_export.xlt"
>
> If Len(Dir(Path & xlsfname)) > 0 Then
> Kill Path & xlsfname
> End If
>
>
> --
> Doug Steele, Microsoft Access MVP
> http://I.Am/DougSteele
> (no private e-mails, please)
>
>
> "PR" <PR[ at ]discussions.microsoft.com> wrote in message
> news:FDFE2C5C-3D92-4E4C-9349-F23E991D488B[ at ]microsoft.com...
> >I am trying to delete, open, save and transfer data to excel I am using the
> > following code but I am getting some errors... could someone suggest what
> > is
> > wrong with the code...
> >
> > Many Regards - PR
> > My Code:
> > Function export_aga()
> > On Error GoTo export_aga_Err
> >
> > Dim db As Database
> > Dim objApp As Excel.Application
> > Dim ObjBook As Excel.Workbook
> > Dim objSheet As Excel.Worksheet
> > Dim Path As String
> > Dim xlsfname As String
> > Dim xltfname As String
> >
> > Set db = CurrentDb()
> > Set objApp = New Excel.Application
> > Set Path = "C:\Documents and Settings\PR\My Documents\Downloads\AWSA
> > Database"
> > Set xlsfname = aga_export.xls
> > Set xltfname = aga_export.xlt
> >
> > 'delete the spreadsheet
> > Kill Path & xlsfname
> >
> > ' create a workbook from the template
> > Set ObjBook = objApp.Workbook.Open(Path & xltfname)
> >
> > ' save a template as a workbook
> > objXLBook.SaveAs (Path & xlsfname)
> > objXLBook.Close
> >
> > ' transfer data to new wookbook
> > DoCmd.SetWarnings False
> > DoCmd.TransferSpreadsheet acExport, 8, "qry_AGA_Corps", Path &
> > xlsfname, ""
> > DoCmd.SetWarnings True
> >
> > Set db = Nothing
> > Set objApp = Nothing
> > Set ObjBook = Nothing
> > Set objSheet = Nothing
> >
> > export_aga_Exit:
> > Exit Function
> >
> > export_aga_Err:
> > MsgBox Error$
> > Resume export_aga_Exit
> >
> > End Function
>
>
>
Re: Working with Excel from Access both in Office 2003
"PR" <paul.raeburn[ at ]vodafone.net> 12/30/2008 7:47:44 PM
Doug,

I found the reason for the last error... I did not reference the excel
objects

I also get an error at:

Set xlsfname = "aga_export.xls"

Set xltfname = "aga_export.xlt"

Stating object required

regards


Re: Working with Excel from Access both in Office 2003
"Douglas J. Steele" <NOSPAM_djsteele[ at ]NOSPAM_gmail.com> 12/31/2008 12:36:06 AM
My first post mentioned that you shouldn't use the Set keyword on those two
lines.

--
Doug Steele, Microsoft Access MVP
http://I.Am/DougSteele
(no private e-mails, please)


"PR" <paul.raeburn[ at ]vodafone.net> wrote in message
news:%23yyF%23draJHA.5392[ at ]TK2MSFTNGP02.phx.gbl...
[Quoted Text]
> Doug,
>
> I found the reason for the last error... I did not reference the excel
> objects
>
> I also get an error at:
>
> Set xlsfname = "aga_export.xls"
>
> Set xltfname = "aga_export.xlt"
>
> Stating object required
>
> regards
>
>


Home | Search | Terms | Imprint
Newsgroups Reader