Open new then save with name

Open new then save with name

Anonymous
Not applicable
528 Views
9 Replies
Message 1 of 10

Open new then save with name

Anonymous
Not applicable

With the following code I can end up opening a new
excel file.  Now I would like to save that file.  How do I do
that?  I have looked in excel vba help with no help from it.  I'm
doing this from an ACAD vba module.


--
 
Thanks,
David M.
Gardner
Change the DOT to reply
0 Likes
529 Views
9 Replies
Replies (9)
Message 2 of 10

fxcastil
Advocate
Advocate
David,

There are three methods for saving an excel file/workbook
Save, SaveAs,SaveCopyAs

ActiveWorkbook.Save
WorkBook.SaveAs Filename:=NewFileName
ActiveWorkbook.SaveCopyAs "C:\TEMP\XXXX.XLS"

In addition to close a workbook with out saving use
Workbooks("BOOK1.XLS").Close SaveChanges:=False

To find methods these use the object browser in Excel IDE (VBA) environment

Fred Castillo
0 Likes
Message 3 of 10

Anonymous
Not applicable

The following is a excerpt from Excel 2003 VBA Help
for Workbook.SaveAs



Saves changes to the workbook in a different file.


expression.SaveAs(
class=bterm>FileName
, FileFormat,
class=bterm>Password
, WriteResPassword,
class=bterm>ReadOnlyRecommended
,
class=bterm>CreateBackup
, AccessMode,
class=bterm>ConflictResolution
, AddToMru,
TextCodepage,
class=bterm>TextVisualLayout
,
class=bterm>Local
)


expression    Required. An expression that
returns one of the above objects.


Filename   
Optional Variant. A string that indicates the name of the
file to be saved. You can include a full path; if you don't, Microsoft Excel
saves the file in the current folder.


FileFormat   
Optional Variant. The file format to use when you save the
file. For a list of valid choices, see the

href="mk:@MSITStore:C:\Program%20Files\Microsoft%20Office\OFFICE11\1033\vbaxl10.chm::/html/xlproFil...

property. For an existing file, the default format is the last file format
specified; for a new file, the default is the format of the version of Excel
being used.


Password   
Optional Variant. A case-sensitive string (no more than 15
characters) that indicates the protection password to be given to the
file.


WriteResPassword   
Optional Variant. A string that indicates the
write-reservation password for this file. If a file is saved with the password
and the password isn't supplied when the file is opened, the file is opened as
read-only.



class=runinhead>ReadOnlyRecommended
    Optional
class=bterm>Variant
. True to display a message when the
file is opened, recommending that the file be opened as read-only.


CreateBackup   
Optional Variant. True to create a backup
file.



class=runinhead>AccessMode
   Optional

class=DropDown onkeypress=Outline2() onclick=Outline2()
href="mk:@MSITStore:C:\Program%20Files\Mic...

size=1>.




class=runinhead>ConflictResolution
    Optional

href="mk:@MSITStore:C:\Program%20Files\Microsoft%20Office\OFFICE11\1033\vbaxl10.chm::/html/xlmthSav...

size=1>.



AddToMru   
Optional Variant. True to add this
workbook to the list of recently used files. The default value is
class=bterm>False
.


TextCodePage   
Optional Variant. Not used in U.S. English Microsoft
Excel.


TextVisualLayout   
Optional Variant. Not used in U.S. English Microsoft
Excel.


Local   Optional
class=bterm>Variant
. True saves files against the
language of Microsoft Excel (including control panel settings).
class=bterm>False
(default) saves files against the language of

class=glossary onkeypress="AppendPopup(this,'ofVBA')"
onclick="AppendPopup(this,'ofVBA')"
href="m...
(which is
typically US English unless the VBA project where Workbooks.Open is run from is
an old internationalized XL5/95 VBA project).


Hopefully this helps.


--
Phil Custer II, P.E.
Custer Services,
Inc.

href="mailto:custer@landfillgas.com">custer@landfillgas.com


 



style="PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">


