• 0

[VBA] Some Scripting Help in Excel?


Question

Hi guys,

I've got a problem with a VBA script that someone created for Excel. This script is supposed to deal with four columns.

A - Name of user

B - email address of user

C - A value of "yes" or "no"

D - A column filled by the script (more on that in a second)

Here is the idea: When the macro is run, it will look at column C. If column C says, "Yes" then it will send an email to the address in Column B, automatically including the user's name (column A) at the top of the email. Once the email has been sent to the address, the script will fill column D with "send" so that when the script is run in future it knows not to send another message to that user.

Here is the script (taken from here which may also include a better explanation than I have):

Sub Test2()
'For Tips see: http://www.rondebruin.nl/win/winmail/Outlook/tips.htm
'Working in Office 2000-2013
	Dim OutApp As Object
	Dim OutMail As Object
	Dim cell As Range

	Application.ScreenUpdating = False
	Set OutApp = CreateObject("Outlook.Application")

	On Error GoTo cleanup
	For Each cell In Columns("B").Cells.SpecialCells(xlCellTypeConstants)
		If cell.Value Like "?*@?*.?*" And _
		   LCase(Cells(cell.Row, "C").Value) = "yes" _
		   And LCase(Cells(cell.Row, "D").Value) <> "send" Then

			Set OutMail = OutApp.CreateItem(0)

			On Error Resume Next
			With OutMail
				.To = cell.Value
				.Subject = "Reminder"
				.Body = "Dear " & Cells(cell.Row, "A").Value _
					  & vbNewLine & vbNewLine & _
						"Please contact us to discuss bringing " & _
						"your account up to date."
				'You can add files also like this
				'.Attachments.Add ("C:\test.txt")
				.Send  'Or use Display
			End With
			On Error GoTo 0
			Cells(cell.Row, "D").Value = "send"
			Set OutMail = Nothing
		End If
	Next cell

cleanup:
	Set OutApp = Nothing
	Application.ScreenUpdating = True
End Sub

My VBA coding ability isn't worth a penny, but from what I can see in that script it seems to make sense. Sure enough, when I have run a test the D column has populated with "send." But I do not receive an email.

This script is used in Excel 2010, trying to communicate with Outlook 2010 (both programs were open at the same time).

Can someone see an issue with the above script that I've missed? Or can someone think of an easier way of getting this kind of thing to work?

If anyone needs more information, let me know.

Cheers!

EDIT: I should have probably mentioned that I will be modifying the script at some stage so that it actually fits in with the exact task I'm trying to do, but for the moment I just want the "basic" script to work.

Also, if anyone has any tips for quick-learning VBA it would be appreciated.

Link to comment
https://www.neowin.net/forum/topic/1148764-vba-some-scripting-help-in-excel/
Share on other sites

3 answers to this question

Recommended Posts

  • 0
  On 24/04/2013 at 14:00, Intrinsica said:

Hi guys,

I've got a problem with a VBA script that someone created for Excel. This script is supposed to deal with four columns.

A - Name of user

B - email address of user

C - A value of "yes" or "no"

D - A column filled by the script (more on that in a second)

Here is the idea: When the macro is run, it will look at column C. If column C says, "Yes" then it will send an email to the address in Column B, automatically including the user's name (column A) at the top of the email. Once the email has been sent to the address, the script will fill column D with "send" so that when the script is run in future it knows not to send another message to that user.

Here is the script (taken from here which may also include a better explanation than I have):

Sub Test2()
'For Tips see: http://www.rondebruin.nl/win/winmail/Outlook/tips.htm
'Working in Office 2000-2013
	Dim OutApp As Object
	Dim OutMail As Object
	Dim cell As Range

	Application.ScreenUpdating = False
	Set OutApp = CreateObject("Outlook.Application")

	On Error GoTo cleanup
	For Each cell In Columns("B").Cells.SpecialCells(xlCellTypeConstants)
		If cell.Value Like "?*@?*.?*" And _
		   LCase(Cells(cell.Row, "C").Value) = "yes" _
		   And LCase(Cells(cell.Row, "D").Value) <> "send" Then

			Set OutMail = OutApp.CreateItem(0)

			On Error Resume Next
			With OutMail
				.To = cell.Value
				.Subject = "Reminder"
				.Body = "Dear " & Cells(cell.Row, "A").Value _
					  & vbNewLine & vbNewLine & _
						"Please contact us to discuss bringing " & _
						"your account up to date."
				'You can add files also like this
				'.Attachments.Add ("C:\test.txt")
				.Send  'Or use Display
			End With
			On Error GoTo 0
			Cells(cell.Row, "D").Value = "send"
			Set OutMail = Nothing
		End If
	Next cell

cleanup:
	Set OutApp = Nothing
	Application.ScreenUpdating = True
End Sub

My VBA coding ability isn't worth a penny, but from what I can see in that script it seems to make sense. Sure enough, when I have run a test the D column has populated with "send." But I do not receive an email.

