vba code - Replace text in html email without breaking formatting - vba

I am trying to replace specific text in an email (skype meeting invite) with a custom piece of text that is a URL. I can do this fine for plain text via a vba macro in outlook click to run (2016/2019), but if I run the below against the expected html email that the meetings create it breaks the formatting completely.
Sub ChangeS4bMeeting()
Application.ActiveInspector.CurrentItem.body = Replace(Application.ActiveInspector.CurrentItem.body, "Conference ID: ", "https://linkhere")
End Sub
It ends up replacing the text correctly and adding what follows that space after the colon to the replaced text as the URL, but formatting of the whole invite email breaks.
I have tried using "HTMLBody" value instead of "body" value but this fails to run at all as it breaks on that line. Any ideas? Would it also be possible to have this run automatically when I selected New Items > Skype Meeting in outlook rather than running the macro manually (or have the macro include the creation of a skype meeting directly)?
Thanks in advance!

You must ensure that the content placed in the HTMLBody is properly formatted and that any such text is placed between the and tags in the HTMLBody.
For more information, please see the following link:Find and Replace as a VBA Macro
You can create custom rules that execute scripts to implement specific conditions for executing the script.
please see the following link: Create a Custom Rule executing a Script

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.

VBA Macro for MS Word to Ignore Spelling Errors in Selected Block of Text

VBA Noob here. I take my python programming notes in a word document since I can import images into it and align/format text quickly. Any code pasted into this document comes up as a spelling error. I'm trying to find a way to ignore spelling errors within a selected text area so I don't have to deal with ignoring each spelling error line of code individually. I don't want to turn off spell check in the document.
Ideally, I'd able to write a macro that read:
Selection.ShowSpellingErrors = True
but ShowSpellingErrors() can only be used with ActiveDocument. I was able to a record a macro that ignored spelling errors with:
Selection.LanguageID = wdEnglishUS
Selection.NoProofing = True
However, any new text I type into this also doesn't get proofed, which is something I don't want. I want to be able to write new text and see any errors I make. Thanks for any help!
Not a VBA Macro, but I think this answer may be relevant to your problem anyway.
Try creating a style for code which does not include spell check. Anything with this style does not get spell checked, while the rest of the document does. Sometimes I find the code shows the red underline, but if you run spell check it should just disappear without needing to be 'fixed'.
Create a new style, in the modify formatting dialog, go to Format > Language:
Tick the 'Do not check spelling or grammar' checkbox:
Highlight your code and use the new style. Any text not in this style will still be spellchecked:

Microsoft Excel Search Engine - Hyperlink Results Provides Error Message

I've been trying for two days to figure out this problem.
I have created an Excel search engine to search through a database of hyperlinks in the same worksheet. I want the results to display working hyperlinks, but right now it just displays the text. When the hyperlink is clicked, I get an error message - "Cannot open the specified file."
Can someone please help me. I've tried multiple codes including the following:
=HYPERLINK("#"&VLOOKUP(D5,A2:C91,3,FALSE),""""))) to no avail.
I can also email the spreadsheet if a email is provided. Thank you!
A hyperlink cannot start with the # sign. If the link is to a file, then you need to start with the file path, like shown in the Excel help
=HYPERLINK("D:\FINANCE\1stqtr.xlsx", H10)
If the link goes to another sheet in the same file, use
=HYPERLINK("[Budget.xlsx]E56", E56)
The Vlookup will need to return the correct file path or a valid URL, including the "http://"

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.

macro to return active document format for IF/Then statement

I am creating a document template for a report for my staff to use and I have a command button at the bottom that will delete all of the command buttons in the report and protect it as read only to close out the report.
I don't want someone accidentally making these changes to the template if they happen to open that instead of a new document based on it.
So I would like a string of code that checks the active document, if it is .dotm I want it to display a message box and exit. if it is a .docx I want it to continue with the rest of the code I have written.
I have been unable to return the format or use it in an IF/THEN statement. I have been unable to find anything on the net on this either. Is it impossible? or should I be checking for the file extension? If so how would I use that as a value in an IF/THEN Statement?
The document may have been based on the template, but not yet saved. In which case it would be called "Document1", etc., without a dot.
If InStr(ActiveDocument.Name,".") = 0 Then
'it is a new document, based on a template
ElseIf InStr(ActiveDocument.Name,".dotm") > 0 Then
'it is a/the template
This of course assumes that the ActiveDocument is the correct one. If they click a button in the document then this is correct, but if they use the Macros dialog then you may want to include additional checks.
I would use the following, which ignores differences in case (.dotm, .DOTm):
If InStr(UCase(ActiveDocument.Name), ".DOTM") > 0 Then
'it is a template..
Else
'it's just a document
End If
Checking ActiveDocument.AttachedTemplate.Name can also be useful, to confirm if the active-document is one based on your template.