Outlook/VBA - find UID in Subject and run search for UID in Mail items - vba

I've been in and out of so many Microsoft docs and forums SEARCHING for the answer, I figured I'd just ask.
Long story short - I want to write a macro that, with an email highlighted in the main/mail pane in Outlook ...
Searches the subject line of the email for the case number/UID (always a four digit year, hyphen, and a six digit number, e.g., 2019-012345); and then
runs a basic search (not Advanced Find) in my inbox and all subfolders for any email with that UID in the subject line or body.
I'd offer a jumping off point but Outlook doesn't have a record macro function.
Functionally speaking ... my Outlook search option is set to include results from all folders. I hit Ctrl-E and type in the UID and hit enter to search. Would love to reduce that to one key stroke
Does the code have to get into DASL and MAPI? The extent of my VBA knowledge goes to user forms and template letters so ... I'm in way over my head.

Related

MS Access | How to get content from mail body to table?

I am working on a project where I need to get ms access table/data from mail body and execute some command in SAP. I can manage SAP part but issue is that how to get information from mail body. i tried linking my mailbox in access but it shows me all mail body text but i need some specified contents only. example
Hello,
Please supplement budget
WBS Amt
N.10002077.001 1
from above what i need is just "N.10002077.001" and "1" ,but how to get that information only in table is the issue?
Further, what I will get in my mail will be table with 2 column but access imports it as a simple text.
It is impossible to give a definite answer to your question because it is too vague but it is possible to get you started.
Have a look at this answer of mine: https://stackoverflow.com/a/12146315/973283. The question is not relevant other than the OP did not understand that showing screenshots told us little about what the body looked like to a VBA macro. The answer includes a macro that copies selected properties from every email in Inbox to an Excel worksheet. This will allow you to see what an email’s body looks like to a VBA macro.
How will you identify the emails from which you wish to extract data? The two simple choices are:
Look at every email in a folder and identify the interesting one by examining the subject, sender or some other property.
Select the interesting emails then run a macro which uses ActiveExplorer to access the selected emails.
The answer referenced above demonstrates technique 1. There are lots of answers demonstrating technique 2 but I can add an example macro if necessary.
An email typically has an Html body and a text body. If an email has an Html body, that is the one shown to the user. A macro can access either or both. Your screen shot looks like a text body although appearances can be deceptive. If it is a text body, the email does not have an Html body.
If it is a text body, the layout of the body is probably something like:
Hello,{cr}{lf}
Please supplement budget{cr}{lf}
WBS{tab}{tab}{tab}{tab}{tab}Amt{cr}{lf}
N.10002077.001{tab}{tab}1{cr}{lf}
This assumes, the sender has used variable numbers of tabs to line up the columns.
You could use Split on vbCr & vbLf to convert the string body into an array of strings with one line per array entry. Discard lines up to and including the line starting “WBS” then process each line down to any signature. Split each line on vbTab and expect to find two entries with values with the rest blank.
See how far you can get with the above hints then clarify your answer if you need more information.

How can I build custom Word fields with VBA

I've found a question in stackoverflow: "How can I build Word fields with VBA" by JonnyGold.
I'm interested in the same question, but possibly on other reasons. The answers to JonnyGold question doesn't satisfy me. I'm still in MS-word 2003. My problem is to construct a custom word field, which would recognize a bookmark name around cursor location, saves that name in some custom variable/property, so that in a case of need a hyperlink of ref field could return a cursor to the said bookmark.
I need that mechanism to facilitate an easy work with a list of bibliographic sources, so that a user can by one click to go from a reference to a source and then to return back. Note that one source could be referenced in several different places and a user should be able to return to a reference, he/she clicked before.
I tried to use REF field with MACROBUTTON field inside, but MACROBUTTON requires double or one click on a button/text, which I want to avoid. I would like to create a field {RUNMACRO MacroName}, which would run a specified VDA Macro.

Microsoft Word VBA - IF & THEN Find word and print in another sheet

