我正在使用vbscript和excels来自动完成我的任务之一。下面提到的代码会在outlook中自动起草一个带有附件的电子邮件。但是我仍然必须手动输入'aa'值和代码'1'值。这些值在excel中定义,我想从那里获取它。我可以从objFso.GetFileName(objFile.path)
获取'name'值。但我不知道如何在html代码中传递这些值。
For Each objFolder In objFolder.SubFolders
For Each objFile In objFolder.Files
If objFso.GetExtensionName(objFile.Path) = "xls" Then
Set MyApp = CreateObject("Outlook.Application")
Set MyItem = MyApp.CreateItem(0) 'olMailItem
With MyItem
.To = "a@abc.com"
.Subject = ""
.ReadReceiptRequested = False
.HTMLBody = "<font size='3' face='Calibri' color='#203B78'>Hi,<BR><BR>Please approve the attached doc for **<b>aa</b>** for code **<b>1</b>.**
.Attachments.Add objFolder.Path & "\" & objFso.GetFileName(objFile.path)
End With
MyItem.Display
End if
Next
Next
请建议!!!
答案 0 :(得分:1)
您需要打开Excel文件
Set xl = CreateObject("Excel.Application")
xl.Visible = True 'set to False for production use
Set wb = xl.Workbooks.Open("C:\path\to\your.xls")
Set ws = wb.Sheets(1)
并将值插入邮件正文:
With MyItem
.HTMLBody = "... approve the attached doc for **<b>" _
& ws.Cells(1, 1).Value & "</b>** for code **<b>" _
& ws.Cells(1, 2).Value & "</b>.**"
将1, 1
和1, 2
替换为包含实际值的单元格的行号和列号。
完成后关闭工作簿并退出Excel:
wb.Close
xl.Quit
另一方面,objFile.Path
已经拥有该文件的完整路径,因此您只需使用
.Attachments.Add objFile.Path
而不是
.Attachments.Add objFolder.Path & "\" & objFso.GetFileName(objFile.path)
如果由于某种原因你必须从文件夹和文件名构建路径,那么使用BuildPath
方法应优先于通过字符串连接手动构建路径:
.Attachments.Add objFso.BuildPath(objFolder.Path, objFile.Name)
编辑:文件名可以像这样进行转换:
name = Replace(Mid(objFso.GetBaseName(objFile.Name), 26), "_", " ")
长版:
name = objFso.GetBaseName(objFile.Name) 'get file name without extension
name = Mid(name, 26) 'get part after "Timecard Adjustment_Form_"
name = Replace(name, "_", " ") 'replace underscore with space