我有一个带有信息和按钮的UserForm。当我点击"Demander Attest.“(cbAskAttestation)时,你可以在这里看到:

这将为我准备一封电子邮件:

但是,如何将电子邮件矩阵中的数据替换为用户表单文本框中的数据呢?我想用用户表单中的文本替换电子邮件表右列中的文本。例如,将电子邮件中的“等级”替换为SAP。
附件
UserForm ufReservistInformations中的数据来自以下代码:
Sheets("RECAP").Cells(Lig, 2) = cboFunction
Sheets("RECAP").Cells(Lig, 5) = cboSexReservist
Sheets("RECAP").Cells(Lig, 6) = cboRankReservist
Sheets("RECAP").Cells(Lig, 7) = txtIncorporationNumberReservist
Sheets("RECAP").Cells(Lig, 8) = txtBsnReservist
Sheets("RECAP").Cells(Lig, 9) = txtBirthdateReservist
Sheets("RECAP").Cells(Lig, 10) = txtAgeReservist
Sheets("RECAP").Cells(Lig, 11) = txtBirthplaceReservist
Sheets("RECAP").Cells(Lig, 12) = txtAddressReservist
Sheets("RECAP").Cells(Lig, 13) = txtZipcodeReservist
Sheets("RECAP").Cells(Lig, 15) = txtPhoneReservist
Sheets("RECAP").Cells(Lig, 17) = txtEmailReservist
Sheets("RECAP").Cells(Lig, 18) = txtContactReservist
Sheets("RECAP").Cells(Lig, 19) = txtJobReservist
Sheets("RECAP").Cells(Lig, 20) = txtEsrReservist
Sheets("RECAP").Cells(Lig, 21) = cboLengthContractReservist
Sheets("RECAP").Cells(Lig, 22) = txtEndEsrReservist
Sheets("RECAP").Cells(Lig, 23) = cboSav1Reservist
Sheets("RECAP").Cells(Lig, 24) = txtSav1CommentReservist
Sheets("RECAP").Cells(Lig, 25) = txtRetrainingReservist
Sheets("RECAP").Cells(Lig, 26) = txtFmaChiefReservist
Sheets("RECAP").Cells(Lig, 27) = txtVsaReservist
Sheets("RECAP").Cells(Lig, 28) = txtNextVsaReservist我创建了一个如下模板的电子邮件:
Sub CreateEmailfromTemplate(ByVal email As String, ByVal pathToTemplate As String)
Dim obApp As Object
Dim NewMail As Outlook.MailItem
Set obApp = Outlook.Application
'Change the template file folder path according to your case
Set NewMail = obApp.CreateItemFromTemplate(pathToTemplate)
With NewMail
.To = email
End With
NewMail.Display
Set obApp = Nothing
Set NewMail = Nothing
End Sub尝试使用RobertBaron的答案,如下所述
我尝试使用Replace函数。
Dim obApp As Object
Dim NewMail As Outlook.MailItem
Set obApp = Outlook.Application
'Change the template file folder path according to your case
Set NewMail = obApp.CreateItemFromTemplate("\\bspp.fr\Travail\CCL1\MTMA\Groupe Adjudant de Compagnie\RESERVISTES\CORRESPONDANCE\Demande d'attestation de recyclage.msg")
With NewMail
mailBody = .Body
End With
mailBody = Replace(mailBody, "1cl", cboRankReservist)
With NewMail
.Body = mailBody
End With
NewMail.Display
Set obApp = Nothing
Set NewMail = Nothing然而,结果并没有保留数组,而是呈现了一列单词。实际上,以下是使用mailBody = Replace(mailBody, "1cl", cboRankReservist)后的结果
Bonjour,
J’ai l’honneur de vous demander une attestation de formation continue équipier-secouriste pour le personnel suivant :
Groupement
1 GIS
Compagnie
20
N° incorporation
91109
Grade
1cl
...

发布于 2019-06-27 18:30:20
发布于 2019-06-29 03:26:13
在Outlook中使用正文有三种主要方式:
您可以在Chapter 17: Working with Item Bodies文章中阅读更多关于这方面的内容。
选择哪种方式由你决定。但我认为Word在这种情况下可能会有所帮助。若要在Outlook VBA代码中使用这些技术,请使用“工具”|“引用”命令添加对Microsoft Word对象库的引用。
https://stackoverflow.com/questions/56788536
复制相似问题