VBA脚本保存文件以从一台计算机而不是其他计算机更正路径

时间:2015-12-04 21:10:52

标签: excel vba filepath

我有一张自动Excel表格,其中以下脚本是该功能的一部分。当我单击工作表上的按钮时,它会准确保存到路径中,并通过电子邮件将其发送给下一个要查看的人。

从另一台计算机运行时,它不会将其保存到路径中。请注意,我删除了错误处理程序。在删除之前,它仍会通过电子邮件发送附件,但不会保存到其他计算机的路径中。

所有用户都拥有该文件夹的完全访问权限。代码在工作表中。如果我把它放在模块中并单击按钮就没有任何反应。

Debug显示模块卡在saveas文件路径部分。

非常感谢任何帮助。

由于

Private Sub CommandButton1_Click()


If WorksheetFunction.CountA(Range("$A27:$O32")) = 0 Then
MsgBox "Order has no accessories. Please submit to Distribution.", vbOKOnly
Exit Sub
End If


Dim objOutlook As Outlook.Application
  Dim objOutlookMsg As Outlook.MailItem
  Dim objOutlookRecip As Outlook.Recipient
  Dim objOutlookAttach As Outlook.Attachment
  Dim TheAddress As String
  Dim Attachment As String
  Dim FName           As String
  Dim FPath           As String
  Dim Fmail As String
  Dim FSection As String
  Dim Ddate As String

    FPath = "N:\Administration\Vehicle Orders"
    FName = Sheets("Company Vehicle Order Form").Range("A13").Text
    FSection = Sheets("Company Vehicle Order Form").Range("A25").Text
    Ddate = Date
    ActiveWorkbook.SaveAs Filename:= _
    FPath & "\" & FName & " " & FSection & Ddate, _
    FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False

  Set objOutlook = CreateObject("Outlook.Application")


  Set objOutlookMsg = objOutlook.CreateItem(olMailItem)
  TheAddress = "ji@hdai.com"
  Attachment = FPath & "\" & FName & " " & FSection & Ddate & ".xlsm"

     With objOutlookMsg

        Set objOutlookRecip = .Recipients.Add(TheAddress)
        objOutlookRecip.Type = olTo
        Set objAoutlookAttach = .Attachments.Add(Attachment)





        .Subject = "Company Vehicle Order Form"
        .Body = "Good day. Please review the attached Vehicle Order Form approve/ omit Accessories. If omitting, just delete the part and click on 'Submit to Distribution' button in the bottom BLUE section. " & _
        vbCrLf & vbCrLf & "Thank you.  "

        .Importance = olImportanceHigh  'High importance


          .Send
      End With

   Set objOutlookMsg = Nothing
   Set objOutlook = Nothing

   ActiveWorkbook.Close
End Sub

1 个答案:

答案 0 :(得分:2)

Change the Drive Letter Alias N in

FPath = "N:\Administration\Vehicle Orders"

to the UNC path.

So:

FPath = "\\myserver\mypath\Administration\Vehicle Orders"

Since Drive Letters are just aliases and can be set to any letter desired by any user, using it variables can produce errors. The UNC path will never change though (unless IT migrates servers, but you will definitely know if that happens before it happens :))

UNC Paths can be found in Windows Explorer:

enter image description here