Can't save a spreadsheet via VBA

User avatar
Abraxus
3StarLounger
Posts: 254
Joined: 01 Mar 2010, 17:34
Location: Blue Springs, MO

Can't save a spreadsheet via VBA

Post by Abraxus »

I have the following code to do some really cool stuff with a spreadsheet via VBA from Access 2013:

Code: Select all

Set objxl = CreateObject("excel.application")
objxl.Workbooks.Open FileName:=strPath & "\Template.xlsx"
objxl.Visible = True
objxl.screenupdating = False
objxl.Sheets("TheReport").Select
'Do all the really cool stuff here....
objxl.screenupdating = True
'Save the Spreadsheet
objxl.ActiveWorkbook.SaveAs FileName:="C:\MyFolder\" & Format(Now, "YYYYMMDD") & "_Auction Results.xls", FileFormat:=xlNormal, Password:="", WriteResPassword:="", ReadOnlyRecommended:=False, CreateBackup:=False
When it hits the objxl.ActiveWorkbook.SaveAs it gives me this error: SaveAs method of Workbook class failed

What am I missing?
Morgan

User avatar
HansV
Administrator
Posts: 78512
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Can't save a spreadsheet via VBA

Post by HansV »

Are you using early binding (i.e. you have set a reference to the Microsoft Excel 15.0 Object Library in Tools > References...), or late binding (i.e. no reference)?
Best wishes,
Hans

User avatar
Abraxus
3StarLounger
Posts: 254
Joined: 01 Mar 2010, 17:34
Location: Blue Springs, MO

Re: Can't save a spreadsheet via VBA

Post by Abraxus »

Late binding.
Morgan

User avatar
HansV
Administrator
Posts: 78512
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Can't save a spreadsheet via VBA

Post by HansV »

Thanks. That means that xlNormal is not defined. You can either declare

Const xlNormal = -4143

in your code, or replace xlNormal with -4143 wherever you use it.
Best wishes,
Hans

User avatar
Abraxus
3StarLounger
Posts: 254
Joined: 01 Mar 2010, 17:34
Location: Blue Springs, MO

Re: Can't save a spreadsheet via VBA

Post by Abraxus »

Awesome, thank you!!
Morgan