convert outlook msg to html using vbscript - vbscript

I'm pretty new to vbscript and I'm just writing a simple script that converts an msg file to html. So far I have:
Dim objshell,BaseName,outlookapp,emailPath
Set objshell= CreateObject("scripting.filesystemobject")
Set outlookapp = CreateObject("Outlook.Application")
Set email = outlookapp.CreateItemFromTemplate(emailPath)
BaseName = objshell.GetBaseName(emailPath)
emailPath = "C:\Users\makkerman\Desktop\email folder\test.msg"
email.saveas objshell.GetParentFolderName(emailPath) & BaseName & ".html", olFormatHTML
outlookapp.Quit
However, I get no output (and no errors). Can someone enlighten me? Thanks in advance.
Side note: how would I write this so that my current instance of Outlook doesn't close when I run the script as it currently does?

You are using OlBodyFormat.olFormatHTML (2), but you need OlSaveAsType.olHTML (5).

Related

Encoded VBS not working when called from HTA

We have an HTA used for automatic log in to the servers using VBS. To maintain security, we wanted to encode the VBS, which had the credentials for logging in to the server. We came across a VBS script that encoded the VBS file when dragged on to it, the output is a VBE file.
Now, when this VBE is called from HTA, it shows and error, which seems like it is not able to read the VBE properly.
Below is how we are linking the VBE to out HTA :
<script language="VBScript" src="hola.vbe" > </script>
Also, below is the code for encoding :
Option Explicit
dim oEncoder, oFilesToEncode, file, sDest
dim sFileOut, oFile, oEncFile, oFSO, i
dim oStream, sSourceFile
set oFilesToEncode = WScript.Arguments
set oEncoder = CreateObject("Scripting.Encoder")
For i = 0 to oFilesToEncode.Count - 1
set oFSO = CreateObject("Scripting.FileSystemObject")
file = oFilesToEncode(i)
set oFile = oFSO.GetFile(file)
Set oStream = oFile.OpenAsTextStream(1)
sSourceFile=oStream.ReadAll
oStream.Close
sDest = oEncoder.EncodeScriptFile(".vbs",sSourceFile,0,"")
sFileOut = Left(file, Len(file) - 3) & "vbe"
Set oEncFile = oFSO.CreateTextFile(sFileOut)
oEncFile.Write sDest
oEncFile.Close
Next
According to my understanding the encoded VBS should work as normal one, not sure why we are fading issue in this case.
In order to use an encoded VBScript, you need to specify the language engine to use with language="VBScript.Encode" rather than just language="VBScript".
Also, be quite wary if you want to use it "To maintain security". The purpose of the script encoder is to deter casual inspection, but it doesn't "encrypt" the code in any conventional sense, and it's not that hard to get the plain script back.

vbscript to save sync issues folder emails to a folder

Background:
I am a sysadmin for several Windows 7 machines. We have switched from an on-premises Exchange server to Office 365 Exchange Online. The computers are using Outlook 2013/2010 to connect to Exchange Online. All the clients are using full cache mode, so there is a complete OST cache file on each computer.
Because of this, there are now the following folders showing in the Folder view of Outlook: Sync Issues, then (subfolders) Conflicts, Local Failures and Server Failures.
I could not find a way to remotely administer or view these messages, so my only option appears to be manually opening each user's Outlook and viewing the folders. I have tried setting up rules to forward any messages that appear in these folders to me, but I could not get them to work on new emails that appear in these folders.
I would like to write a script that will save the email messages in these folders to a network folder where I can view them later. I would also like to have the script run on an input text file of all the computer names on the network, if possible.
I have no script writing background or knowledge. This is what I have pieced together from the web so far:
Dim OL, NmeSpace
Set OL = CreateObject("Outlook.Application")
Set NmeSpace = OL.GetNamespace("MAPI")
Set Inbx = NmeSpace.GetDefaultFolder(olFolderSyncIssues)
Set Fldr = Application.ActiveExplorer.CurrentFolder
DirName = "C:\Emails\"
For Each itm In Fldr.Items
' Save email as a file.
Next
Thanks in advance for any advice you can offer. I am studying scripting but I do not have the knowledge yet to write the script I want.
Here is how you would go about doing that with a vbscript. Right now it only does the oSyncFolder as I left out the other two folders so you could get some practice writing it yourself.
Dim oApp
Dim ns
Dim oSyncFolder
Dim oConflictFolder
Dim oSrvFailuresFolder
Dim dirName
dirName = "PathToFolder\"
Set oApp = CreateObject("Outlook.Application")
Set ns = oApp.GetNamespace("MAPI")
Set oSyncFolder = ns.GetDefaultFolder(20)'olFolderSyncIssues
Set oConflictFolder = ns.GetDefaultFolder(19)'olFolderConflicts
Set oSrvFailuresFolder = ns.GetDefaultFolder(22)'olFolderServerFailures
Dim iCounter
iCounter = oSyncFolder.Items.Count
For iCounter = oSyncFolder.Items.Count To 0 Step -1
Dim oItem
Set oItem = oSyncFolder.Items.Item(iCounter)
oItem.SaveAs dirName & "email" & CStr(iCounter) & ".msg"
'uncomment line below to remove the item after it has been relocated.
'oItem.Delete
Next
Set oSyncFolder = Nothing
Set oConflictFolder = Nothing
Set oSrvFailuresFolder = Nothing
Set oApp = Nothing
msgbox "Finished"

