Excel crashes with long file names

G

Guest

I am running a macro in Excel 2000 that does a "save as". If the file already
exists and the full filename (including drive and path) is greater than 149
characters, then Excel crashes. Apart from the obvious of shortening the
directory or file names, does anyone know how to stop this happening?

Example code below:

Private Sub CommandButton1_Click()

' This line works (149 characters)
ActiveWorkbook.SaveAs
"C:\abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijkl.xls"

' This line works (150 characters) if file does not already exist
ActiveWorkbook.SaveAs
"C:\abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklm.xls"

' This line crashes (150 characters) if file already exists
ActiveWorkbook.SaveAs
"C:\abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklm.xls"

End Sub
 
J

Jan Karel Pieterse

Hi Confuscious,
Apart from the obvious of shortening the
directory or file names, does anyone know how to stop this happening?

Delete the file you're overwriting first:

On error resume next 'In case file is not there
Kill "c:\YaddaYadda.xls"
Activeworkbook.SaveAs "c:\YaddaYadda.xls"

Regards,

Jan Karel Pieterse
Excel MVP
http://www.jkp-ads.com
Member of:
Professional Office Developer Association
www.proofficedev.com
 
G

Guest

Hi Jan

Thanks for that.

I'd already thought of that one but hadn't included it in the original post.
It will probably be the simplest solution though so I'll probably go with it
anyway.

Confuscious
 
N

NickHK

Confuscious said:
Hi Jan

Thanks for that.

I'd already thought of that one but hadn't included it in the original
post.
It will probably be the simplest solution though so I'll probably go with
it
anyway.

Confuscious
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top