Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
325 views
in Technique[技术] by (71.8m points)

excel - How to add VBA Signature on Email with HTMLBody?

I got the following from How to add default signature in Outlook. However, I keep getting an error "Application-defined or object-defined error" where the signature variable is set. How can I fix this error?

Dim OApp As Object, OMail As Object, signature As String
Set OApp = CreateObject("Outlook.Application")
Set OMail = OApp.CreateItem(0)
With OMail
    .Display
End With
signature = OMail.body
With OMail
    '.To = "[email protected]"
    '.Subject = "Type your email subject here"
    '.Attachments.Add
    .body = "Add body text here" & vbNewLine & signature
    '.Send
End With
Set OMail = Nothing
Set OApp = Nothing

I know I could get the signature from the C:Users...Signatures folder. However at work, we all use virtual desktops and the permissions there are a tad janky.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your signature has to be declared as a variant.

Dim OApp As Object
Dim OMail As Object
Dim Signature As Variant

Set OApp = CreateObject("Outlook.Application")
Set OMail = OutApp.CreateItem(0)

On Error Resume Next
With OMail
    'Capture signature block.
    .Display
    Signature = .HTMLBody
    '.To = Recipients
    '.CC = CarbonCopy
    .Subject = "Subject"
    .HTMLBody = "<p>Add Body Here.</p>" & Signature
    .Display
End With
On Error GoTo 0

Set OutMail = Nothing
Set OutApp = Nothing

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...