Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Error while running VBscript from sql

Status
Not open for further replies.

travelnitwit

Technical User
Oct 11, 2013
5
US
I have a vbscript that looks at unread emails in an inbox and saves the attachment in a folder. This script works if I click on it manually but hangs when I run it from SQL server 2005. Can some please let me know what could be wrong with it?

Here is the script:

Code:
Const olFolderInbox = 6

Set objOutlook = CreateObject("Outlook.Application")
Set objNamespace = objOutlook.GetNamespace("MAPI")
Set objMailbox = objNamespace.Folders("mailbox - ABC")
Set objFolder = objMailbox.Folders("Inbox")

Set colItems = objFolder.Items
Set colFilteredItems = colItems.Restrict("[UnRead] = True")

For Each objMessage in colFilteredItems
    intCount = objMessage.Attachments.Count
    If intCount > 0 Then
        For i = 1 To intCount
            objMessage.Attachments.Item(i).SaveAsFile "C:\" &  _
                objMessage.Attachments.Item(i).FileName
        Next 
    End If
    objMessage.Unread = False
Next

This is what i use in SQL to run the script:
SQL:
DECLARE @SQLCommand VARCHAR(400)

SET @SQLCommand = 'cscript C:\Outlook.vbs'

EXEC master..xp_cmdshell @SQLCommand
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top