LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 75
Default unable to create a file if already exiists

I am creating a simple text file. And appending the data to the file. i need
to run the same macro every time.
It is fine when i run the script at least once. When i am running for the
first time it is creating the file and it is showing the file is exist window.
Please help or let me know is there another way of creating a file and error
handling if file exists.

************
Set fso = New FileSystemObject
fso.CreateTextFile fileName, overwrite:=True

If fso.FileExists(fileName) Then
If MsgBox("The file " & fso.GetFileName(fileName) & " already
exists. Do " & _
"you want to replace the existing file?", vbYesNo +
vbExclamation + _
vbDefaultButton2, PROJECT_NAME) = vbNo Then
Exit Sub
End If
End If


Set ts = fso.OpenTextFile(fileName, ForWriting, True)
With Range(ActiveWorkbook.Worksheets("Anvil").Cells(1, dataColumn), _
ActiveWorkbook.Worksheets("Anvil").Cells(rowCount, dataColumn))
For Each tempCell In .Cells

If tempCell.Row < rowCount Then
tempCell.Value = Left(tempCell.Value,
Len(tempCell.Value) - 1)
Call ts.WriteLine(tempCell.Value)
Else
tempCell.Value = Left(tempCell.Value,
Len(tempCell.Value) - 1)
Call ts.Write(tempCell.Value)
End If
Next
End With
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
unable to create a file if already exiists Bobby Excel Discussion (Misc queries) 1 June 18th 09 09:23 PM
unable to create a file if already exiists Bobby Excel Discussion (Misc queries) 0 June 18th 09 06:15 PM
Unable to create/use hyperlinks Siilverain Excel Worksheet Functions 4 April 13th 09 05:09 PM
Unable to click on charts & unable to create any new charts Excel Snapclick Charts and Charting in Excel 4 February 1st 09 07:59 PM
Unable to create any link/hyperlinks in a particular excel file Nabanita Excel Discussion (Misc queries) 0 June 21st 05 11:52 AM


All times are GMT +1. The time now is 08:32 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"