With the following code I can end up opening a
new excel file.  Now I would like to save that file.  How do I do
that?  I have looked in excel vba help with no help from it.  I'm
doing this from an ACAD vba module.


--
 
Thanks,
David M.
Gardner
Change the DOT to reply
0 Likes
Message 4 of 10

Anonymous
Not applicable
I'm using this code:


Dim excelApp As Object
Dim wbkObj As Object
Dim shtObj As Object

On Error Resume Next
Set excelApp = GetObject(, "Excel.Application")
If Err <> 0 Then
Err.Clear
Set excelApp = CreateObject("Excel.Application")
If Err <> 0 Then
MsgBox "Could not start Excel", vbExclamation
End
End If
End If
excelApp.Visible = True
Set wbkObj = Workbooks.Open(FileName:=strDWGIndex)
If Workbooks.COUNT = "0" Then
Workbooks.Add ("X:\drawing index.xls")
Workbooks.SaveAs FileName:=strDWGIndex
End If
Set wbkObj = Nothing
Set excelApp = Nothing


and I get this error highlighting the ".saveas"



--

Thanks,
David M. Gardner
Change the DOT to reply
wrote in message news:5088689@discussion.autodesk.com...
David,

There are three methods for saving an excel file/workbook
Save, SaveAs,SaveCopyAs

ActiveWorkbook.Save
WorkBook.SaveAs Filename:=NewFileName
ActiveWorkbook.SaveCopyAs "C:\TEMP\XXXX.XLS"

In addition to close a workbook with out saving use
Workbooks("BOOK1.XLS").Close SaveChanges:=False

To find methods these use the object browser in Excel IDE (VBA) environment

Fred Castillo
0 Likes
Message 5 of 10

Anonymous
Not applicable
Here is the problem:

If Workbooks.COUNT = "0" Then


Workbooks.Count is a number value, not a text string value. That is why
the...Add() and ...SaveAs() are never executed. It should be:

If Workbooks.Count=0 Then...

The other problen is to use "On Error Resume Next" without trying to handle
possible error.

In you case, the statement

If Workbooks.Count="0"

generates a error fo "Type misnatch", but the process simply RESUMEs to next
line and continue, so you do not notice something wrong has happened.

My thumb of rule is to not use "On Error Resume Next" unless you try to
examine Err object is each following line of code that could reuilt in
runtime exception.

"David M. Gardner" wrote in message
news:5088791@discussion.autodesk.com...
I'm using this code:


Dim excelApp As Object
Dim wbkObj As Object
Dim shtObj As Object

On Error Resume Next
Set excelApp = GetObject(, "Excel.Application")
If Err <> 0 Then
Err.Clear
Set excelApp = CreateObject("Excel.Application")
If Err <> 0 Then
MsgBox "Could not start Excel", vbExclamation
End
End If
End If
excelApp.Visible = True
Set wbkObj = Workbooks.Open(FileName:=strDWGIndex)
I
f Workbooks.COUNT = "0" Then
Workbooks.Add ("X:\drawing index.xls")
Workbooks.SaveAs FileName:=strDWGIndex
End If
Set wbkObj = Nothing
Set excelApp = Nothing


and I get this error highlighting the ".saveas"



--

Thanks,
David M. Gardner
Change the DOT to reply
wrote in message news:5088689@discussion.autodesk.com...
David,

There are three methods for saving an excel file/workbook
Save, SaveAs,SaveCopyAs

ActiveWorkbook.Save

WorkBook.SaveAs Filename:=NewFileName
ActiveWorkbook.SaveCopyAs "C:\TEMP\XXXX.XLS"

In addition to close a workbook with out saving use
Workbooks("BOOK1.XLS").Close SaveChanges:=False

To find methods these use the object browser in Excel IDE (VBA) environment

Fred Castillo
0 Likes
Message 6 of 10

Anonymous
Not applicable
Thank you for that bit of info. But that does not answer/fix the error I
get. If you can look at my 2nd post that has the error attached.

--

Thanks,
David M. Gardner
Change the DOT to reply
"Norman Yuan" wrote in message
news:5089506@discussion.autodesk.com...
Here is the problem:

If Workbooks.COUNT = "0" Then


Workbooks.Count is a number value, not a text string value. That is why
the...Add() and ...SaveAs() are never executed. It should be:

If Workbooks.Count=0 Then...

The other problen is to use "On Error Resume Next" without trying to handle
possible error.

In you case, the statement

If Workbooks.Count="0"

generates a error fo "Type misnatch", but the process simply RESUMEs to next
line and continue, so you do not notice something wrong has happened.

My thumb of rule is to not use "On Error Resume Next" unless you try to
examine Err object is each following line of code that could reuilt in
runtime exception.

"David M. Gardner" wrote in message
news:5088791@discussion.autodesk.com...
I'm using this code:


Dim excelApp As Object
Dim wbkObj As Object
Dim shtObj As Object

On Error Resume Next
Set excelApp = GetObject(, "Excel.Application")
If Err <> 0 Then
Err.Clear
Set excelApp = CreateObject("Excel.Application")
If Err <> 0 Then
MsgBox "Could not start Excel", vbExclamation
End
End If
End If
excelApp.Visible = True
Set wbkObj = Workbooks.Open(FileName:=strDWGIndex)
I
f Workbooks.COUNT = "0" Then
Workbooks.Add ("X:\drawing index.xls")
Workbooks.SaveAs FileName:=strDWGIndex
End If
Set wbkObj = Nothing
Set excelApp = Nothing


and I get this error highlighting the ".saveas"



--

Thanks,
David M. Gardner
Change the DOT to reply
wrote in message news:5088689@discussion.autodesk.com...
David,

There are three methods for saving an excel file/workbook
Save, SaveAs,SaveCopyAs

ActiveWorkbook.Save

WorkBook.SaveAs Filename:=NewFileName
ActiveWorkbook.SaveCopyAs "C:\TEMP\XXXX.XLS"

In addition to close a workbook with out saving use
Workbooks("BOOK1.XLS").Close SaveChanges:=False

To find methods these use the object browser in Excel IDE (VBA) environment

Fred Castillo
0 Likes
Message 7 of 10

Anonymous
Not applicable
Ah, you may want to try to qualify the Workbooks with excelApp. That is,

Replace:

Workbooks....

with:

excelApp.Workbooks...


"David M. Gardner" wrote in message
news:5091764@discussion.autodesk.com...
Thank you for that bit of info. But that does not answer/fix the error I
get. If you can look at my 2nd post that has the error attached.

--

Thanks,
David M. Gardner
Change the DOT to reply
"Norman Yuan" wrote in message
news:5089506@discussion.autodesk.com...
Here is the problem:

If Workbooks.COUNT = "0" Then


Workbooks.Count is a number value, not a text string value. That is why
the...Add() and ...SaveAs() are never executed. It should be:

If Workbooks.Count=0 Then...

The other problen is to use "On Error Resume Next" without trying to handle
possible error.

In you case, the statement

If Workbooks.Count="0"

generates a error fo "Type misnatch", but the process simply RESUMEs to next
line and continue, so you do not notice something wrong has happened.

My thumb of rule is to not use "On Error Resume Next" unless you try to
examine Err object is each following line of code that could reuilt in
runtime exception.

"David M. Gardner" wrote in message
news:5088791@discussion.autodesk.com...
I'm using this code:


Dim excelApp As Object
Dim wbkObj As Object
Dim shtObj As Object

On Error Resume Next
Set excelApp = GetObject(, "Excel.Application")
If Err <> 0 Then
Err.Clear
Set excelApp = CreateObject("Excel.Application")
If Err <> 0 Then
MsgBox "Could not start Excel", vbExclamation
End
End If
End If
excelApp.Visible = True
Set wbkObj = Workbooks.Open(FileName:=strDWGIndex)
I
f Workbooks.COUNT = "0" Then
Workbooks.Add ("X:\drawing index.xls")
Workbooks.SaveAs FileName:=strDWGIndex
End If
Set wbkObj = Nothing
Set excelApp = Nothing


