See Also
CopyFolder Method | DeleteFolder Method | MoveFolder Method
Applies To: FileSystemObject Object
Creates a folder.
object.CreateFolder(foldername)
Arguments
-
object
- Required. Always the name of a FileSystemObject. foldername
- Required. String expression that identifies the folder to create.
Remarks
An error occurs if the specified folder already exists.
The following code illustrates how to use the CreateFolder method to create a folder.
[JScript] var fso = new ActiveXObject("Scripting.FileSystemObject"); var a = fso.CreateFolder(
"c:\\new folder")
;
[VBScript] Function CreateFolderDemo Dim fso, f Set fso = CreateObject("Scripting.FileSystemObject") Set f =fso.CreateFolder(
"c:\New Folder")
CreateFolderDemo = f.Path End Function
1、工作簿创建和保存:
Sub CreateFolder1()
Path = "D:\导出结果"
Set FSO = CreateObject("Scripting.FileSystemObject")
If FSO.FolderExists(Path) = True Then
FSO.DeleteFolder (Path)
'FSO.GetFolder(Path).Delete
Else
FSO.CreateFolder (Path)
'MkDir Path
End If
End Sub
Sub CreateFolder()
Path = "D:\导出结果"
Set FSO = CreateObject("Scripting.FileSystemObject")
If FSO.FolderExists(Path) = False Then
FSO.CreateFolder (Path)
End If
End Sub
Sub CreateFolderWithPath(csPath As String)
Path = "D:\导出结果" & "\" & csPath
Set FSO = CreateObject("Scripting.FileSystemObject")
If FSO.FolderExists(Path) = False Then
FSO.CreateFolder (Path)
End If
End Sub
Sub CreateWorkbook1()
Path = "D:\导出结果"
Name = Format(Now(), "yyyy年MM月D日hh时mm分ss秒") & ".xls"
Workbooks.Add
'ActiveWorkbook.SaveAs "D:/Workbook.xls"
ActiveWorkbook.SaveAs Filename:=Path & "\" & Name
End Sub
Sub CreateWorkbook()
CreateFolder
Name = Format(Now(), "yyyy年MM月D日hh时mm分ss秒")
Workbooks.Add
CreateFolderWithPath (Name)
Path = "D:\导出结果" & "\" & Name
ActiveWorkbook.SaveAs Filename:=Path & "\" & Name & ".xls"
End Sub
'打开指定工作簿
Sub 打开()
Workbooks.Open Filename:="D:\Workbook.xls"
End Sub
'打开密码保护的文件
Sub 打开密码保护文件()
Workbooks.Open Filename:="D:\Workbook.xls", password:="123"
End Sub
'判断文件是否打开
Sub 判断文件打开状态()
Dim x As Integer
For x = 1 To Workbooks.Count
If Workbooks(x).Name = "Workbook.xls" Then
MsgBox "文件已打开"
Exit Sub
End If
Next x
MsgBox "文件未打开"
End Sub
3、工作簿关闭:
Sub 关闭所有工作簿()
Workbooks.Close
End Sub
Sub 关闭指定工作簿()
Workbooks("打开工作簿.xls").Close
End Sub
Sub 关闭指定工作簿并保存()
Workbooks("Workbook").Close savechanges:=True
End Sub