简体   繁体   English

如何在Excel VBA中以电子邮件中的链接的形式发送ActiveWorkbook.Path?

[英]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. 我正在尝试使用ActiveWorkbook.Path和ActiveWorkbook.Name通过电子邮件将excel工作簿的文件路径发送给某人。 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部分中使用的:

.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. 完整路径应为D:\\ Control Verification \\ WorkbookName.xlsm,但是当我尝试单击链接时,它试图访问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 . 您可以使用ThisWorkbook.FullName而不是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. 另外,如果要发送的工作簿是执行代码的工作簿,则最好使用ThisWorkbook而不是ActiveWorkbook ,因为ActiveWorkbook可以被许多事件更改,并且ThisWorkbook总是返回执行代码的工作簿。

"<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. 在href周围添加引号作为路径:D:\\ Control Verification \\ WorkbookName.xlsm在其中包含一个空格。

I guess the problem is the ActiveWorkbook is not saved... 我想问题是ActiveWorkbook没有保存...

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 . 通常,请尝试谨慎处理VBA ActiveWorkbookActiveCellActiveSheet “活动”内容-有时它们可​​以是其他内容,而不是已久的内容- 如何避免在Excel VBA中使用Select

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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