Vba 有没有一种方法可以发送一个文件,只要它具有csv扩展名,而不考虑文件名?
我将定期向客户发送csv文件。我想知道是否有一种方法可以发送文件夹中的任何文件,只要它具有csv扩展名,因为我不会提前知道文件的名称 我相信我有工作代码,可以让我的VBA脚本与任务调度程序结合起来自动发送电子邮件。我只是停留在如何附加一个文件,我不知道的名称Vba 有没有一种方法可以发送一个文件,只要它具有csv扩展名,而不考虑文件名?,vba,outlook,Vba,Outlook,我将定期向客户发送csv文件。我想知道是否有一种方法可以发送文件夹中的任何文件,只要它具有csv扩展名,因为我不会提前知道文件的名称 我相信我有工作代码,可以让我的VBA脚本与任务调度程序结合起来自动发送电子邮件。我只是停留在如何附加一个文件,我不知道的名称 Const cSmtpUser = "username" ' *** MAKE CHANGES HERE *** Const cSmtpPassword = "password"
Const cSmtpUser = "username" ' *** MAKE CHANGES HERE ***
Const cSmtpPassword = "password" ' *** MAKE CHANGES HERE ***
Const cSmtpServer = "smtp.xxx.yyy" ' *** MAKE CHANGES HERE ***
Const cSmtpPort = 465 ' *** MAKE CHANGES HERE *** (25, 465, 587 common)
Const cFromEmail = "xxxxxxxxx@xxx.yyy" ' *** MAKE CHANGES HERE ***
Const cToEmail = "xxxxxxxxx@xxx.yyy" ' *** MAKE CHANGES HERE ***
Const cSubject = "Daily Email Subject" ' *** MAKE CHANGES HERE ***
Const cEmailBody = "See attached for todays file." ' *** MAKE CHANGES HERE ***
Const cAttachment = "c:\temp\yourfile.txt" ' *** MAKE CHANGES HERE ***
' CDO Constants needed to send email
Const cCdoSendUsingPickup = 1 'Send message using the local SMTP service pickup directory.
Const cCdoSendUsingPort = 2 'Send the message using the network (SMTP over the network).
Const cCdoAnonymous = 0 'Do not authenticate
Const cCdoBasic = 1 'basic (clear-text) authentication
Const cCdoNTLM = 2 'NTLM
Const cCdoSendUsingMethod = "http://schemas.microsoft.com/cdo/configuration/sendusing"
Const cCdoSMTPServer = "http://schemas.microsoft.com/cdo/configuration/smtpserver"
Const cCdoSMTPServerPort = "http://schemas.microsoft.com/cdo/configuration/smtpserverport"
Const cCdoSMTPConnectionTimeout = "http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout"
Const cCdoSMTPAuthenticate = "http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"
Const cCdoSendUserName = "http://schemas.microsoft.com/cdo/configuration/sendusername"
Const cCdoSendPassword = "http://schemas.microsoft.com/cdo/configuration/sendpassword"
Const cCdoSmtpUseSsl = "http://schemas.microsoft.com/cdo/configuration/smtpusessl"
' Create filesystem object
Set objFSO = CreateObject("Scripting.FileSystemObject")
' Get a handle to the config object and it's fields
Set objConfig = CreateObject("CDO.Configuration")
' Set config fields we care about
With objConfig.Fields
.Item(cCdoSendUsingMethod) = cCdoSendUsingPort
.Item(cCdoSMTPServer) = cSmtpServer
.Item(cCdoSMTPServerPort) = cSmtpPort
.Item(cCdoSMTPConnectionTimeout) = 60
.Item(cCdoSMTPAuthenticate) = cCdoBasic
.Item(cCdoSendUserName) = cSmtpUser
.Item(cCdoSendPassword) = cSmtpPassword
.Item(cCdoSmtpUseSsl) = True
.Update
End With
' Create a new message
Set objMessage = CreateObject("CDO.Message")
Set objMessage.Configuration = objConfig
' Populate message fields and send it
With objMessage
.To = cToEmail
.From = cFromEmail
.Subject = cSubject
.Textbody = cEmailBody
If objFSO.FileExists(cAttachment) Then
.AddAttachment cAttachment
End If
.Send
End With
希望我能够发送文件夹中的任何文件,只要它具有csv扩展名。如果这是不可能的,但可以发送任何文件,无论其扩展名如何,我可以使其仅csv文件在文件夹中
谢谢您的帮助。您可以使用此代码来发现具有
扩展名的所有文件。
存在于特定文件夹中:
Dim oFile As Object
Dim oFSO As Object
Dim oFolder As Object
Dim oFiles As Object
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFolder = oFSO.GetFolder("C:\my\Folder\") 'Set this accordingly
Set oFiles = oFolder.Files
'For all files in the folder
For Each oFile In oFiles
If (oFile Like "*.csv") Then
'Add this file to attachments
objMessage.AddAttachment oFile.Path
End If
Next
希望这能有所帮助。欢迎,您有没有研究过使用带有通配符的
Dir()函数?e、 g.*.csv
(假设每次您的文件都在同一个文件夹中)我没有,我对VBA非常陌生,我不确定如何使用它。我想我找到了答案,谢谢您的帮助Dim cAttachment作为字符串cAttachment=Dir(“C:\Users\\ uuuu \ Desktop\Test Folder*csv*”)
这不起作用。。思想?Dim CATTACHENT和CATTACHENT=位于不同的行中。它应该是CATTACHENT=Dir(“C:\Users\uuuuuu\ Desktop\Test Folder\*.csv”)。输入的内容是将\*
更改为*
,这样您的语句就不会像看上去那样错误。但是,尾随*是错误的。