Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to convert .XLS to .CSV using VB 1

Status
Not open for further replies.

basicyen

IS-IT--Management
Oct 4, 2002
15
US
All experts outthere help please..
I am trying to convert excel file to .CSV file(Commad delimeted). I build the scripts from the macro test it and works. But it wont work when embended into VB.
Here is sample of my code

On Error Resume Next
.
.
Set objectExl = GetObject(, "Excel.Application")
If Err.Number <> 0 Then
Set objectExl = CreateObject(&quot;Excel.application&quot;)
blnRunning = False
Else
blnRunning = True
End If
.
.
If objectExl.FileExists &quot;C:\ExcelFile.xls&quot;) Then
objectExl.Workbooks.Open &quot;C:\ExcelFile.Xls&quot;
Sheet= objectExl.Activeworkbooks.sheets(1).Name
Set obWorkSheet = objectExl.Activeworkbooks.sheets(Sheet)
End If

objectExl.ActiveWorkBook.SaveAS FileName:=&quot;H:\yan\VB\bamtrack\amex092002.csv&quot;, FileFormat:=xlCSV, CreateBackup:=False

.
.
.


Please
Help
 
You are missing a &quot;(&quot; in this line:
Code:
If objectExl.FileExists
(
Code:
&quot;C:\ExcelFile.xls&quot;) Then

VBSlammer
redinvader3walking.gif

Unemployed in Houston, Texas
 
VBSlammer.... I see your correction. However, my need is to solve the following statement that still don't work to save from .Xls to .CSV file.

objectExl.ActiveWorkBook.SaveAS
FileName:=&quot;H:\yan\VB\bamtrack\amex092002.csv&quot;, _
FileFormat:=xlCSV, CreateBackup:=False

Please help....

Thanks

 
I don't have any problems using code similar to yours. The only thing I can think of is maybe you're trying to use an invalid path, or the .xls file contains features that cannot be converted to .csv format.

Code:
Sub ConvertToCSV(ByVal strFile As String, ByVal strNewName As String)
On Error GoTo ErrHandler

  Dim xl As Excel.Application
  Dim wb As Excel.Workbook
  Dim sht As Excel.Worksheet
  
  If Dir(strFile) = &quot;&quot; Then GoTo ExitHere
  
  Set xl = New Excel.Application
  
  Set wb = xl.Workbooks.Open(strFile)
  Set sht = wb.Sheets(1)
  
  wb.SaveAs FileName:=strNewName, FileFormat:=xlCSV, CreateBackup:=False
  
  wb.Close True
    
  xl.Quit
  
ExitHere:
  On Error Resume Next
  Set sht = Nothing
  Set wb = Nothing
  Set xl = Nothing
  Exit Sub
ErrHandler:
  MsgBox &quot;Error: &quot; & Err & &quot; - &quot; & Err.Description
  Resume ExitHere
End Sub
VBSlammer
redinvader3walking.gif

Unemployed in Houston, Texas
 
VBSlammer...you have been very helpful and Great. Your reply is very well documented and correct. I utilize it your procedure and it works. Thanks a million.

God Bless

Thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top