and I get this error highlighting the ".saveas"



--

Thanks,
David M. Gardner
Change the DOT to reply
wrote in message news:5088689@discussion.autodesk.com...
David,

There are three methods for saving an excel file/workbook
Save, SaveAs,SaveCopyAs

ActiveWorkbook.Save

WorkBook.SaveAs Filename:=NewFileName
ActiveWorkbook.SaveCopyAs "C:\TEMP\XXXX.XLS"

In addition to close a workbook with out saving use
Workbooks("BOOK1.XLS").Close SaveChanges:=False

To find methods these use the object browser in Excel IDE (VBA) environment

Fred Castillo
0 Likes
Message 8 of 10

Anonymous
Not applicable
OK. I don't get the error anymore but it does not save the file.

--

Thanks,
David M. Gardner
Change the DOT to reply
"Norman Yuan" wrote in message
news:5091822@discussion.autodesk.com...
Ah, you may want to try to qualify the Workbooks with excelApp. That is,

Replace:

Workbooks....

with:

excelApp.Workbooks...


"David M. Gardner" wrote in message
news:5091764@discussion.autodesk.com...
Thank you for that bit of info. But that does not answer/fix the error I
get. If you can look at my 2nd post that has the error attached.

--

Thanks,
David M. Gardner
Change the DOT to reply
"Norman Yuan" wrote in message
news:5089506@discussion.autodesk.com...
Here is the problem:

If Workbooks.COUNT = "0" Then


Workbooks.Count is a number value, not a text string value. That is why
the...Add() and ...SaveAs() are never executed. It should be:

If Workbooks.Count=0 Then...

The other problen is to use "On Error Resume Next" without trying to handle
possible error.

In you case, the statement

If Workbooks.Count="0"

generates a error fo "Type misnatch", but the process simply RESUMEs to next
line and continue, so you do not notice something wrong has happened.

My thumb of rule is to not use "On Error Resume Next" unless you try to
examine Err object is each following line of code that could reuilt in
runtime exception.

"David M. Gardner" wrote in message
news:5088791@discussion.autodesk.com...
I'm using this code:


Dim excelApp As Object
Dim wbkObj As Object
Dim shtObj As Object

On Error Resume Next
Set excelApp = GetObject(, "Excel.Application")
If Err <> 0 Then
Err.Clear
Set excelApp = CreateObject("Excel.Application")
If Err <> 0 Then
MsgBox "Could not start Excel", vbExclamation
End
End If
End If
excelApp.Visible = True
Set wbkObj = Workbooks.Open(FileName:=strDWGIndex)
I
f Workbooks.COUNT = "0" Then
Workbooks.Add ("X:\drawing index.xls")
Workbooks.SaveAs FileName:=strDWGIndex
End If
Set wbkObj = Nothing
Set excelApp = Nothing


and I get this error highlighting the ".saveas"



--

Thanks,
David M. Gardner
Change the DOT to reply
wrote in message news:5088689@discussion.autodesk.com...
David,

There are three methods for saving an excel file/workbook
Save, SaveAs,SaveCopyAs

ActiveWorkbook.Save

WorkBook.SaveAs Filename:=NewFileName
ActiveWorkbook.SaveCopyAs "C:\TEMP\XXXX.XLS"

In addition to close a workbook with out saving use
Workbooks("BOOK1.XLS").Close SaveChanges:=False

To find methods these use the object browser in Excel IDE (VBA) environment

Fred Castillo
0 Likes
Message 9 of 10

Anonymous
Not applicable
Here is revised code:

Dim excelApp As Object
Dim wbkObj As Object
Dim shtObj As Object

On Error Resume Next
Set excelApp = GetObject(, "Excel.Application")
If Err.Number <> 0 Then
Err.Clear
Set excelApp = CreateObject("Excel.Application")
If Err.Number <> 0 Then
MsgBox "Could not start Excel", vbExclamation
''End
Exit Sub 'or Function. NEVER use "End"
End If
End If
excelApp.Visible = True

