VBScript打开将csv转换为xls的文件夹

时间:2013-08-12 12:06:42

标签: excel csv vbscript

任何人都可以解释为什么这不起作用,它会抛出错误说:

线:14 Char:1 错误:对象必需:'[string:“H:\ VBS”]' 代码:800A01A8

Const xlDelimited = 1
Const xlNormal = -4143 

Set Excel = CreateObject("Excel.Application")
Set fso = CreateObject("Scripting.FileSystemObject")

Dim Excel
dim objShell
dim objFolder
Dim fileLocation

set objShell = CreateObject("shell.application")
set objFolder = objshell.BrowseForFolder(0, "Select File Location", 1, "H:\")
set fileLocation = CStr(objFolder.self.path)

Set Excel = CreateObject("Excel.Application")
Set fso = CreateObject("Scripting.FileSystemObject")

folderDestination = InputBox("Enter Move to folder")
For Each f In fso.GetFolder(fileLocation).Files
  If LCase(fso.GetExtensionName(f)) = "csv" Then
        .Workbooks.Open
    .Sheets(1).Columns("A").TextToColumns .Range("A1"), xlDelimited,,,,,,,True,"|"
    .ActiveWorkbook.SaveAs .ActiveWorkbook.Path & "\1.xls", xlNormal
    .Quit
  End If
Next

至少可以说是非常困惑:S再次感谢任何帮助!

2 个答案:

答案 0 :(得分:2)

“CStr(objFolder.self.path)”求值为字符串,因此不应在分配中使用Set(仅用于对象)。

答案 1 :(得分:0)

试试这个:

Const xlDelimited = 1

Const xlNormal = -4143 


Set Excel = CreateObject("Excel.Application")

Set fso = CreateObject("Scripting.FileSystemObject")


Dim Excel

dim objShell

dim objFolder

Dim fileLocation


set objShell = CreateObject("shell.application")

set objFolder = objshell.BrowseForFolder(0, "Select File Location", 1, "H:\")

set fileLocation = fso.GetFolder(objFolder.self.path)

Set Excel = CreateObject("Excel.Application")

Set fso = CreateObject("Scripting.FileSystemObject")

folderDestination = InputBox("Enter Move to folder")

For Each f In fso.GetFolder(fileLocation).Files

  If LCase(fso.GetExtensionName(f)) = "csv" Then

        .Workbooks.Open

    .Sheets(1).Columns("A").TextToColumns .Range("A1"), xlDelimited,,,,,,,True,"|"

    .ActiveWorkbook.SaveAs .ActiveWorkbook.Path & "\1.xls", xlNormal

    .Quit

  End If

Next
相关问题