为什么 Dir 在我的脚本中没有被识别为方法?
Why is Dir not recognized as a method in my script?
我正在开发一个 VBS 脚本来通过 outlook 自动发送电子邮件。每次脚本运行时,文件夹中都会有 2 个文件,我需要它附加名称中具有“格式”的文件。我尝试使用 Dir 方法,这样我就可以通配文件路径,但它会抛出一个错误,说明 Dir是一个未定义的变量。
这是我的代码:
Option Explicit
Const olMailItem = 0
Sub SendBasicEmail()
Dim olApp: Set olApp = CreateObject("Outlook.Application")
Dim olEmail: Set olEmail = olApp.CreateItem(olMailItem)
Dim strLocation
strLocation = Dir("C:\Users\MChambers\Desktop\Pricing Reports\Pricing_Report_*Formatted.xslx")
If strLocation <> "" Then
With olEmail
.SentOnBehalfOfName = "generic"
.Attachments.Add (strLocation)
.To = "myemail"
.Subject = "Subject"
.send
End With
End If
End Sub
不幸的是,与 VBA 和 VB6 不同,VBScript 没有 Dir
函数。您应该能够创建自己的辅助函数,该函数使用 Regular Expressions 代替。
举个例子:
Option Explicit
Function FindFirstFile(strDirPath, strPattern)
Dim strResult
Dim objRegExp, objMatches
Set objRegExp = New RegExp
objRegExp.Pattern = strPattern
objRegExp.IgnoreCase = True
Dim objFso, objFolder, objFile
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(strDirPath)
For Each objFile in objFolder.Files
Set objMatches = objRegExp.Execute(objFile.Name)
If objMatches.Count > 0 Then
strResult = objMatches(0).Value
Exit For
End If
Next
FindFirstFile = strResult
End Function
用法:
Dim strPattern
strPattern = "Pricing_Report_.*Formatted\.xslx"
strLocation = FindFirstFile("C:\Users\MChambers\Desktop\Pricing Reports\", strPattern)
If strLocation <> "" Then
' Do something here.
End If
注:
就像 Dir
函数一样,这将 仅 return 文件名。但是,您似乎正在尝试获取完整路径。如果是这样,您可以替换以下行:
FindFirstFile = strResult
..类似:
If Len(strResult) > 0 Then
If Right(strDirPath, 1) <> "\" Then strDirPath = strDirPath & "\"
strResult = strDirPath & strResult
End If
FindFirstFile = strResult
我正在开发一个 VBS 脚本来通过 outlook 自动发送电子邮件。每次脚本运行时,文件夹中都会有 2 个文件,我需要它附加名称中具有“格式”的文件。我尝试使用 Dir 方法,这样我就可以通配文件路径,但它会抛出一个错误,说明 Dir是一个未定义的变量。
这是我的代码:
Option Explicit
Const olMailItem = 0
Sub SendBasicEmail()
Dim olApp: Set olApp = CreateObject("Outlook.Application")
Dim olEmail: Set olEmail = olApp.CreateItem(olMailItem)
Dim strLocation
strLocation = Dir("C:\Users\MChambers\Desktop\Pricing Reports\Pricing_Report_*Formatted.xslx")
If strLocation <> "" Then
With olEmail
.SentOnBehalfOfName = "generic"
.Attachments.Add (strLocation)
.To = "myemail"
.Subject = "Subject"
.send
End With
End If
End Sub
不幸的是,与 VBA 和 VB6 不同,VBScript 没有 Dir
函数。您应该能够创建自己的辅助函数,该函数使用 Regular Expressions 代替。
举个例子:
Option Explicit
Function FindFirstFile(strDirPath, strPattern)
Dim strResult
Dim objRegExp, objMatches
Set objRegExp = New RegExp
objRegExp.Pattern = strPattern
objRegExp.IgnoreCase = True
Dim objFso, objFolder, objFile
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(strDirPath)
For Each objFile in objFolder.Files
Set objMatches = objRegExp.Execute(objFile.Name)
If objMatches.Count > 0 Then
strResult = objMatches(0).Value
Exit For
End If
Next
FindFirstFile = strResult
End Function
用法:
Dim strPattern
strPattern = "Pricing_Report_.*Formatted\.xslx"
strLocation = FindFirstFile("C:\Users\MChambers\Desktop\Pricing Reports\", strPattern)
If strLocation <> "" Then
' Do something here.
End If
注:
就像 Dir
函数一样,这将 仅 return 文件名。但是,您似乎正在尝试获取完整路径。如果是这样,您可以替换以下行:
FindFirstFile = strResult
..类似:
If Len(strResult) > 0 Then
If Right(strDirPath, 1) <> "\" Then strDirPath = strDirPath & "\"
strResult = strDirPath & strResult
End If
FindFirstFile = strResult