2013-06-28 46 views
1

我有以下代碼:VBScript中無法找到文件中指定的服務器2003

set app = CreateObject("Excel.Application") 
Set wb = app.Workbooks.Open("Y:\Billing_Common\autoemail\*.xls") 

set sh = wb.Sheets("Auto Email Script") 
row = 2 
name = "Customer" 
email = sh.Range("A" & row) 
subject = "Billing" 
the = "the" 
LastRow = sh.UsedRange.Rows.Count 

For r = row to LastRow 
    If App.WorkSheetFunction.CountA(sh.Rows(r)) <> 0 Then 
     SendMessage email, name, subject, TRUE, _ 
     NULL, "Y:\Billing_Common\autoemail\Script\energia-logo.gif", 143,393 
     row = row + 1 
     email = sh.Range("A" & row) 
    End if 
Next 
wb.close 
set wb = nothing 
set app = nothing 


Sub SendMessage(EmailAddress, DisplayName, Subject, DisplayMsg, AttachmentPath, ImagePath, ImageHeight, ImageWidth) 

    ' Create the Outlook session. 
    Set objOutlook = CreateObject("Outlook.Application") 

    template = FindTemplate() 

    ' Create the message. 
    Set objOutlookMsg = objOutlook.CreateItem(0) 

    With objOutlookMsg 
     ' Add the To recipient(s) to the message. 
     Set objOutlookRecip = .Recipients.Add(EmailAddress) 
     objOutlookRecip.resolve 
     objOutlookRecip.Type = 1 

    ' Set the Subject, Body, and Importance of the message. 
    .Subject = Subject 
    .bodyformat = 3 
    .Importance = 2 'High importance 

    body = Replace(template, "{First}", name) 
    body = Replace(body, "{the}", the) 

    if not isNull(ImagePath) then 
     if not ImagePath = "" then 
     .Attachments.add ImagePath 
     image = split(ImagePath,"\")(ubound(split(ImagePath,"\"))) 
     body = Replace(body, "{image}", "<img src='cid:" & image & _ 
     "'" & " height=" & ImageHeight &" width=" & ImageWidth & ">") 
     end if 
    else 
     body = Replace(body, "{image}", "") 
    end if 

    if not isNull(AttachMentPath) then 
     .Attachments.add AttachmentPath 
    end if 

    .HTMLBody = body 
     .Save 
     .Send 
    End With 
    Set objOutlook = Nothing 
End Sub 

Function FindTemplate() 
    Set OL = GetObject("", "Outlook.Application") 
    set Drafts = OL.GetNamespace("MAPI").GetDefaultFolder(16) 
    Set oItems = Drafts.Items 

    For Each Draft In oItems 
     If Draft.subject = "Template" Then 
      FindTemplate = Draft.HTMLBody 
      Exit Function 
     End If 
    Next 
End Function 

時流掉我的本地機器它工作正常,但是當運行關閉Windows服務器時,它拋出在該行的錯誤:

Set wb = app.Workbooks.Open("Y:\Billing_Common\autoemail\*.xls") 

說它無法找到指定的文件,服務器上有Office 2003,我已經跑出了它爲什麼不工作的想法。

任何幫助將不勝感激!

謝謝。

+0

是映射到服務器上的'Y:'驅動器? – 2013-06-28 10:28:12

+0

是的,我檢查了命名,拼寫一切都很混亂,以至於無法在服務器上運行。我想它不是與2003兼容的,因爲我有我的2007年本地PC –

+0

也許2003不支持通配符在路徑 – 2013-06-28 10:35:41

回答

2

最有可能的Office 2003 Open方法不支持路徑通配符。您必須枚舉該文件夾中的文件:

Set app = CreateObject("Excel.Application") 
Set fso = CreateObject("Scripting.FileSystemObject") 

For Each f In fso.GetFolder("Y:\Billing_Common\autoemail").Files 
    If LCase(fso.GetExtensionName(f)) = "xls" Then 
    Set wb = app.Workbooks.Open(f.Path) 
    ... 
    wb.Close 
    End If 
Next 
+0

非常感謝安斯加爾,工作後我ammended代碼! –

+0

對於枚舉文件稍微更方便的選擇,Office 2003中仍然有Application.FileSearch對象(http://msdn.microsoft.com/en-us/library/office/aa219847(v=office.11​​).aspx) 。這已在Office 2007+中被棄用,部分被WorkBooks.Open等通配符支持取代。 –

相關問題