创建目录和新建文本文件

这篇博客介绍了如何在VBA中创建多级目录,包括使用`MakeSureDirectoryPathExists`函数创建指定路径的目录,并提供了创建和操作文本文件的示例,如追加写入、读取文件以及检查文件是否存在。
摘要由CSDN通过智能技术生成

 1、以下代码可以建立多级文件夹:
Option Explicit
Private Declare Function MakeSureDirectoryPathExists Lib "imagehlp.dll" (ByVal lpPath As String) As Long
'创建指定的目录(可以是多级目录)

Private Function CreateDirectory(ByVal sDirectory As String) As Boolean
'创建指定的目录(可以是多级目录),sDirectory:要创建的文件夹
On Error GoTo ErrHandle
Dim lngResult As Long
sDirectory = checkpath(sDirectory)
lngResult = MakeSureDirectoryPathExists(sDirectory)
CreateDirectory = IIf(lngResult = 0, False, True)
ErrHandle:
If Err <> 0 Then
CreateDirectory = False
End If
End Function

Private Function checkpath(ByVal sPath As String) As String
If Right$(sPath, 1) = "/" Then
checkpath = sPath
Else
checkpath = sPath & "/"
End If
End Function

Private Sub Command1_Click()
CreateDirectory "c:/abc/def/ghi"

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值