Есть ли способ принять ввод (адрес электронной почты) от пользователя и автоматически отправить тот же лист Excel, используя код vba? - PullRequest
0 голосов
/ 28 мая 2020

В этом листе

Private Sub CommandButton1_Click()


     On Error GoTo ErrHandler
        ' SET Outlook APPLICATION OBJECT.
        Dim objOutlook As Object
        Set objOutlook = CreateObject("Outlook.Application")



    ' CREATE EMAIL OBJECT.
    Dim objEmail As Object
    Set objEmail = objOutlook.CreateItem(olMailItem)

        With objEmail
            .to = "webadmin@encodedna.com"
            .Subject = "This is a test message from Arun Banik"
            .Body = "Hi there"
            .Display           ' DISPLAY MESSAGE.
        End With

        ' CLEAR.
        Set objEmail = Nothing:    Set objOutlook = Nothing

ErrorHandler:
        '
    End Sub

В модуле, так есть ли способ, где я могу принимать данные от пользователя, и лист Excel отправляется на соответствующий идентификатор электронной почты

With objEmail


    .To = "arunbanik21@rediffmail.com"
        .CC = "arun@mail.com"
        .BCC = "arun@hotmail.com"
        .Subject = "This is a test message from Arun"
        .Body = "Hi there"
        .Attachments.Add ("e:\report.doc")
        .Send


  End With

Ответы [ 2 ]

0 голосов
/ 28 мая 2020
Sub Mail_workbook_Outlook_1()

Dim UserInputToEmail As String

    Dim OutApp As Object
    Dim OutMail As Object

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

 UserInputToEmail = Application.InputBox("Enter your email id")

    On Error Resume Next
    With OutMail
        .to = UserInputToEmail
        .CC = ""
        .BCC = ""
        .Subject = "This is the Subject line"
        .Body = "Hi there"
        .Attachments.Add ActiveWorkbook.FullName
        .Send
 Application.Wait (Now + TimeValue("0:00:15"))
    End With
    On Error GoTo 0

    Set OutMail = Nothing
    Set OutApp = Nothing
End Sub
0 голосов
/ 28 мая 2020

Это может go в любом модуле, в котором написан ваш код.

Просто убедитесь, что код inputbox находится перед тем, как назначать свои значения полям электронной почты.

Также msgbox просто показывает вам, что делает код.

Sub test()
Dim UserInput As String

UserInput = Application.InputBox("Your input goes here: ")

MsgBox UserInput

End Sub

Итак, в вашем приложении это может быть что-то вроде:

Dim UserInputToEmail As String

UserInputToEmail = Application.InputBox("Please enter a To address: ")

With oOutlook
    .To = UserInputToEmail 'This will be whatever is put into the InputBox above.
    .CC = "SomeEmail@test.com"
    .Body = "Test Email"
    .Send
End With

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...