1

Scenario:

From an Access-form with selected recordset, a report is filled with data, this is converted via VBA into HTML format and sent with Outlook. The content then appears as HTML in the email-body of the recipient. This contains a link that points to the recordset in this form. So assuming the recipient also has ACCESS and also the exact same database (and same location & content), the link would open his database file when clicked, then open the form, then open that exact record by using the query.

Based on this...

Hyperlink-Parts

...I added a text field into my report with this content and marked it as a hyperlink:

Click here#C:\MainDatabase.accdb#Query frmForm01

Click the hyperlink in the report works (opens Access & file), the link in the locally stored HTML-tempfile also works.

Problem:

Although the link is sent, also appears in the recipient email-body as link "Click here", but displayed link is not clickable, just blue underlined text!

After a lot of experimenting I found out that it's all Outlook! The link is intact in the Outlook-mail-body preview before sending....it must be an Outlook-setting that converts the link into text during/for sending. In the Outlook menu I have already checked the format settings: convert to HTML is OK!(not plain text). So what else could be the cause??

Would be very grateful for solutions. Thanks!

My code:

Sub CreateHTMLMail()

Dim OutApp As Outlook.Application
Dim OutMail As Outlook.MailItem
Dim FSO As Object
Dim AttchFile As String, EmailTo As String, strSig As String

Set OutApp = New Outlook.Application
Set OutMail = OutApp.CreateItem(olMailItem)

    AttchFile = "C:\AttachmTemp.htm"
    EmailTo = "abcd@efgh.com"

    DoCmd.OpenReport "NewReport", acViewReport, , "[ID]='" & Me.ID & "'", acHidden
    DoCmd.OutputTo acOutputReport, "NewReport", acFormatHTML, AttchFile
    
Set FSO = CreateObject("Scripting.FileSystemObject")

strSig = FSO.OpenTextFile(AttchFile).ReadAll
        
    With OutMail
    .TO = EmailTo
    .Subject = "New Message"
    .BodyFormat = olFormatHTML
    .HTMLBody = strSig
    .send
    End With

End If

Kill AttchFile

Set OutApp = Nothing
Set OutMail = Nothing

End Sub
Jasco
  • 225
  • 3
  • 8
  • The https: URL works, because that is a protocol that Windows (and therefore Outlook) understands. The file: URL could work also, if you get it right (try 2-6 "/" after file: ) - but it won't help you beyond simply opening a file. Hence the suggestion of a custom URL protocol where you can pass parameters. – Andre Jun 16 '21 at 12:59
  • I'm not saying you are wrong...but hope dies last (..not having to mess with registry :-)) – Jasco Jun 16 '21 at 13:16

1 Answers1

2

I do this using a custom URL protocol handler.

First, create (and distribute after testing) a .reg file like this (replace myapp by your application name):

Windows Registry Editor Version 5.00

; Andre, 2019

[HKEY_CLASSES_ROOT\myapp]
@="URL:myapp-Open"
"URL Protocol"=""
"EditFlags"=hex:02,00,00,00

[HKEY_CLASSES_ROOT\myapp\DefaultIcon]
@="C:\\path\\myapp\\myapp.ico"

[HKEY_CLASSES_ROOT\myapp\shell]
@="open"

[HKEY_CLASSES_ROOT\myapp\shell\open]

[HKEY_CLASSES_ROOT\myapp\shell\open\command]
; using a .vbs to start my app
; @="wscript C:\\path\\myapp\\Startmyapp.vbs \"%1\""
; (untested) starting Access directly, needs the /cmd switch
@="\"C:\\Program Files (x86)\\Microsoft Office\\Office16\\msaccess.exe\" C:\\path\\myapp\\Myapp.accde /cmd \"%1\""

; If you use Outlook, prevent the Security warning
; https://stackoverflow.com/a/34660198/3820271
; Note: the whole subtree starting with (at least) "Security" will be created.
; This path is for Office 2016.
[HKEY_CURRENT_USER\Software\Policies\Microsoft\Office\16.0\Common\Security\Trusted Protocols\All Applications\myapp:]

Note that I use a .vbs script to start my Access application, it should work as well with the path to msaccess.exe plus your local frontend.
See comments in myapp\shell\open\command

Then in your HTML email, use URLs like this:

url:myapp:ID:2357     

ID:2357 will be passed as command-line parameter to your application.

See Opening Microsoft Access with parameters on how to read this string, then parse it in your Autoexec function and open your form.

To test this, simply type myapp:ID:2357 in Start/Run or in a Cmd window.

Andre
  • 26,751
  • 7
  • 36
  • 80
  • Hey Andre, thank you for your effort, but I am extremely reserved about registry changes, so will better wait a while for other suggestions, if not then I will rather go on without the link idea. – Jasco Jun 16 '21 at 06:26
  • 1
    It doesn't bite. :) There are already a gazillion entries in HKEY_CLASSES_ROOT, an additional one won't hurt. – Andre Jun 16 '21 at 07:15
  • Once Windows starts bugging, then bites are my smallest problem. But thanks for the code, i'll keep it as plan b in mind should all ropes break... – Jasco Jun 16 '21 at 11:04
  • Hi Andre ...question: what is the exact syntax here: **@="URL:myapp-Open"** the full path of the .exe file ("C:\Program Files (x86)\Microsoft Office\Office14\MSACCESS.EXE")? And if so, do I need to enter this complete path over everywhere myapp is? Thanks – Jasco Jun 26 '21 at 11:01
  • 1
    No, this is just a string, a name that you can freely choose. I think it's called the "verb", or the action name. If we were to define a new context menu item for Explorer, it would show up there. @Jasco – Andre Jun 26 '21 at 18:44
  • But you wrote: **(replace myapp by your application name)**?? – Jasco Jun 26 '21 at 18:50
  • 1
    `myapp` is a name you choose, e.g. `JascoApp`. The `@=` name (default value) is also defined by you, but it makes sense to make it recognizable. You can open regedit and look at (or export) other protocol keys, e.g. `HKEY_CLASSES_ROOT\ftp`. They have a similar structure. @Jasco – Andre Jun 27 '21 at 07:44