VBA工作簿操控备忘

CreateFolder Method

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


2、工作簿打开:

'打开指定工作簿
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


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值