Need Assistance with VBSCRIPT to capture screenshots of user's desktop

I am a novice when it comes to object oriented program. That said, I have been trying to construct a vbscript that will capture a screenshot of the desktop and immediately save it to a folder I specify. Here is the code I have so far:
' START
Dim screenSize
screenSize = New screenSize.Size(Screen.PrimaryScreen.Bounds.X,Screen.PrimaryScreen.Bounds.Y)
Dim screenGrab
screenGrab = New screenGrab.Bitmap(My.Computer.Screen.Bounds.Width, my.Computer.Screen.Bounds.Height)
Dim g
g = g.System.Drawing.Graphics.FromImage(screenGrab)
dim copyS
copyS = Graphics.CopyPixels4.PaintEventArgs
dim copyS2
copyS2 = copyS.Graphics.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation)
dim saveTo
saveTo = screenGrab.Save("C:\temp\screenGrab.bmp")
' END
I prefer to keep this in VBSCRIPT as this script will be incorporated into an existing vbscript I created. I currently get an error at line 3 stating "class not defined 'screensize". I am also concerned that even if I fix the error at line 3 I may run into other syntax issues afterward. The overall intent of the script is to 1) get the screen dimensons ; 2) perform the screenshot ; 3) and save the file to a destination. I am open to any suggestions to make this work.
I appreciate any help I can get at this point. Thank you.
It seems like you have messed VB.NET with VBScript.
screenSize, screenGrab, System.Drawing.Graphics - there are no such classes in VBScript by default.
What I'd suggest is to use some screen capture ActiveX (google it).
Or create your own ActiveX with VB6 by using code like this. Create new ActiveX project in VB6, add that module and compile.
And remember to run regsvr32.exe youractivex.ocx before using it in your script.

Lotus Notes - lotusscript: shell function: illegal function call

I have a problem: I want to run a file from lotusscript code:
Dim result As Integer
result = Shell("D:\testF.dsx", 1)
And I get the following error: Illegal function call.
If I want to execute it from formula it works:
#Command([Execute]; "D:\\testF.dsx")
Thanks a lot!
I had the same problem with the shell function in lotus script with PDF files. And my workaround is to use the windows scripting host to launch the file. I am pretty sure this will solve your problem too.
Set objShell = CreateObject("WScript.Shell")
returnValue = objShell.Run("d:\testF.dsx", 3, false)enter code here
It is not possible to "Execute" a Textfile. usually there is no "run" function defined for dsx- files.
You could do it like:
Dim result as Integer
result = Shell("notepad.exe D:\testF.dsx", 1)
Or find out, which program is linked to dsx (via registry) and execute the corresponding exe with filename as Parameter. If the filename contains spaces, then it has to be enclosed:
Dim result as Integer
result = Shell({notepad.exe "D:\testF.dsx"}, 1)
And reading your last question this approach for sure is NOT the right for your request. You have to use "open" in order to process files... Like Per Hendrik told you in his response.
I had similar problems passing parameters to Outlook.
On some windows machines, this #Formula worked perfectly:
#Command([Execute]; "Outlook.exe"; "/recycle")
On Windows Terminal Servers, it caused Outlook to be unable to parse the "/recycle"
The Shell-command from LotusScript wasn't even able to locate the Outlook.exe, as it was not in the PATH.
Ralf's code helped me in this respect. The "WScript.Shell" seems able to interact with Windows registry settings. Anyway, here is the code that works for activating an open Outlook window.
Dim objShell, returnValue
Set objShell = CreateObject("WScript.Shell")
returnValue = objShell.Run("outlook.exe /recycle", 3, False)

