返回列表 上一主題 發帖

excel郵件寄發多問題

謝謝stillfish、是用反了,可是現在有另一個問題;資料有兩個人同時要接收時,程式判讀只會將最後一位列在收件者上面,請問這是哪個部份有誤?
另外請問夾帶檔案位置的部份一直測試不出來是語法錯誤嗎?謝謝
  1. Sub SEND()
  2.     Dim OutApp As Object
  3.     Dim OutMail As Object
  4.     Dim FileName As String
  5.     Dim EmailTo As String
  6.     Dim EmailCC As String
  7.     Dim EmailBCC As String
  8.     Dim EmailSubject As String
  9.     Dim EmailBody As String
  10.     Dim cell As Integer
  11.    
  12.     SendType = CStr(Sheets("Sheet2").Cells(2, 2))
  13.      If SendType = "1" Then
  14.         cell = 8
  15.      ElseIf SendType = "2" Then
  16.         cell = 9
  17.      ElseIf SendType = "3" Then
  18.         cell = 10
  19.      ElseIf SendType = "4" Then
  20.         cell = 11
  21.      Else
  22.         cell = 0
  23.      End If

  24.      If cell > 0 Then
  25.         For i = 4 To 6 Step 1
  26.             If CBool(Sheets("Sheet2").Cells(i, cell)) = "TRUE" Then
  27.                 EmailTo = CStr(Sheets("Sheet2").Range("G" & i)) & ";"
  28.             End If
  29.         Next
  30.             EmailCC = CStr(Sheets("Sheet2").Range("G" & 7))
  31.             EmailBCC = CStr(Sheets("Sheet2").Range("G" & 8))
  32.             EmailSubject = CStr(Sheet2.Cells(2, 3))
  33.             EmailBody = CStr(Sheet1.Range("A" & 11)) & vbNewLine
  34.             
  35.             'If [Sheet1].Cells(8, 1) <> "" Then .AddAttachment ([Sheet1].Cells(8, 1))

  36.             If EmailCC <> "" Then
  37.                 Set rng = Sheets("Sheet1").Range("A11:G30").SpecialCells(xlCellTypeVisible)
  38.                 On Error Resume Next
  39.                 ' Only send the visible cells in the selection.
  40.                 'Set rng = Selection.SpecialCells(xlCellTypeVisible)
  41.                 On Error GoTo 0
  42.                 If rng Is Nothing Then

  43.                     MsgBox "The selection is not a range or the sheet is protected. " & _
  44.                            vbNewLine & "Please correct and try again.", vbOKOnly
  45.                     Exit Sub
  46.                 End If
  47.                 With Application
  48.                     .EnableEvents = False
  49.                     .ScreenUpdating = False
  50.                 End With

  51.                 Set OutApp = CreateObject("Outlook.Application")
  52.                 Set OutMail = OutApp.CreateItem(0)
  53.            
  54.                 On Error Resume Next
  55.                 On Error GoTo 0

  56.                 With OutMail
  57.                      .To = EmailTo
  58.                      .CC = EmailCC
  59.                      .BCC = EmailBCC
  60.                      .Subject = EmailSubject
  61.                      .body = EmailBody
  62. '                     .attachments.Add
  63.                      .display
  64.                  End With
  65.                 With Application
  66.                     .EnableEvents = True
  67.                     .ScreenUpdating = True
  68.                 End With
  69.                 Set OutMail = Nothing
  70.                 Set OutApp = Nothing
  71.             End If
  72.      End If
  73. End Sub
複製代碼

TOP

回復 11# Mindyj
line#28
     EmailTo = EmailTo & CStr(Sheets("Sheet2").Range("G" & i)) & ";"

line#67
    .attachments.Add "C:\aa.txt"
    紅色為附檔的路徑

是這樣嗎?

TOP

回復 12# stillfish00


Hi stillfish,
夾帶檔案指定為shee1的a8儲存格,我使用了
                 .AddAttachment Sheet1.Cells(8, 1)
debug會停留在.addattachment

TOP

回復 13# Mindyj


    試試?
.attachments.add Sheet1.Cells(8, 1)

不行的話會不會是A8上的附件位置有錯?
懂得發問,答案就會在其中

今日の一秒は  明日にない
http://kimbalko-chi.blogspot.com
http://kimbalko.blogspot.com

TOP

回復 14# kimbal

還是不是、很納悶,因為這一行不執行的話整個程式是沒問題的,應該是不需要另外寫function?

TOP

本帖最後由 Mindyj 於 2013-5-31 14:15 編輯

剛剛測試了如果將夾帶檔案直接輸入檔案位置是可行的,

可是使用者會依每次需求不同選擇不同的檔案、所以才將檔案位置指定輸入至Sheet1的A8儲存格,

但是當我將夾帶檔案改為指定至儲存格就一直出現錯誤,

而錯誤一直停留在.attachments.add Sheet1.Cells(8, 1)

TOP

回復 16# Mindyj
.attachments.add Sheet1.Cells(8, 1).value

TOP

本帖最後由 Mindyj 於 2013-5-31 15:06 編輯

回復 17# stillfish00
執行結果:object doesn't support this property or method

一個頭n個大、請問還有別的方式嗎?

TOP

回復 18# Mindyj
能附上執行會出錯的檔案看看嗎

TOP

回復 17# stillfish00


    更正!!!!成功了!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    謝謝stillfish!!!!!!:handshake

TOP

        靜思自在 : 時時好心就是時時好日。
返回列表 上一主題