我正在Excel 2013/2016中运行一个发票程序。目的是创建发票,保存为PDF,创建邮件并附加PDF-完全自动化。
我的代码按行分解:
objMessage.AddAttachment=GemFilSom(指向库和文件的链接)
这是代码:
Private Sub CommandButton2_Click()
Dim Sender As String
Dim Modtager As String
Dim Emne As String
Dim Tekst As String
Dim Vedhaft As String
Dim Tekst1 As String
Dim Tekst2 As String
Dim Tekst3 As String
Dim Tekst4 As String
Dim Tekst6 As String
Dim Fulltekst As String
Dim Pw As String
Dim Port As String
Dim SMTPUdbyder As String
Dim GemFilSom As String
Const cdoSendUsingPickup = 1
Const cdoSendUsingPort = 2
Const cdoAnonymous = 0
Const cdoBasic = 1
Const cdoNTLM = 2
Sheets("Udskrift").Activate
' ActiveWindow.SelectedSheets.PrintOut Copies:=2, Collate:=True, _
' IgnorePrintAreas:=False
GemFilSom = Worksheets("FakturaList").Range("L8").Value 'lib + filename
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=GemFilSom _
, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
:=False, OpenAfterPublish:=False
Afsender = Worksheets("FakturaList").Range("L23").Value 'from
Modtager = Worksheets("FakturaList").Range("L24").Value 'to
Emne = Worksheets("FakturaList").Range("L25").Value 'Subject
Tekst1 = Worksheets("FakturaList").Range("L14").Value 'Bodytekst
Tekst2 = Worksheets("FakturaList").Range("L15").Value ' "
Tekst3 = Worksheets("FakturaList").Range("L16").Value ' "
Tekst4 = Worksheets("FakturaList").Range("L17").Value ' "
Tekst5 = Worksheets("FakturaList").Range("L18").Value ' "
Tekst6 = Worksheets("FakturaList").Range("L19").Value ' "
Pw = Worksheets("FakturaList").Range("L22").Value 'Password
Port = Worksheets("FakturaList").Range("L31").Value 'SMTP Port
SMTPUdbyder = Worksheets("FakturaList").Range("L32").Value 'SMTP Server
Vedhaft = Worksheets("FakturaList").Range("L27").Value 'Attachment 'Lib+Filename
Fulltekst = Tekst1 & vbNewLine & vbNewLine & vbNewLine & vbNewLine & Tekst2 & vbNewLine & Tekst3 & vbNewLine & vbNewLine & Tekst4 & vbNewLine & vbNewLine & Tekst5 & vbNewLine & Tekst6
'Bodytext
Set objMessage = CreateObject("CDO.Message")
objMessage.Subject = Emne
objMessage.From = Afsender
objMessage.To = Modtager
objMessage.Bcc = Afsender
**objMessage.AddAttachment = GemFilSom** ' This line doesn't work and do not 'attach
objMessage.TextBody = Fulltekst
***objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/Attachment") = GemFilSom*** objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = SMTPUdbyder
objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = cdoBasic
objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = Afsender
objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = Pw
objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = Port
objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 10
objMessage.Configuration.Fields.Update
objMessage.Send
End Sub
我已经花了4天时间测试谷歌了,怎么了……有人能帮忙吗?我对VBA 有基本的理解
最佳收益Michael
objMessage.AddAttachment
是一个方法,但您当前正尝试为其分配值,而不是向其传递参数。
代替
objMessage.AddAttachment = GemFilSom
它试图将GemFilSom
的值分配给objMessage.AddAttachment
(这不起作用,因为这不是属性)使用:
objMessage.AddAttachment GemFilSom
这是将GemFilSom
作为参数传递给方法,方法将把对象附加到邮件项。