简体   繁体   中英

How to send ActiveWorkbook.Path as link in email in excel VBA?

I'm trying to use ActiveWorkbook.Path and ActiveWorkbook.Name to send the excel workbook's file path to someone through email. I got it to work when I input the file path as a string, but for some reason it doesn't recognize the full path when I do it this way. I don't want to use a hard-coded file address in case someone moves it to a different folder. This is what I'm using in the .HTMLBody part of the code:

.HTMLBody = emailtext & "<p>&nbsp;</p>" & "<p><strong>Click this link to go to task verification worksheet: <A href=" & _
        "" & ActiveWorkbook.Path & "\" & ActiveWorkbook.Name & "" & ">Verification Workbook</A></strong></p>"

The full path should be D:\\Control Verification\\WorkbookName.xlsm but when I try to click the link it's trying to access D:\\Control. Thanks in advance for any help!

You can simplify your code by using ThisWorkbook.FullName which returns the entire path instead of ActiveWorkbook.Path & "\\" & ActiveWorkbook.Name .

Also, if the workbook you want to send is the one executing the code, it's best to use ThisWorkbook instead of ActiveWorkbook , since ActiveWorkbook can be changed by many events and ThisWorkbook always returns the workbook executing the code.

"<p>&nbsp;</p>" & "<p><strong>Click this link to go to task verification worksheet: <A href=" & _
    """" & ActiveWorkbook.Path & "\" & ActiveWorkbook.Name & """" & ">Verification Workbook</A></strong></p>"

Adds quotations around the href, as the path: D:\\Control Verification\\WorkbookName.xlsm contains a space in it.

I guess the problem is the ActiveWorkbook is not saved...

Save it somewhere and try this:

Public Sub TestMe()

    ActiveWorkbook.Save

    MsgBox "<p>&nbsp;</p>" & "<p><strong>Click this link: <A href=" & _
           "" & ActiveWorkbook.Path & "\" & ActiveWorkbook.Name & "" & _
           ">Verification Workbook</A></strong></p>"

End Sub

In general, try to be a bit careful around "Active" things in VBA ActiveWorkbook , ActiveCell , ActiveSheet - sometimes they can be something else and not the awaited ones - How to avoid using Select in Excel VBA .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM