Adding a Log from user entered comments

ChaosPup

New Member
Joined
Sep 27, 2021
Messages
48
Office Version
  1. 365
Platform
  1. Windows
Hi all, I have a short bit of code I'd like to expand upon, hoping someone can point me in the right direction. I have a sheet with several basic macro buttons. Each time one of the macro buttons is clicked it opens an InputBox for the user to enter any additional comments they wish, then sends an automated email to a predefined list of addresses, and appends the extra comments to the end of the email. As it is, it works fine. See code below (sorry about all the X’s!)

Excel Formula:
Sub XXXX()

'Set email objects
Dim OutApp As Object
Dim OutMail As Object
Dim strbody As String
Dim combody As String

'Set up InputBox for comments
combody = InputBox("Please enter any comments", "Comments")

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

'Create Mail in Outlook
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)

strbody = "Hello," & vbNewLine & vbNewLine & _
          "XXXX" & vbNewLine & vbNewLine & _
          "**********THIS EMAIL HAS BEEN AUTOMATICALLY GENERATED, PLEASE DO NOT RESPOND**********"

On Error Resume Next
With OutMail
    .To = "XXXX"
    .CC = ""
    .BCC = ""
    .Subject = "XXXX"
    .Body = strbody & vbNewLine & vbNewLine & "COMMENTS - " & combody
    'You can add a file like this
    '.Attachments.Add ("C:\test.txt")
    .Send   'or use .Display
End With
On Error GoTo 0

Set OutMail = Nothing
Set OutApp = Nothing

MsgBox ActiveCell.Value & vbNewLine & _
"NOTIFICATION SENT"

End Sub

What I’d like to add is this – after the user has entered their comments and pressed ‘OK’, the comment(s) that were entered are entered onto a ‘LOG’ worksheet (already created within the workbook), along with the date and time. The next time someone uses the workbook and presses one of the macros, it adds those comments to the log on the next row down, as so on.

Can anyone advise how I could add the 'combody' content to the LOG sheet? Thanks!!
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Did you mean something like this?

VBA Code:
Sub XXXX()

'Set email objects
Dim OutApp As Object
Dim OutMail As Object
Dim strbody As String
Dim combody As String

' Variables
Dim wsLOG As Worksheet: Set wsLOG = Worksheets("LOG")
Dim EmptyRow As Range


'Set up InputBox for comments
combody = InputBox("Please enter any comments", "Comments")

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

'Create Mail in Outlook
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)

strbody = "Hello," & vbNewLine & vbNewLine & _
          "XXXX" & vbNewLine & vbNewLine & _
          "**********THIS EMAIL HAS BEEN AUTOMATICALLY GENERATED, PLEASE DO NOT RESPOND**********"

On Error Resume Next
With OutMail
    .To = "XXXX"
    .CC = ""
    .BCC = ""
    .Subject = "XXXX"
    .Body = strbody & vbNewLine & vbNewLine & "COMMENTS - " & combody
    'You can add a file like this
    '.Attachments.Add ("C:\test.txt")
    .Send 'or use.Display
End With
On Error GoTo 0

' Write
Set EmptyRow = wsLOG.Range("A" & wsLOG.Cells(Rows.Count, "A").End(xlUp).Row + 1).Resize(1, 3)
EmptyRow.Cells(1).Value2 = combody: EmptyRow.Cells(2).Value2 = Date: EmptyRow.Cells(3).Value2 = Time 'combody, date,time

Set OutMail = Nothing
Set OutApp = Nothing

MsgBox ActiveCell.Value & vbNewLine & _
"NOTIFICATION SENT"

End Sub
 
Upvote 0
Solution
Did you mean something like this?

VBA Code:
Sub XXXX()

'Set email objects
Dim OutApp As Object
Dim OutMail As Object
Dim strbody As String
Dim combody As String

' Variables
Dim wsLOG As Worksheet: Set wsLOG = Worksheets("LOG")
Dim EmptyRow As Range


'Set up InputBox for comments
combody = InputBox("Please enter any comments", "Comments")

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

'Create Mail in Outlook
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)

strbody = "Hello," & vbNewLine & vbNewLine & _
          "XXXX" & vbNewLine & vbNewLine & _
          "**********THIS EMAIL HAS BEEN AUTOMATICALLY GENERATED, PLEASE DO NOT RESPOND**********"

On Error Resume Next
With OutMail
    .To = "XXXX"
    .CC = ""
    .BCC = ""
    .Subject = "XXXX"
    .Body = strbody & vbNewLine & vbNewLine & "COMMENTS - " & combody
    'You can add a file like this
    '.Attachments.Add ("C:\test.txt")
    .Send 'or use.Display
End With
On Error GoTo 0

' Write
Set EmptyRow = wsLOG.Range("A" & wsLOG.Cells(Rows.Count, "A").End(xlUp).Row + 1).Resize(1, 3)
EmptyRow.Cells(1).Value2 = combody: EmptyRow.Cells(2).Value2 = Date: EmptyRow.Cells(3).Value2 = Time 'combody, date,time

Set OutMail = Nothing
Set OutApp = Nothing

MsgBox ActiveCell.Value & vbNewLine & _
"NOTIFICATION SENT"

End Sub
That is fantastic. Thanks very much!
 
Upvote 0
Nice to hear we were able to help and thanks for the feedback!
 
Upvote 0

Forum statistics

Threads
1,224,813
Messages
6,181,111
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top