我在Excel中有一个代码,当一个人一次使用该表单时,它可以很好地工作。在网络上的多台计算机上打开此表单后,将立即显示错误消息。
错误消息显示为: 运行时错误“ 1004”: 文档未保存。该文档可能已打开,或者在保存时可能遇到错误。
单击“调试”时,它突出显示以下行: ws.ExportAsFixedFormat 0,文件名
Private Sub CommandButton1_Click()
Dim d As Date
Dim m As String
Dim dy As String
Dim yr As String
Dim FileName As String
Dim a1 As String
Dim b1 As String
Dim ws As Worksheet
Dim dr As String
dr = "G:\Operations\Expedite Requests"
Set ws = Sheets("Sheet1")
myarr = Array("Date", "Customer", "PO#", "Workorder#", "Part Number", "Requested Due Date", _
"Original Promise Date", "Special Note(s)", "Material Substitutions", "Requested By")
j = 0
With ws
A5 = .Range("A5").Value
A9 = .Range("A9").Value
For i = 3 To 21 Step 2
If .Cells(i, 1).Value = "" Then
msg = msg & myarr(j) & vbLf
End If
j = j + 1
Next
If msg <> vbNullString Then
If MsgBox("Form is not complete. Do you want to continue?" & vbLf & _
"(Please Type N/A If The Information is not Avalaible)" & vbLf & "Following information is missing:" & vbLf & msg, vbQuestion + vbYesNo) <> vbYes Then
Exit Sub
End If
End If
End With
Set ws = Sheets("Expedite Form")
d = Date
m = Month(d)
dy = Day(d)
yr = Year(d)
FileName = dr & "\" & yr & "-" & m & "-" & dy & " " & A5 & A9
ws.ExportAsFixedFormat 0, FileName
With CreateObject("Outlook.Application").CreateItem(0)
Dim cell As Range
Dim strBody As String
For Each cell In ThisWorkbook.Sheets("Sheet1").Range("A1:A21")
strBody = strBody & cell.Value & vbNewLine
Next
.To = "TEST1@TEST1.com"
.CC = "TEST2@TEST2.com;
.BCC = "TEST3@TEST3.com"
.Subject = "Expedite Request"
.Body = strBody
.attachments.Add dr & "\" & yr & "-" & m & "-" & dy & " " & A5 & A9 & ".pdf"
.Send 'use .Display to display email; use .send to send email
MsgBox ("Email Sent To Operations Team")
End With
Call resetForm
End Sub
Sub resetForm()
Worksheets("Sheet1").Range("A5").Value = ""
Worksheets("Sheet1").Range("A7").Value = ""
Worksheets("Sheet1").Range("A9").Value = ""
Worksheets("Sheet1").Range("A11").Value = ""
Worksheets("Sheet1").Range("A13").Value = ""
Worksheets("Sheet1").Range("A15").Value = ""
Worksheets("Sheet1").Range("A17").Value = ""
Worksheets("Sheet1").Range("A19").Value = ""
Worksheets("Sheet1").Range("A21").Value = ""
End Sub