VBS Email adding attachement failed (trying to share file in HTML emails)

I've been struggling on one-click sharing file with email attachment to colleagues -- by default it'll always lead to an Email in plain text without signature, and always with the prefix "ready to share....".

Below link mentioned something, but not fulfill my target.

Copy SENDMAIL to Outlook as HTML instead of Plain Text?

Courtesy of [Joseph's Script Method] I searched, i found that there is a VBS way out, and i am trying to accomplish that into a DOpus button (instead of right-click -> send to)

BTW: a very similar approach discovered here, but no further progress. and I've achieved much more...:slight_smile:

Email selected files with Outlook. Code provide, need help with creating a button

What i did -- Approach A:
i created a button with very simple command:

C:\Apps\SendMail.vbs "%1"

and of course I put the script at the correct place. and it works!
Sadly, it only works with single files, and if I've selected multiple, a lot of mails will jump out.

What i did -- Approach B: VBS in DO CommandButton
I created a button with VBS as below:

Option Explicit
Function OnClick(ByRef clickData)
	Dim cmd, lister, tab, selItem, folderEnum, folderItem
	' ---------------------------------------------------------
	Set cmd = clickData.func.command
	cmd.deselect = false ' Prevent automatic deselection

Dim OutApp, oNameSpace, oInbox, oEmailItem, olMailItem
Dim a, oAttachments, subjectStr, olFormatHTML
Dim objFSO,strSigFilePath, objSignatureFile, strBuffer, strText
olMailItem = 0
olFormatHTML = 2
Set OutApp = CreateObject("Outlook.Application") 'opens Outlook
Set oEmailItem = OutApp.CreateItem(olMailItem) 'opens new email
Const LineBreak = "<br>"

	DOpus.Output "Selected items in " & clickData.func.sourcetab.path & ":"
	If clickData.func.sourcetab.selected.count = 0 Then
		DOpus.Output "  (none)"
		Exit function
		For Each selItem in clickData.func.sourcetab.selected
			If (selItem.is_dir) Then
				DOpus.Output "  (d) " & selItem.RealPath
				DOpus.Output "  (f) " & selItem.RealPath
		Set oAttachments = oEmailItem.Attachments.Add(selItem.RealPath)
				subjectStr = subjectStr & Right(selItem.RealPath,Len(selItem.RealPath)-(InStrRev(selItem.RealPath,"\"))) & ", " 
				strText=strText & LineBreak &  Right(selItem.RealPath,Len(selItem.RealPath)-(InStrRev(selItem.RealPath,"\")))
			End If
	End If

DOpus.Output clickData.func.sourcetab.selected(0).RealPath
Set objFSO = CreateObject("Scripting.FileSystemObject")
strSigFilePath = "C:\Users\huangb\AppData\Roaming\Microsoft\Signatures\"
Set objSignatureFile = objFSO.OpenTextFile(strSigFilePath & "Short_en.htm")
strBuffer = objSignatureFile.ReadAll

oEmailItem.Subject = "FileSharing: " & Left(subjectStr, len(subjectStr)-2) 
oEmailItem.BodyFormat = olFormatHTML
oEmailItem.HTMLBody = "Please check attachment: "  & strText &  ". " & LineBreak & LineBreak &  strBuffer  


End Function

it also works, mostly...
When the code runs into the add attachment line, it failed.

	Set oAttachments = oEmailItem.Attachments.Add(selItem.RealPath)

the compiler simply said "the operation failed"
but if I skip this line, everything works perfectly.
However, I am quite convinced that the syntax was correct (in VB & windows world).

Could anybody help me here, please?

I got the original VBS script here:

Joseph's Script Method

Sorry that I am a new forum user and i could only post 2 links inside.

This line?:

Set oAttachments = oEmailItem.Attachments.Add(selItem.RealPath)

What is the error message you get from that line? (Assuming that's what's happening.)

Dear Leo,
Sorrry I forget to hightlight that line -- Yes! You are correct.

I've re-edited the post.
please check.

Try converting the path to a string when you add it:

Set oAttachments = oEmailItem.Attachments.Add(CStr(selItem.RealPath))

(may not help, just a guess)

It worked!!!
you are my hero!

BTW: What's the type of RealPath property then? (if it's not string type)

It's an object with properties and methods of its own (https://www.gpsoft.com.au/help/opus12/index.html#!Documents/Scripting/Path.htm). Its default value is a string so Outlook could query it to implicitly convert it to a string, but obviously doesn't.

OK, Great.
Thanks again!