0

我正在使用 vbscript 和 excel 来自动化我的一项任务。下面提到的代码会自动在 Outlook 中起草一封带有附件的电子邮件。但我仍然必须手动输入“aa”值和代码“1”值。这些值是在 excel 中定义,我想从那里获取它。我可以从中获取“名称”值objFso.GetFileName(objFile.path)。但我不确定如何在 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    

请推荐!!!

4

1 回答 1

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
于 2013-06-03T07:49:56.820 回答