This script is used in Excel 2010, trying to communicate with Outlook 2010 (both programs were open at the same time).

Can someone see an issue with the above script that I've missed? Or can someone think of an easier way of getting this kind of thing to work?

If anyone needs more information, let me know.

Cheers!

EDIT: I should have probably mentioned that I will be modifying the script at some stage so that it actually fits in with the exact task I'm trying to do, but for the moment I just want the "basic" script to work.

Also, if anyone has any tips for quick-learning VBA it would be appreciated.

The script works fine when I try it here using Excel 2010 and Outlook 2010. It sends the email and the email shows up in my external email account. Are you saving the workbook as an xlsx or xls? When I initially tried saving as an xlsx it warned me that the script wouldn't work in a macro-free workbook and that I would need to save it with a file type that would support the script so I saved it as an xls. I need to look into why it is throwing that error with xlsx since I know I have used macros in xlsx workbooks before but that was the only issue I ran into.

  • 0

Huh, how odd. So I wonder why it isn't working here then...maybe there's a security policy in place that prevents the email being sent?

I tried saving it as an .xlsx, .xls and .xlsm, none of them seemed to help sending the email out.

I guess I'll stick with it for a bit longer and see if I can make some progress.

This topic is now closed to further replies.
  • Posts

    • I talked about OnlyOffice. And stop insulting people.
    • I’m hugely disappointed by how far this site has drifted from what its community actually came here for. An occasional speculative piece is fine, but for few months large number of the feed feels like the click-bait “future hype” (not even related to tech) that used to clutter the click bait sites. Guess it's time to start looking for new venue, if its keep going down this path...
    • They probably mean File Pilot, which this colorful rainbow program can never compete with, because this WinUI crap isn’t made for apps like that. My advice? You should just switch the framework already
    • Scientists uncover bizarre new material that's breaking one of the laws of this Universe by Sayan Sen Image by Ron Lach via Pexels Scientists from the University of Chicago and UC San Diego have discovered a group of materials that behave in surprising ways when put under heat, pressure or electricity. Instead of responding like most materials, these can shrink when heated, expand when compressed, and even bounce back to their original state with the right electric charge. The work focuses on oxygen-redox (OR) materials—types that can help batteries store more energy but typically suffer from stability problems due to structural disorder. In their normal state, the materials follow the usual rules of thermodynamics. But in what's called a “metastable” state, a kind of temporary balance, they behave in reverse. “When heated, the material shrinks instead of expanding,” said Prof. Shirley Meng, senior author of the study published in Nature. This is linked to what’s known as a disorder–order transition inside the material’s structure. The team recorded a negative thermal expansion rate of −14.4(2) × 10⁻⁶ °C⁻¹, which means the material actually contracts when warmed up. This goes against a common theory called the Grüneisen relationship, which usually explains why materials expand with heat. And pressure? Even stranger. When they pushed the material on all sides at levels seen in Earth's tectonic plates, it expanded instead of getting smaller. “Negative compressibility is just like negative thermal expansion,” explained Prof. Minghao Zhang. “If you compress a particle of the material in every direction… it will expand.” They also found that electricity can reset the material’s structure. By tweaking the voltage limits, they recovered almost 100% of the original structure and performance. This has big potential for battery tech, especially electric vehicles (EVs). “When we use the voltage, we drive the material back to its pristine state. We recover the battery,” said Zhang. He added: “You just do this voltage activation… your car will be a new car. Your battery will be a new battery.” The research may lead to materials with zero thermal expansion, helpful in everything from buildings to aircraft. Zhang noted, “Take every single building, for example. You don't want the materials making up different components to change volume that often.” As they move forward, the team wants to understand how redox chemistry can further control these effects and expand practical uses. “One of the goals is bringing these materials from research to industry,” said co-first author Bao Qiu. Their work opens up a new way of thinking about material design, where energy doesn’t just power devices, but reshapes the building blocks themselves. Source: University of Chicago, Nature This article was generated with some help from AI and reviewed by an editor. Under Section 107 of the Copyright Act 1976, this material is used for the purpose of news reporting. Fair use is a use permitted by copyright statute that might otherwise be infringing.
    • "Elon Musk says a kid-friendly 'Baby Grok' is on the way" No kid wants to be a baby anymore. "Kid friendly" I would have named it "Kid Grok"
  • Recent Achievements

    • Rookie
      Snake Doc went up a rank
      Rookie
    • First Post
      nobody9 earned a badge
      First Post
    • One Month Later
      Ricky Chan earned a badge
      One Month Later
    • First Post
      leoniDAM earned a badge
      First Post
    • Reacting Well
      Ian_ earned a badge
      Reacting Well
  • Popular Contributors

    1. 1
      +primortal
      495
    2. 2
      Michael Scrip
      203
    3. 3
      ATLien_0
      197
    4. 4
      Xenon
      137
    5. 5
      +FloatingFatMan
      116
  • Tell a friend

    Love Neowin? Tell a friend!