outlook script that automatically opens links in emails - vba

I am using windows 10 on my pc and use chrome most of the time for outlook. I am trying to get a script that automatically opens a link received in a email. I have found vba scripts but they do not work i keep getting errors. If any of you could help me out with this?

You can use the HTMLBody property of the MailItem class to find all hyperlinks there. Then you can use the following method to run Chrome with a link found:
Sub LaunchInChrome (url as String)
Dim chromePath As String
chromePath = """C:\Program Files\Google\Chrome\Application\chrome.exe"""
Shell (chromePath & " -url " & url)
End Sub

Related

Shell.Execute in VBA is not doing anything

I am trying to use powershell to extract some files from a zip folder located on a drive where I only have read access to my desktop in a temp folder where I can do whatever I like to them.
Using the code below I get no errors but the powershell code just does nothing.
Am I missing something?
Sub unzip_test()
Dim myshell As Shell32.Shell
Set myshell = New Shell32.Shell
Dim args As String
args = "Expand-Archive -LiteralPath " & "'C:\Users\user1\Desktop\TEMP\examplezip.zip'" & " -destinationpath " & "'C:\Users\user1\Desktop\TEMP\tester'"
'Debug.Print (args)
myshell.ShellExecute "powershell", vargs:=args
End Sub
The debug.print prints Expand-Archive -LiteralPath 'C:\Users\user1\Desktop\TEMP\examplezip.zip' -destinationpath 'C:\Users\st11524\Desktop\TEMP\tester'
Also, I have "Microsoft Shell Controls and Automation" checked in my references.
It should work. I think the problem is somewhere in the Expand-Archive call, but you can't see the answer.
The quick fix is to add -NoExit to your call, like this
args = "-NoExit Expand-Archive -LiteralPath " ...
This will allow you to read the error before it closes, but you can't read it from your code. If you need that, have a look here
After testing this on another computer I've come to the conclusion that this is a permissions thing. My personal computer runs the script just fine where my work computer won't. I don't see anything wrong with the code listed above so I will consider this issue resolved.

MS Outlook VBA to upload email attachment to a Sharepoint with authentication

I'm still sort of a beginner in using VBA and I've been trying to figure out how to upload a file via VBA in MS Outlook to a Sharepoint. I've tried mapping the Sharepoint to my Network Drive and such but to no avail.
My code is as follows:
Public Sub saveAttachSentDate(MItem As Outlook.MailItem)
Dim oAttachment As Outlook.Attachment
Dim sSaveFolder As String
Dim file As String
Dim DateFormat As String
sSaveFolder = "(URL of the sharepoint along with the folder to save it on)"
For Each oAttachment In MItem.Attachments
DateFormat = Format(MItem.SentOn - 1, "mm.dd.yy ")
file = sSaveFolder & DateFormat & oAttachment.DisplayName
oAttachment.SaveAsFile sSaveFolder & DateFormat & oAttachment.DisplayName
Next
End Sub
My File Name is labelled like this: "[My Department] - (Client Name) Telephony Summary"
I always get this error
"Run-time error '2147024735 (800700a1)': Cannot save the attachment. File name or directory name is not valid."
I'm thinking that the probable cause is that the sharepoint I'm uploading to requires a username and password every time you access it. I tried another sharepoint using the same exact code that doesn't require login credentials and it works just fine. I can't seem to find a work-around and I'd appreciate any help!
The path passed to the SaveAsFile method can't be represented by the URL string or network location. You need to specify a local folder from which you can start uploading files. The Outlook object model doesn't provide anything for uploading files to any web servers, so you will have to do that on your own. To get that working I'd recommend developing a COM add-in. For example, VSTO based add-ins are built on top of .net framework and can use BCL classes to deal with that. See Walkthrough: Create your first VSTO Add-in for Outlook to get started quickly.

VBA - Hide Chrome window

I have a vba routine in excel that downloads something with chrome. But I'd like to stop the chrome window from opening in front of me and instead have it run in the background, keeping the focus on my excel file.
The code looks like this :
Sub dl()
Dim WebUrl As String
WebUrl = [J1]
Shell ("C:\Program Files (x86)\Google\Chrome\Application\chrome.exe -url " & WebUrl)
End Sub
I've heard of shell functions (with vbMinimizedNoFocus) which I think could do the trick but I can't find anywhere the correct syntax and an example of how to apply it...
Can anyone help me ?
Thank you very much in advance !
The documentation for Shell defines vbMinimizedNoFocus as 6 and gives the syntax:
Shell "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe -url " & WebUrl, vbMinimizedNoFocus ' Should be defined in the global namespace

How to save Outlook mails as .msg file with categories and other details?

