【问题标题】:VBScript Cannot Find file Specified Server 2003VBScript 找不到文件指定的服务器 2003
【发布时间】:2013-06-28 08:23:52
【问题描述】:

我有以下代码:

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,我想不出它为什么不工作。

任何帮助将不胜感激!

谢谢。

【问题讨论】:

  • Y: 驱动器是否映射到服务器上?
  • 是的,我检查了命名,所有拼写都如此混乱以至于无法在服务器上运行。我认为它与 2003 不兼容,因为我的本地 PC 上有 2007
  • 也许 2003 不支持路径中的通配符

标签: excel vbscript outlook windows-server-2003


【解决方案1】:

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

【讨论】:

  • 对于枚举文件的更方便的替代方法,Office 2003 仍然具有 Application.FileSearch 对象 (msdn.microsoft.com/en-us/library/office/…)。这在 Office 2007+ 中已被弃用,部分被 WorkBooks.Open 等中的通配符支持所取代。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多