2013-11-14 305 views
0

我从.xlsx创建一个.csv文件,所以我可以使用它从Scribe上传到我的CRM。一切都很好,并且很棒,但是我的.xlsx文件在一些列中静态地使用了100行的下拉列表。所以这个.csv文件有大约90行“,,,,,,”,我不希望它有。以下是我的脚本,它将此xlsx转换为csv,然后打开csv备份以删除这些行并重新编写.csv文件。唯一的问题是我无法重新打开文件,因为我得到了拒绝权限的错误。OpenTextFile权限被拒绝

Set objArgs = CreateObject("Scripting.FileSystemObject") 
objStartFolder = "C:\Users\bmckie\Documents\SugarSync Shared Folders\Janis Jarvis\CRM Import Spreadsheet" 
Set objFolder = objArgs.GetFolder(objStartFolder) 

Set colFiles = objFolder.Files 

For Each objFile in colFiles 
If UCase(objArgs.GetExtensionName(objFile.name)) = "XLSX" Then 

Set objExcel = CreateObject("Excel.application") 
objExcel.application.visible=false 
objExcel.application.displayalerts=false 
set objExcelBook = objExcel.Workbooks.Open(objStartFolder & "\" & objFile.Name) 

newfile = objStartFolder & "\" & objArgs.GetBaseName(objFile.Name) & ".csv" 
objExcelBook.SaveAs newfile, 23 

Set objFile = objArgs.OpenTextFile(newfile, 1) 

Do Until objFile.AtEndOfStream 
    strLine = objFile.Readline 
    secondLine = strLine 
    strLine = Replace(strLine, ",", "") 
    strLine = Replace(strLine, " ", "") 
    If Len(strLine) > 0 Then 
     strNewContents = strNewContents & secondLine & vbCrLf 

    End If 
Loop 
MsgBox(strNewContents) 

objFile.Close 

Set newobjFile = objArgs.OpenTextFile(newfile, 2) 
newobjFile.Write strNewContents 
newobjFile.Close 

任何将.xlsx转换为.csv或具有权限问题的解决方案都将不胜感激。

回答

1

您无法打开要写入的文件,因为excel仍然打开文件。关闭工作簿。