It is easy to save emails in Outlook VBA with MailItem.SaveAs
But I don't see any option to save additional details like i.e. the Author and Categories.
The 3rd party program MessageSave allows to save mails with Categories and Author in .msg format. In Windows Explorer the columns Author and Categories show the same information like in Outlook.
Does anybody know how to save messages using Outlook VBA including these additional information?
I bought MessageSave and it's a good program but they don't allow their save function to be used in VBA. The only workaround is to let MessageSave save messages when they "arrive" in a specific folder. If necessary I can use this function but this is just a workaround.
Here is a sample how the emails saved with MessageSave are shown in Windows Explorer:
here is a process i followed: (win7 64)
web search "windows vba set extended file property"
first hit: StackOverfow 16989882
web search: "DSOFile.OleDocumentProperties"
hit microsoft: The Dsofile.dll files lets you edit Office document properties when you do not have Office installed
https://support.microsoft.com/en-us/help/224351/the-dsofile.dll-files-lets-you-edit-office-document-properties-when-yo
that is not a typo ... it ends in "when-yo"
download: DsoFileSetup_KB224351_x86.exe
open DsoFileSetup_KB224351_x86.exe using 7-zip program (from 7-zip.org)
copy dsofile.dll from DsoFileSetup_KB224351_x86.exe (using 7-zip) into a folder desktop (named "testFiles" in this example) (this could be anywhere ... maybe windows system32 or syswow64 ... i only tried on desktop )
open command prompt window as administrator
navigate to folder that contains dsofile.dll
execute following: regsvr32 dsofile.dll
should receive success confirmation
start outlook ... vba editor ... tools ... references
and find "DSO OLE Document Properties Reader 2.1" and check the box on left
back to vba editor ... create new module
paste in the following: (this is just a minimal test script)
Sub extendedProperties()
Dim objFile As OleDocumentProperties
Set objFile = CreateObject("DSOFile.OleDocumentProperties")
objFile.Open ("C:\Users\js\Desktop\testFiles\myMessage.msg") ' adjust to match your system
objFile.SummaryProperties.Subject = "My Subject"
objFile.Save
Set objFile = Nothing
End Sub
copy (drag&drop) an email "myMessage" from outlook to folder (on desktop in this example)
right-click on folder column header ... click on more ... find "subject" ...
click checkbox
ran script
subject column should contain "My Subject" next to myMessage.msg (or whatever your message is named)
there may be a simpler way ... maybe windows PowerShell has a command that could be called from vba
here is a more usable script
it has no error checking
no check for duplicate message names
no check for illegal filenames (except for ":" character)
just select a bunch of emails in any outlook folder and run this
' make sure you have a reference to "DSO OLE Document Properties Reader"
Sub extendedProperties()
Dim msg As mailItem
Dim objFile As OleDocumentProperties
' Set objFile = CreateObject("DSOFile.OleDocumentProperties")
Set objFile = New OleDocumentProperties
Dim fileName As String
Dim subjectText As String
' !!!!!!!! select a bunch of messages before running this !!!!!!!!
For Each msg In ActiveExplorer.Selection
subjectText = Replace(msg.Subject, ":", "_") ' get rid of illegal file name character (there are others)
' adjust the destination folder for your liking
fileName = "C:\Users\js\Desktop\testFiles\" & subjectText & ".msg"
Debug.Print fileName
msg.SaveAs fileName
objFile.Open fileName
objFile.SummaryProperties.Subject = "My Subject"
'objFile.Save
objFile.Close True ' save and close !!!!! duplicate filenames get overwritten !!!!!
' stop ' uncomment this line and the code will stop. press F5 to run, F8 to single-step
Next msg
Set msg = Nothing
Set objFile = Nothing
End Sub

Windows Service Seems not work appropriately

It is my first time to create a Windows Service and after read many articles on the internet, I made one by myself. It installs successfully, runs, but does not work as expected.
For example:
Inside of a Timer, I call the following code:
Public Sub WriteLog(log As String)
log = DateTime.Now.ToLongTimeString() & ", " & DateTime.Now.ToLongDateString() & vbNewLine & " -> " & log
Dim path As String = "c:\Temp\z1111.log"
Dim sw As StreamWriter
sw = File.AppendText(path)
sw.WriteLine(log)
sw.Flush()
sw.Close()
End Sub
The code above works in a Windows Form project, but as Windows Service it creates a file named z1111.log, add the content overwriting and does not do anything else, but when I delete the file, it creates again with a new date and time.
The question is: why as Windows Form it appends and as Windows Service it overwrites and stop, only works when I delete the file?
It is possible to download the project: LINK
Does anyone know how to solve it?
I solved this problem by changing the log file to the Windows folder. It has been a security issue.