I am a bit of novice when it comes to VBA (mostly navigate through the code by Recording actions and then altering it accordingly to what I need).
My current problem is the following:
I am going to compile hundreds of word Docs that contain email addresses from clients that I need. In order to make this as easy as possible, I would like to have some code that finds their emails AND additional information that surrounds the email addresses (Name, Location,Job Title,and [possibly] Phone Number) and then copies and pastes the mentioned info to another designated document. The documents and the abovementioned info are formatted as such :
FirstName LastName
Location
Job title # Company
email address - phone number
Now, I believe this will include and IF/THEN statement since not all clients have their email addresses in the documents.
So, IF there is an email address THEN copy it along with the 3 lines above and the phone number that is separated by "a space" "-" "a space"AND paste it on another sheet. IF there is no email address, then keep going.
This query code will probably include a FIND that needs to have a "#" and ".com" attached to the same string. This will be needed since the document also includes other text that has ".com" and "#" but not together.
This sounds harder than what it really is, but again I'm a novice so not completely sure. Feel free to ask any additional questions!
This is an extremely broad question, but I suggest you do the following:
Use a Scripting.FileSystemObject to iterate through files in a folder, looking for Word documents
Open the document using the Word Automation object model
Application object
Application.Documents property, and Documents collection
Documents.Add method, and the Document object, to create the destination document
Documents.Open method, to open existing documents
Find emails within the document (via the Document.Content property, and the Range object)
Range.Find property and the Find object
If the Find.Execute2007 method returns true, then:
Extend the range to the previous 3 paragraphs
Range.MoveStart method
Copy and paste the range to the destination document
You can write only the text to the destination document — Range.Text property, and the Range.Insert-* methods
Or, you can use the clipboard Range.Copy and Range.Paste
Or, you can export to an external file (Range.ExportFragment) and later import from the external file (Range.ImportFragment)

Working with multiple discontinuous selection

I'm trying to do something with a multiple selection. I wanna add some text before every selected paragraph but, when I select multiple discontinuous paragraphs, if I do Selection.Paragraphs.Count I always get "1".
How could I work with all paragraphs apart?
Example:
Paragraph1(Selected first)
Paragraph2
Paragraph3(Selected second)
What I got when I try to add some text at the beginning of these paragraphs:
Paragraph1
Paragraph2
TEXTParagraph3
What I really want to obtain:
TEXTParagraph1
Paragraph2
TEXTParagraph3
I'm working like this:
sub x()
dim p as paragraph
for each p in selection.paragraphs
p.range.insertbefore("TEXT")
next
End sub
Word simply cannot do what you'd like for it to do. Developers have wished for this since multiple selections were introduced in 2003 (I think it was, might have been version 2007). Word's object model simply does not support it.
If this is something you want to provide to the user to make life easier you'll need to give the tool a way to mark the paragraphs so your code can recognize them. You could provide a macro, for example, that assigns an incrementing bookmark name to each selection (the user selects, then runs your macro; repeat for each paragraph). Your code can then address each bookmark and perform the actions. To make this more user friendly you can assign the macro to a keyboard shortcut and/or a button in the Ribbon/QAT and/or the right-click menu.

VB, Outlook Macro: How to put variable, (fields), in an email template

I have a simple macro that reads Strings from a .csv file. I want to place (Replace) those Strings into the body of an email. The email will be opened from a template, so I want the fields in the template, and then have my macro start a new email from the template and replace the fields with the String variables.
I'm not finding anything posted that shows how to format such a field in the body of the email.
A link to a reference would be helpful.
The rest of the story: The email template body was pasted from a Word mailmerge document, so it already has mailmerge fields in the correct locations. There is probably a way to make mailmerge work in Outlook, but mailmerge in Word was problematic, and I don't know what makes mailmerge tick, so when it broke on a user's computer I had to rebuild the merge document, etc. Now we're switching to email instead of a printed Word document, and I'm more comfortable with writing a macro to explicitly place the data. I haven't done much programming in vba, so I'm picking up the syntax piece by piece as I go.
There are no fields. Your Outlook .oft template should contain unique string placeholders.
Once you .CreateItemFromTemplate, replace the unique string placeholders.
MyMail.HTMLBody = Replace(MyMail.HTMLBody, "UniqueStringPlaceholder_1", "csvstring_1")
You will likely work out some kind of loop.