Getting “Remote server machine does not exist or not available” in Outlook VBScript

南楼画角 提交于 2021-02-11 16:48:02

问题


I have written on Outlook VBScript which downloads the attachment from Outlook. But now I am encountering an error

Remote Server machine does not exist or is unavailable

I get this error a few time and also sometimes this code runs without any error. I was able to track the exact point of failure. The line is

Set olns = olApp.GetNameSpace("MAPI")

I have almost tried everything yet I am not able to to find the solution.

Set Arg = WScript.Arguments
Dim item1
Dim objsubject
Dim intcount
Dim i
Dim savename
Dim vTextFile
Dim filename
Dim extension
Dim t
Dim Itimestamp
Dim savefolder
Dim vSenderEmailAddress
Dim vFlagTextFileCreate

vFlagTextFileCreate = True
savefolder = "C:\Users\SANxSAxAABOTDEV\Documents\Automation Anywhere Files\Automation Anywhere\My Tasks\ThrdOutlookTest"
vTextFile = savefolder & "\File Report.txt"

Set fso = CreateObject("Scripting.FileSystemObject")
On Error Resume Next
Set olApp = GetObject(, "Outlook.Application")
If Err.Number <> 0 Then    'Could not get instance of Outlook, so create a new one
    Err.Clear
    Set olApp = CreateObject("Outlook.Application")
End If
On Error Goto 0
Set olns = olApp.GetNameSpace("MAPI")
olns.Logon "Outlook", , False, True
Set objFolder = olns.GetDefaultFolder(6)

'objFolder.InAppFolderSyncObject = True
'syc.Start
For Each item1 In objFolder.Items
    If item1.Unread = True Then
        objsubject = item1.Subject
        If InStr(UCase(objsubject) ,"RPA BOT") Then
            intCount = item1.Attachments.Count
            If intcount > 0 Then
                For i = 1 To intcount
                    If InStr(item1.Attachments(i).filename, ".xls") Then
                        t = Now()
                        'Adding timestamp to the file to make it unique
                        Itimestamp = Right("0" & Hour(t), 2) & _
                                     Right("0" & Minute(t), 2) & _
                                     Right("0" & Second(t), 2)
                        fileName   = Left(item1.Attachments(i).filename, InStr(item1.Attachments(i).filename, ".xl") - 1)
                        extension  = Right(item1.Attachments(i).filename, Len(item1.Attachments(i).filename) - InStr(item1.Attachments(i).filename, ".xl"))
                        savename   = saveFolder & "\" & fileName & "_" & Itimestamp & "." & extension
                        item1.Attachments(i).SaveAsFile savename
                        WScript.Sleep 1000
                        If item1.SenderEmailType = "SMTP" Then
                            vSenderEmailAddress = item1.SenderEmailAddress
                        ElseIf item1.SenderEmailType = "EX" Then
                            vSenderEmailAddress = item1.Sender.GetExchangeUser.PrimarySmtpAddress
                        End If 'If item1.SenderEmailType
                        'Create InfoFile If does not exist
                        If vFlagTextFileCreate = True Then
                            vFlagTextFileCreate = False
                            fso.CreateTextFile vTextFile
                        End If
                        Set ts = fso.OpenTextFile(vTextFile, 8, True, 0)
                        ts.WriteLine fileName & "_" & Itimestamp & "." & extension & "|" & item1.Subject & "|" & vSenderEmailAddress & vbLf
                        ts.Close
                    End If 'If InStr(item1.Attachments(i).filename
                Next
                'Turning the unread mail to read
                item1.Unread = False
            End If 'If intcount > 0 Then
        End If 'If Instr(objsubject ,
    End If 'If item1.Unread=True
Next
olns.Logoff
Set olns  = Nothing
Set olApp = Nothing
WScript.Quit

I did some research and came to know that this issue may be caused due to the fact that the outlook session is already opened before running the script. So I added a patch before Set fso = CreateObject("Scripting.FileSystemObject") to kill all the outlook.exe session. I did not get any error for a while. I don't know if this is correct. I would be happy to know your feedback on this.


回答1:


Killing the outlook.exe session before I create the object for Outlook.Application solved my error. I added a patch before Set fso = CreateObject("Scripting.FileSystemObject") to kill outlook.exe sessions. Below is the patch

strComputer = "."
Set Arg  = WScript.Arguments
Process = "outlook.exe"
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") 
Set colProcess = objWMIService.ExecQuery ("Select * from Win32_Process Where Name like '" & Process & "%'")
For Each p in colProcess
  On Error Resume Next
          p.Terminate   
  On Error GoTo 0          
Next
SET objWMIService = Nothing
SET colProcess = Nothing


来源:https://stackoverflow.com/questions/55956086/getting-remote-server-machine-does-not-exist-or-not-available-in-outlook-vbscr

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!