Save images in Outlook 2007

Programmatically, of course.
Having already asked this question on superuser, I'm looking at writing a simple macro to pull down the displayed image in an HTML message (email or feed) in outlook 2007, and allow me to save it to disk.
Unfortunately, I havent been able to find where in the OL object model I can reference either linked images, or the html content itself. Finding attached files is easy, its the linked/displayed images that is my issue.
Any help? Of course, if you have a better non-programmatic answer, I'll be glad to see that - over on superuser, of course...
This is based on the MSDN docs. I don't have Outlook to test it.
Assuming you have an email message open, you can call GetInspector method on the MailItem instance that you have & use its HTMLEditor property to get handle to the DOM.
From here on, you can call regular methods such as document.Images to get handle to all the image elements. I don't know, how one can save it locally to a disk but I am sure, there must be some method to do it.
I had a second look at shahkalpeshs answer and came up with the following solution:
(Written in Outlook 2003)
Option Explicit
Private Sub getImages()
Dim xmlhttp_ As xmlhttp
Dim htmldoc As Object
Dim currentImage As Object
Dim currentResponse() As Byte
Dim startTime As Date
Dim maxTime As Long
Dim pathFolder As String
Dim pathFull As String
Dim nrFile As Integer
pathFolder = "C:\YourFolder\Images\" '"(small fix for stackoverflow syntaxhighlighter)
maxTime = 30 ' max time to load 1 File in seconds '
If Me.ActiveWindow.CurrentItem.GetInspector.EditorType = olEditorHTML Then
Set htmldoc = Me.ActiveWindow.CurrentItem.GetInspector.HTMLEditor
Set xmlhttp_ = New xmlhttp
For Each currentImage In htmldoc.images
xmlhttp_.Open "GET", currentImage.src
If Left(currentImage.src, 8) <> "BLOCKED:" Then
xmlhttp_.Send
startTime = Now
pathFull = pathFolder & currentImage.nameProp
pathFull = Replace(pathFull, "?", vbNullString)
pathFull = Replace(pathFull, "&", vbNullString)
Do While xmlhttp_.readyState <> 4
If DateTime.DateDiff("s", startTime, Now) > maxTime Then Exit Do
DoEvents
Loop
If xmlhttp_.readyState = 4 Then
If Dir(pathFull) <> "" Then Kill pathFull
nrFile = freeFile
Open pathFull For Binary As #nrFile
currentResponse = xmlhttp_.responseBody
Put #nrFile, , currentResponse
Close #nrFile
End If
End If
Next currentImage
End If
Set xmlhttp_ = Nothing
Set currentImage = Nothing
Set htmldoc = Nothing
End Sub
This code will download all the images that are displayed in your ActiveWindow and save them in a folder.
You will need to add a Reference to Microsoft XML (any version >= 2.6 should work) through Tools->References in the VBA Editor
If you want to, you can also set a Reference to the Microsoft HTML Object Library and change:
Dim htmldoc As Object
Dim currentImage As Object
to:
Dim htmldoc As HTMLDocument
Dim currentImage As HTMLImg
Regarding your comment:
#marg, Thanks for the detailed response. I still cant believe the solution has to be so convoluted - the image is already displaying, why should I have to download it again? And what if I want to save only a single image? (In Outlook 2003, you were able to right click on the image and select save as... now no more.) Since this is the closes to an actual workable solution, and since there doesnt seem to be any better solution in current Outlook - i'm giving you the bounty...
I don't have 2007 to look for a non-programming solution.
I do not believe that the MailItem Object (CurrentItemin my solution is a MailItem) differs much between the versions (but I base that assumption on 0 % research :D) and I was not able to locate the direct local path where the displayed images are stored even though I am pretty sure they should be in your browsers cache-folder. Crawling your folder for a file with the name currentImage.nameProp and copying it to your destination folder would be an alternative solution. Simply redownloading the image should not be that bad :D

Resources