简体   繁体   English

通过vba发送电子邮件,但未发送具有功能的模块

[英]Sending emails through vba but the modules with functions were not sent

I have some vba code that sends an excel sheet directly through outlook email. 我有一些VBA代码可以直接通过Outlook电子邮件发送Excel工作表。 I saved the excel file as a .xlsm, so the macros and VBA programs would work on my device. 我将excel文件另存为.xlsm,因此宏和VBA程序可以在我的设备上运行。 However, when the recipient of the email opens the excel sheet, the modules (which contains certain user defined formulas) are not sent. 但是,当电子邮件的收件人打开excel工作表时,不会发送模块(包含某些用户定义的公式)。 I was wondering whether there is a way I can ensure that the modules are sent through the email or another place I can place the code for the function so it will be automatically sent through the email. 我想知道是否有办法确保通过电子邮件发送模块,或者可以在其他地方放置该函数的代码,以便将其自动通过电子邮件发送。

Thanks in advance for your help. 在此先感谢您的帮助。

I have tried looking online for any situations, but all I saw was to put the function in module and there was no explanation about how to send the module with an email created through vba. 我尝试过在线查找任何情况,但是我所看到的只是将函数放在模块中,并且没有关于如何使用通过vba创建的电子邮件发送模块的说明。

Email: 电子邮件:

Dim FileExtStr As String
Dim FileFormatNum As Long
Dim Sourcewb As Workbook
Dim Destwb As Workbook
Dim TempFilePath As String
Dim TempFileName As String
Dim OutApp As Object
Dim OutMail As Object

With Application
    .ScreenUpdating = False
    .EnableEvents = False
End With

Set Sourcewb = ActiveWorkbook

'Copy the ActiveSheet to a new workbook
ActiveSheet.Copy
Set Destwb = ActiveWorkbook

'Determine the Excel version and file extension/format
With Destwb
    If Val(Application.Version) < 12 Then
        'You use Excel 97-2003
        FileExtStr = ".xls": FileFormatNum = -4143
    Else
        'You use Excel 2007-2016
        Select Case Sourcewb.FileFormat
        Case 51: FileExtStr = ".xlsx": FileFormatNum = 51
        Case 52:
            If .HasVBProject Then
                FileExtStr = ".xlsm": FileFormatNum = 52
            Else
                FileExtStr = ".xlsx": FileFormatNum = 51
            End If
        Case 56: FileExtStr = ".xls": FileFormatNum = 56
        Case Else: FileExtStr = ".xlsb": FileFormatNum = 50
        End Select
    End If
End With

'    'Change all cells in the worksheet to values if you want
'    With Destwb.Sheets(1).UsedRange
'        .Cells.Copy
'        .Cells.PasteSpecial xlPasteValues
'        .Cells(1).Select
'    End With
'    Application.CutCopyMode = False

'Save the new workbook/Mail it/Delete it
TempFilePath = Environ$("temp") & "\"
TempFileName = "Part of " & Sourcewb.name & " " & Format(Now, "dd-mmm-yy h-mm-ss")

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

With Destwb
    .SaveAs TempFilePath & TempFileName & FileExtStr, FileFormat:=FileFormatNum
    On Error Resume Next
    With OutMail
        .to = Range("B61")
        .CC = ""
        .BCC = ""
        .Subject = "CS Equipment Pricebook"
        .Body = Range("B30:B40")
        .Attachments.Add Destwb.FullName
        'You can add other files also like this
        '.Attachments.Add ("C:\test.txt")
        .Send   'or use .Display
    End With
    On Error GoTo 0
    .Close savechanges:=False
End With

'Delete the file you have send
Kill TempFilePath & TempFileName & FileExtStr

Set OutMail = Nothing
Set OutApp = Nothing

With Application
    .ScreenUpdating = True
    .EnableEvents = True
End With

Function: 功能:

Function username()
    username = Environ("Username")
End Function

Whenever the email is sent, the module with the function would not be sent. 无论何时发送电子邮件,都不会发送具有该功能的模块。

In order for automatically-running subroutines to work correctly in Microsoft Excel, they must be contained within a Visual Basic module. 为了使自动运行的子例程在Microsoft Excel中正常工作,它们必须包含在Visual Basic模块中。

You must insert a Visual Basic module into the workbook and then place the code in the new module. 您必须将Visual Basic模块插入工作簿中,然后将代码放入新模块中。

To insert a Visual Basic module: - In the Project Explorer in the Visual Basic Editor, activate the workbook that contains the code in question. 要插入Visual Basic模块:-在Visual Basic编辑器的项目浏览器中,激活包含相关代码的工作簿。 - On the Insert menu, click Module. -在“插入”菜单上,单击“模块”。 - After the new module is inserted, cut the code from its original location and paste it into the Visual Basic module. -插入新模块后,从其原始位置剪切代码并将其粘贴到Visual Basic模块中。 Then, save the workbook. 然后,保存工作簿。

See VBA code "behind" a worksheet or a workbook may not work in Excel for more information. 有关更多信息,请参见工作表或工作簿“后面”的VBA代码可能无法在Excel中工作

Finally, make sure that everything is included to your workbook, see Where is the Excel Personal Macro Workbook Located? 最后,确保所有内容都包含在您的工作簿中,请参阅Excel Personal Macro Workbook位于何处? .

PS If you need to distribute your Office-based solution to multiple machines, I'd suggest developing a VSTO based add-in. PS:如果您需要将基于Office的解决方案分发到多台计算机,建议您开发基于VSTO的加载项。 Read more about them in the Walkthrough: Create your first VSTO Add-in for Excel article. 演练中了解有关它们的更多信息:创建您的第一个VSTO Excel加载项

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

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