'' if the file strDWGIndex refers to does not exist, you will get error
on next line
Set wbkObj = excelApp.Workbooks.Open(FileName:=strDWGIndex)

''So handle the error if file not found
If Err.Number<>0 Then
If excelApp.Workbooks.COUNT = 0 Then
Set wbkObj=Workbooks.Add ("X:\drawing index.xls") ' 'You must
guarrantee "X:\drawing Index.xls" always exists
''Workbooks.SaveAs FileName:=strDWGIndex ''Workbooks does not
have a method called SaveAs(), Workbook does
wbkObj.SaveAs FileName:=strDWGIndex
End If
End If

'if you want Excel and the workbook to stay open. that is fine.
Otherwise, you should close it

'wbkObj.Close False ''Save/not save while closing
'excelApp.Quit ''Quit Excel application

Set wbkObj = Nothing
Set excelApp = Nothing


"David M. Gardner" wrote in message
news:5091937@discussion.autodesk.com...
OK. I don't get the error anymore but it does not save the file.

--

Thanks,
David M. Gardner
Change the DOT to reply
"Norman Yuan" wrote in message
news:5091822@discussion.autodesk.com...
Ah, you may want to try to qualify the Workbooks with excelApp. That is,

Replace:

Workbooks....

with:

excelApp.Workbooks...


"David M. Gardner" wrote in message
news:5091764@discussion.autodesk.com...
Thank you for that bit of info. But that does not answer/fix the error I
get. If you can look at my 2nd post that has the error attached.

--

Thanks,
David M. Gardner
Change the DOT to reply
"Norman Yuan" wrote in message
news:5089506@discussion.autodesk.com...
Here is the problem:

If Workbooks.COUNT = "0" Then


Workbooks.Count is a number value, not a text string value. That is why
the...Add() and ...SaveAs() are never executed. It should be:

If Workbooks.Count=0 Then...

The other problen is to use "On Error Resume Next" without trying to handle
possible error.

In you case, the statement

If Workbooks.Count="0"

generates a error fo "Type misnatch", but the process simply RESUMEs to next
line and continue, so you do not notice something wrong has happened.

My thumb of rule is to not use "On Error Resume Next" unless you try to
examine Err object is each following line of code that could reuilt in
runtime exception.

"David M. Gardner" wrote in message
news:5088791@discussion.autodesk.com...
I'm using this code:


Dim excelApp As Object
Dim wbkObj As Object
Dim shtObj As Object

On Error Resume Next
Set excelApp = GetObject(, "Excel.Application")
If Err <> 0 Then
Err.Clear
Set excelApp = CreateObject("Excel.Application")
If Err <> 0 Then
MsgBox "Could not start Excel", vbExclamation
End
End If
End If
excelApp.Visible = True
Set wbkObj = Workbooks.Open(FileName:=strDWGIndex)
I
f Workbooks.COUNT = "0" Then
Workbooks.Add ("X:\drawing index.xls")
Workbooks.SaveAs FileName:=strDWGIndex
End If
Set wbkObj = Nothing
Set excelApp = Nothing


and I get this error highlighting the ".saveas"



--

Thanks,
David M. Gardner
Change the DOT to reply
wrote in message news:5088689@discussion.autodesk.com...
David,

There are three methods for saving an excel file/workbook
Save, SaveAs,SaveCopyAs

ActiveWorkbook.Save

WorkBook.SaveAs Filename:=NewFileName
ActiveWorkbook.SaveCopyAs "C:\TEMP\XXXX.XLS"

In addition to close a workbook with out saving use
Workbooks("BOOK1.XLS").Close SaveChanges:=False

To find methods these use the object browser in Excel IDE (VBA) environment

Fred Castillo
0 Likes
Message 10 of 10

Anonymous
Not applicable
Thank you.

--

Thanks,
David M. Gardner
Change the DOT to reply
"Norman Yuan" wrote in message
news:5092347@discussion.autodesk.com...
Here is revised code:

Dim excelApp As Object
Dim wbkObj As Object
Dim shtObj As Object

On Error Resume Next
Set excelApp = GetObject(, "Excel.Application")
If Err.Number <> 0 Then
Err.Clear
Set excelApp = CreateObject("Excel.Application")
If Err.Number <> 0 Then
MsgBox "Could not start Excel", vbExclamation
''End
Exit Sub 'or Function. NEVER use "End"
End If
End If
excelApp.Visible = True

'' if the file strDWGIndex refers to does not exist, you will get error
on next line
Set wbkObj = excelApp.Workbooks.Open(FileName:=strDWGIndex)

''So handle the error if file not found
If Err.Number<>0 Then
If excelApp.Workbooks.COUNT = 0 Then
Set wbkObj=Workbooks.Add ("X:\drawing index.xls") ' 'You must
guarrantee "X:\drawing Index.xls" always exists
''Workbooks.SaveAs FileName:=strDWGIndex ''Workbooks does not
have a method called SaveAs(), Workbook does
wbkObj.SaveAs FileName:=strDWGIndex
End If
End If

'if you want Excel and the workbook to stay open. that is fine.
Otherwise, you should close it

'wbkObj.Close False ''Save/not save while closing
'excelApp.Quit ''Quit Excel application

Set wbkObj = Nothing
Set excelApp = Nothing


"David M. Gardner" wrote in message
news:5091937@discussion.autodesk.com...
OK. I don't get the error anymore but it does not save the file.

--

Thanks,
David M. Gardner
Change the DOT to reply
"Norman Yuan" wrote in message
news:5091822@discussion.autodesk.com...
Ah, you may want to try to qualify the Workbooks with excelApp. That is,

Replace:

Workbooks....

with:

excelApp.Workbooks...


"David M. Gardner" wrote in message
news:5091764@discussion.autodesk.com...
Thank you for that bit of info. But that does not answer/fix the error I
get. If you can look at my 2nd post that has the error attached.

--

Thanks,
David M. Gardner
Change the DOT to reply
"Norman Yuan" wrote in message
news:5089506@discussion.autodesk.com...
Here is the problem:

If Workbooks.COUNT = "0" Then


Workbooks.Count is a number value, not a text string value. That is why
the...Add() and ...SaveAs() are never executed. It should be:

If Workbooks.Count=0 Then...

The other problen is to use "On Error Resume Next" without trying to handle
possible error.

In you case, the statement

If Workbooks.Count="0"

generates a error fo "Type misnatch", but the process simply RESUMEs to next
line and continue, so you do not notice something wrong has happened.

My thumb of rule is to not use "On Error Resume Next" unless you try to
examine Err object is each following line of code that could reuilt in
runtime exception.

"David M. Gardner" wrote in message
news:5088791@discussion.autodesk.com...
I'm using this code:


Dim excelApp As Object
Dim wbkObj As Object
Dim shtObj As Object

On Error Resume Next
Set excelApp = GetObject(, "Excel.Application")
If Err <> 0 Then
Err.Clear
Set excelApp = CreateObject("Excel.Application")
If Err <> 0 Then
MsgBox "Could not start Excel", vbExclamation
End
End If
End If
excelApp.Visible = True
Set wbkObj = Workbooks.Open(FileName:=strDWGIndex)
I
f Workbooks.COUNT = "0" Then
Workbooks.Add ("X:\drawing index.xls")
Workbooks.SaveAs FileName:=strDWGIndex
End If
Set wbkObj = Nothing
Set excelApp = Nothing


and I get this error highlighting the ".saveas"



--

Thanks,
David M. Gardner
Change the DOT to reply
wrote in message news:5088689@discussion.autodesk.com...
David,

There are three methods for saving an excel file/workbook
Save, SaveAs,SaveCopyAs

ActiveWorkbook.Save

WorkBook.SaveAs Filename:=NewFileName
ActiveWorkbook.SaveCopyAs "C:\TEMP\XXXX.XLS"

In addition to close a workbook with out saving use
Workbooks("BOOK1.XLS").Close SaveChanges:=False

To find methods these use the object browser in Excel IDE (VBA) environment

Fred Castillo
0 Likes