I know how to add a script and run it, but I am not familiar with VBA itself.
I'm not particularly familiar with VBA either. Anything I do in it takes me ages to write & test.
If I am writing something new or just trying out lines of code I've found online I often find it useful to display each new command line in a MsgBox instead of actioning it so I can see exactly what it's doing with the variables in use and then follow that line with a Stop line so it does not go any further.
So, for example, my line
Code:
DTG = Mid(myItem.SentOn, 7, 4) & Mid(myItem.SentOn, 4, 2) & Left(myItem.SentOn, 2) & " " & Mid(myItem.SentOn, 12, 2) & Mid(myItem.SentOn, 15, 2) & Mid(myItem.SentOn, 18, 2)
started life as two lines
Code:
MsgBox Mid(myItem.SentOn, 7, 4) & Mid(myItem.SentOn, 4, 2) & Left(myItem.SentOn, 2) & " " & Mid(myItem.SentOn, 12, 2) & Mid(myItem.SentOn, 15, 2) & Mid(myItem.SentOn, 18, 2)
Stop 'Check this before letting the code run any further
and I'd gradually work my way down the draft script testing each bit as I went. The 'proper' ways of doing this are built-in to the VBA Editor and include use of the Immediates pane.
That script I posted above was my very first VBA script and it took me at least a week to write.
I've been using it since the late nineties to set my email Subject fields before dragging emails into File explorer folders for long-term storage where the email Subject becomes its filename.
- Hence the title of that VBA above. Each file in a File explorer folder needs a unique filename so I have to do something with sets of emails all called RE This weekly report - discussions
- I discovered a bug in the code in 2021 after having used the script for over twenty years. A proper programmer might have coped with that midnight condition straightaway but I had not even thought about it until it happened. I have added the midnight correction to the earlier post just in case you end up playing with it. [I must have copied & pasted it from an earlier backup before. It is now exactly what I use in Outlook.]
One complicating factor in VBA is that varies between Office applications albeit only to a small extent. So you have to search on, say, Outlook VBA SaveAs to avoid picking up irrelevant results concerning Excel VBA.
- This is eased for me because I have not updated from Office 2007 & my VBA Help is built-in to each application rather than being obtained through online searches so I don't get messed up by stray results.
- There are lots of online guides but I tend to use them as references to help me overcome hurdles when I'm writing something new [once a year, if that]. I've never made the time to sit down & study VBA properly [or PowerShell].
All the best,
Denis