|
发表于 2022-7-5 18:37:01
|
显示全部楼层
This is the Module attached to the Create CSV button in excel.
I tried to add checks along the way to see what was happening.
My goal in part was to stop the error 1004 for coming up while trying to saveas if the csv was already open.
It does seem as though AutoCAD has it open sometimes if loaded manually through the UpdateTitleBlock.lsp with the file name as Nil.
(setq csvfile Nil) ;; e.g. (setq csvfile "C:/myfolder/myfile.csv")
I'm not sure how to determine what autocad does at all, or how it affects the state of the file.
As you may see I have just copied and pasted code from the net and tried to get it to work.
The first section is how it used to be before I started playing with it. (This was a week after the errors occurred so I don't think it caused it at all)
Sub SaveAsCSV()
'
' export Macro
'strName = ThisWorkbook.Path & "\" & "dwgreg.csv"
'Range("3:500").Select
'Selection.Copy
'Workbooks.Add
'ActiveSheet.Paste
'ActiveWorkbook.SaveAs filename:=strName, FileFormat:=xlCSV, CreateBackup:=False
'Application.DisplayAlerts = False
'ActiveWorkbook.Close
'Application.DisplayAlerts = True
'Application.ScreenUpdating = True
'MsgBox "File has been Created and Saved as: " & vbCr & strName, , "Copy & Save Report"
'end Sub
Dim wbS As Workbook
Dim wbS2 As Workbook, wbT2 As Workbook
Dim wsS2 As Worksheet, wsT2 As Worksheet
Set wbS = ThisWorkbook
Set wbS2 = ThisWorkbook 'workbook that holds this code
Set wsS2 = wbS2.ActiveSheet
wsS2.Copy
Set wbT2 = ActiveWorkbook 'assign reference asap
Set wsT2 = wbT2.Worksheets("MRWA")
wsT2.Name = "DRAWING REGISTER" 'rename sheet
wsT2.Range("1:2").Select
Selection.DELETE
ActiveSheet.Shapes.Range(Array("Button 1")).Select
Selection.DELETE
Cells.Select
Selection.ClearFormats
'MsgBox wbS2.Path, , "PATH"
'Test to see if the folder path existsSheets("Drawing Index").
Dim FolderPath2 As String
FolderPath2 = wbS2.path
If Right(FolderPath2, 1) "\" Then
FolderPath2 = FolderPath2 & "\"
End If
If Dir(FolderPath2, vbDirectory) vbNullString Then
MsgBox "Folder exists"
Else
MsgBox "Folder doesn't exist"
End If
'test if file exists
Dim FilePath2 As String
Dim TestStr2 As String
FilePath2 = wbS2.path & "\DWGREG.csv"
'MsgBox FilePath2, , "FILE PATH"
TestStr2 = ""
On Error Resume Next
TestStr2 = Dir(FilePath2)
'MsgBox TestStr2 & "= File Name", , "Test string equals " & TestStr2
On Error GoTo 0
If TestStr2 = "" Then
MsgBox "File doesn't exist", , "FILE NULL"
Else
MsgBox "File exists", , "TestStr2 = " & TestStr2
End If
'Test if file is open
'MsgBox "Is Drawing Open?", vbOKOnly, "TEST"
If bIsBookOpen("DWGREG.csv") Then
MsgBox ("DWGREG.csv is open!" & vbCr + vbCr + "CLOSE THE SPREADSHEET and TRY AGAIN"), , "DRAWING REGISTER IS OPEN"
ActiveWorkbook.Close SaveChanges:=False
Exit Sub
Else
'MsgBox "The Book is not open!", , "DRAWING REGISTER IS NOT OPEN"
End If
Dim InputFolder As String
Dim OutputFolder As String
InputFolder = wbS.path
'MsgBox InputFolder, , "input folder"
'MsgBox ActiveWorkbook.FullName, , "Full Name"
'MsgBox ActiveWorkbook.path, , "Path"
'MsgBox wbS.FullName
'save new workbook
wbT2.SaveAs filename:=wbS.path & "\DWGREG", FileFormat:=6, CreateBackup:=False
Application.DisplayAlerts = False
Application.ScreenUpdating = False
ActiveWorkbook.Close
Application.DisplayAlerts = True
Application.ScreenUpdating = True
'MsgBox wbS.FullName
'MsgBox ActiveWorkbook.Path
strName2 = wbS2.path & "\DWGREG.csv"
MsgBox "File has been Created and Saved as:" & vbCr & strName2, , "COPY & SAVE REPORT"
End Sub |
|