英文:
VBA code works in a workbook but not in PERSONAL.XLSB
问题
我已经在工作簿上创建了VBA代码,该代码将该工作簿中的每个工作表作为附件发送到指定的电子邮件收件人。它运行得很好。我希望这段代码可供其他工作表使用,因此我将工作代码添加到了我的PERSONAL.XLSB中。但是,当我将相同的代码复制到PERSONAL.XLSB时,它不起作用。甚至在原来有效的工作表上也不起作用,因为我将代码移到了Personal.XLSB中。
我在Personal.xlsb中有其他宏也在工作,所以我知道我正确地使用了我的个人宏工作簿。
Sub Mail_Every_Worksheet()
'由ExtendOffice更新
Dim xWs As Worksheet
Dim xWb As Workbook
Dim xFileExt As String
Dim xFileFormatNum As Long
Dim xTempFilePath As String
Dim xFileName As String
Dim xOlApp As Object
Dim xMailObj As Object
On Error Resume Next
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
xTempFilePath = Environ$("temp") & "\"
If Val(Application.Version) < 12 Then
xFileExt = ".xls": xFileFormatNum = -4143
Else
xFileExt = ".xlsm": xFileFormatNum = 52
End If
Set xOlApp = CreateObject("Outlook.Application")
For Each xWs In ThisWorkbook.Worksheets
If xWs.Range("S2").Value Like "?*@?*.*" Then
xWs.Copy
Set xWb = ActiveWorkbook
xFileName = xWs.Name & " - " _
& Left(ThisWorkbook.Name, InStr(ThisWorkbook.Name, ".") - 1) & " "
Set xMailObj = xOlApp.CreateItem(0)
xWb.Sheets.Item(1).Range("S2").Value = ""
With xWb
.SaveAs xTempFilePath & xFileName & xFileExt, FileFormat:=xFileFormatNum
With xMailObj
'在下面指定CC、BCC、主题和正文
.To = xWs.Range("S2").Value
.CC = xWs.Range("S4").Value & ";RSimmons@oldmutual.com;SMfeka@oldmutual.com;MBehari@oldmutual.com;LFurlong@oldmutual.com;KPerumal2@oldmutual.com;IDeVries@oldmutual.com;BEllis@OLDMUTUAL.COM;AMuller4@oldmutual.com"
.BCC = ""
.Subject = ThisWorkbook.Name & " for " & xWs.Range("S1")
.Body = "Dear " & xWs.Range("S3")
.Attachments.Add xWb.FullName
.Display
End With
.Close SaveChanges:=False
End With
Set xMailObj = Nothing
Kill xTempFilePath & xFileName & xFileExt
End If
Next
Set xOlApp = Nothing
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
End Sub
我尝试了以下更改:
将ThisWorkbook.Name更改为ActiveWorkbook.Name
和
将ThisWorkbook.Worksheets更改为ActiveWorkbook.ActiveWorksheets
但然后它只生成一个电子邮件并关闭。
非常感谢您的帮助。
英文:
I have created VBA code on a workbook that sends each worksheet in that workbook as an attachment to a specified email recipient. It works great. I want this code to be available for use on other worksheets so I added the working code to my PERSONAL.XLSB. When I copy that same code to PERSONAL.XLSB it does not work. Not even on the original worksheet that it had worked on because I moved the code to Personal.XLSB
I have other macros in Personal.xlsb that work so I know I'm utilizing my personal macro workbook correctly.
Sub Mail_Every_Worksheet()
'Updateby ExtendOffice
Dim xWs As Worksheet
Dim xWb As Workbook
Dim xFileExt As String
Dim xFileFormatNum As Long
Dim xTempFilePath As String
Dim xFileName As String
Dim xOlApp As Object
Dim xMailObj As Object
On Error Resume Next
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
xTempFilePath = Environ$("temp") & "\"
If Val(Application.Version) < 12 Then
xFileExt = ".xls": xFileFormatNum = -4143
Else
xFileExt = ".xlsm": xFileFormatNum = 52
End If
Set xOlApp = CreateObject("Outlook.Application")
For Each xWs In ThisWorkbook.Worksheets
If xWs.Range("S2").Value Like "?*@?*.?*" Then
xWs.Copy
Set xWb = ActiveWorkbook
xFileName = xWs.Name & " - " _
& VBA.Left(ThisWorkbook.Name, VBA.InStr(ThisWorkbook.Name, ".") - 1) & " "
Set xMailObj = xOlApp.CreateItem(0)
xWb.Sheets.Item(1).Range("S2").Value = ""
With xWb
.SaveAs xTempFilePath & xFileName & xFileExt, FileFormat:=xFileFormatNum
With xMailObj
'specify the CC, BCC, Subject, Body below
.To = xWs.Range("S2").Value
.CC = xWs.Range("S4").Value & ";RSimmons@oldmutual.com;SMfeka@oldmutual.com;MBehari@oldmutual.com;LFurlong@oldmutual.com;KPerumal2@oldmutual.com;IDeVries@oldmutual.com;BEllis@OLDMUTUAL.COM;AMuller4@oldmutual.com"
.BCC = ""
.Subject = ThisWorkbook.Name & " for " & xWs.Range("S1")
.Body = "Dear " & xWs.Range("S3")
.Attachments.Add xWb.FullName
.Display
End With
.Close SaveChanges:=False
End With
Set xMailObj = Nothing
Kill xTempFilePath & xFileName & xFileExt
End If
Next
Set xOlApp = Nothing
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
End Sub
I tried changing
ThisWorkbook.Name to ActiveWorkbook.Name
and
ThisWorkbook.Worksheets to ActiveWorkbook.ActiveWorksheets
but then it only generates one email and closes
Any assistance would be greatly appreciated
答案1
得分: 2
使用变量区分工作簿(发送邮件)
ThisWorkbook
是对包含此代码的工作簿的引用,即PERSONAL.xlsb
,因此它在您的代码中没有地方。- 请改用
ActiveWorkbook
。问题出现在您复制工作表时,通常新创建的工作簿变为(新的)ActiveWorkbook
(更安全的是Workbook(Workbooks.Count)
),然后您不能安全地引用最初的ActiveWorkbook
。因此,请使用一个变量来引用最初的ActiveWorkbook
:
Dim swb as Workbook: Set swb = ActiveWorkbook
然后使用一个不同的变量来引用新的工作簿:dwb
。
快速修复
Sub MailEveryWorksheet()
Dim MutualMails(): MutualMails = Array( _
"RSimmons@oldmutual.com", "SMfeka@ oldmutual.com", _
"MBehari@ oldmutual.com", "LFurlong@ oldmutual.com", _
"KPerumal2@ oldmutual.com", "IDeVries@ oldmutual.com", _
"BEllis@ oldmutual.com", "AMuller4@ oldmutual.com")
Dim dTempFolderPath As String: dTempFolderPath = Environ$("temp") & "\"
Dim dFileExtension As String, dFileFormat As Long
If Val(Application.Version) < 12 Then
dFileExtension = ".xls": dFileFormat = -4143
Else
dFileExtension = ".xlsm": dFileFormat = 52
End If
If ActiveWorkbook Is Nothing Then Exit Sub ' no visible workbooks open
Dim swb As Workbook: Set swb = ActiveWorkbook
Dim swbName As String: swbName = swb.Name
Dim DotPosition As Long: DotPosition = InStrRev(swbName, ".")
Dim sBaseName As String
If DotPosition = 0 Then
sBaseName = swbName
Else
sBaseName = Left(swbName, DotPosition - 1)
End If
Dim olApp As Object: Set olApp = CreateObject("Outlook.Application")
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
Dim dwb As Workbook, sws As Worksheet
Dim dFileName As String, dFilePath As String
For Each sws In swb.Worksheets
If sws.Range("S2").Value Like "?*@?*.?*" Then
sws.Copy
Set dwb = Workbooks(Workbooks.Count)
dFileName = sws.Name & " - " & sBaseName & " " ' why the space?
dFilePath = dTempFolderPath & dFileName & dFileExtension
With dwb
.Sheets(1).Range("S2").Value = ""
Application.DisplayAlerts = False ' overwrite, no confirmation
.SaveAs dFilePath, dFileFormat
Application.DisplayAlerts = True
.Close SaveChanges:=False
End With
With olApp.CreateItem(0)
'specify the CC, BCC, Subject, Body below
.To = sws.Range("S2").Value
.CC = sws.Range("S4").Value & ";" & Join(MutualMails, ";")
.BCC = ""
.Subject = swbName & " for " & sws.Range("S1")
.Body = "Dear " & sws.Range("S3")
.Attachments.Add dFilePath
.Display
'.Send
End With
Kill dFilePath
End If
Next sws
With Application
.EnableEvents = True
.ScreenUpdating = True
End With
MsgBox "Emails sent.", vbInformation
End Sub
英文:
Use Variables to Distinguish Between Workbooks (Send Emails)
-
ThisWorkbook
is a reference to the workbook containing this code which isPERSONAL.xlsb
so it has no place in your code. -
Use
ActiveWorkbook
instead. The problem arises when you copy a sheet, most often, the newly created workbook becomes the (new)ActiveWorkbook
(safer isWorkbook(Workbooks.Count)
), then you cannot safely refer to the initialActiveWorkbook
. Therefore use a variable to reference the initialActiveWorkbook
:Dim swb as Workbook: Set swb = ActiveWorkbook
and a different one to reference the new workbooks:
dwb
.
A Quick Fix
<!-- language: lang-vb -->
Sub MailEveryWorksheet()
Dim MutualMails(): MutualMails = Array( _
"RSimmons@oldmutual.com", "SMfeka@ oldmutual.com", _
"MBehari@ oldmutual.com", "LFurlong@ oldmutual.com", _
"KPerumal2@ oldmutual.com", "IDeVries@ oldmutual.com", _
"BEllis@ oldmutual.com", "AMuller4@ oldmutual.com")
Dim dTempFolderPath As String: dTempFolderPath = Environ$("temp") & "\"
Dim dFileExtension As String, dFileFormat As Long
If Val(Application.Version) < 12 Then
dFileExtension = ".xls": dFileFormat = -4143
Else
dFileExtension = ".xlsm": dFileFormat = 52
End If
If ActiveWorkbook Is Nothing Then Exit Sub ' no visible workbooks open
Dim swb As Workbook: Set swb = ActiveWorkbook
Dim swbName As String: swbName = swb.Name
Dim DotPosition As Long: DotPosition = InStrRev(swbName, ".")
Dim sBaseName As String
If DotPosition = 0 Then
sBaseName = swbName
Else
sBaseName = Left(swbName, DotPosition - 1)
End If
Dim olApp As Object: Set olApp = CreateObject("Outlook.Application")
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
Dim dwb As Workbook, sws As Worksheet
Dim dFileName As String, dFilePath As String
For Each sws In swb.Worksheets
If sws.Range("S2").Value Like "?*@?*.?*" Then
sws.Copy
Set dwb = Workbooks(Workbooks.Count)
dFileName = sws.Name & " - " & sBaseName & " " ' why the space?
dFilePath = dTempFolderPath & dFileName & dFileExtension
With dwb
.Sheets(1).Range("S2").Value = ""
Application.DisplayAlerts = False ' overwrite, no confirmation
.SaveAs dFilePath, dFileFormat
Application.DisplayAlerts = True
.Close SaveChanges:=False
End With
With olApp.CreateItem(0)
'specify the CC, BCC, Subject, Body below
.To = sws.Range("S2").Value
.CC = sws.Range("S4").Value & ";" & Join(MutualMails, ";")
.BCC = ""
.Subject = swbName & " for " & sws.Range("S1")
.Body = "Dear " & sws.Range("S3")
.Attachments.Add dFilePath
.Display
'.Send
End With
Kill dFilePath
End If
Next sws
With Application
.EnableEvents = True
.ScreenUpdating = True
End With
MsgBox "Emails sent.", vbInformation
End Sub
答案2
得分: 0
Sub MailEveryWorksheet()
Dim MutualMails(): MutualMails = Array( _
"RSimmons@oldmutual.com", "SMfeka@oldmutual.com", _
"MBehari@oldmutual.com", "LFurlong@oldmutual.com", _
"KPerumal2@oldmutual.com", "IDeVries@oldmutual.com", _
"BEllis@oldmutual.com", "AMuller4@oldmutual.com")
Dim dTempFolderPath As String: dTempFolderPath = Environ$("temp") & "\"
Dim dFileExtension As String, dFileFormat As Long
If Val(Application.Version) < 12 Then
dFileExtension = ".xls": dFileFormat = -4143
Else
dFileExtension = ".xlsm": dFileFormat = 52
End If
If ActiveWorkbook Is Nothing Then Exit Sub ' no visible workbooks open
Dim swb As Workbook: Set swb = ActiveWorkbook
Dim swbName As String: swbName = swb.Name
Dim DotPosition As Long: DotPosition = InStrRev(swbName, ".")
Dim sBaseName As String
If DotPosition = 0 Then
sBaseName = swbName
Else
sBaseName = Left(swbName, DotPosition-1)
End If
Dim olApp As Object: Set olApp = CreateObject("Outlook.Application")
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
Dim dwb As Workbook, sws As Worksheet
Dim dFileName As String, dFilePath As String
For Each sws In swb.Worksheets
If sws.Range("S2").Value Like "?*@?*.?*" Then
sws.Copy
Set dwb = Workbooks(Workbooks.Count)
dFileName = sws.Name & " - " & sBaseName & " " ' why the space?
dFilePath = dTempFolderPath & dFileName & dFileExtension
With dwb
.Sheets(1).Range("S2").Value = ""
Application.DisplayAlerts = False ' overwrite, no confirmation
.SaveAs dFilePath, dFileFormat
Application.DisplayAlerts = True
.Close SaveChanges:=False
End With
With olApp.CreateItem(0)
'specify the CC, BCC, Subject, Body below
.To = sws.Range("S2").Value
.CC = sws.Range("S4").Value & ";" & Join(MutualMails, ";")
.BCC = ""
.Subject = swbName & " for " & sws.Range("S1")
.Body = "Dear " & sws.Range("S3")
.Attachments.Add dFilePath
.Display
'.Send
End With
Kill dFilePath
End If
Next sws
With Application
.EnableEvents = True
.ScreenUpdating = True
End With
MsgBox "Emails sent.", vbInformation
End Sub
英文:
`Sub MailEveryWorksheet()
Dim MutualMails(): MutualMails = Array( _
"RSimmons@oldmutual.com", "SMfeka@ oldmutual.com", _
"MBehari@ oldmutual.com", "LFurlong@ oldmutual.com", _
"KPerumal2@ oldmutual.com", "IDeVries@ oldmutual.com", _
"BEllis@ oldmutual.com", "AMuller4@ oldmutual.com")
Dim dTempFolderPath As String: dTempFolderPath = Environ$("temp") & "\"
Dim dFileExtension As String, dFileFormat As Long
If Val(Application.Version) < 12 Then
dFileExtension = ".xls": dFileFormat = -4143
Else
dFileExtension = ".xlsm": dFileFormat = 52
End If
If ActiveWorkbook Is Nothing Then Exit Sub ' no visible workbooks open
Dim swb As Workbook: Set swb = ActiveWorkbook
Dim swbName As String: swbName = swb.Name
Dim DotPosition As Long: DotPosition = InStrRev(swbName, ".")
Dim sBaseName As String
If DotPosition = 0 Then
sBaseName = swbName
Else
sBaseName = Left(swbName, DotPosition-1)
End If
Dim olApp As Object: Set olApp = CreateObject("Outlook.Application")
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
Dim dwb As Workbook, sws As Worksheet
Dim dFileName As String, dFilePath As String
For Each sws In swb.Worksheets
If sws.Range("S2").Value Like "?*@?*.?*" Then
sws.Copy
Set dwb = Workbooks(Workbooks.Count)
dFileName = sws.Name & " - " & sBaseName & " " ' why the space?
dFilePath = dTempFolderPath & dFileName & dFileExtension
With dwb
.Sheets(1).Range("S2").Value = ""
Application.DisplayAlerts = False ' overwrite, no confirmation
.SaveAs dFilePath, dFileFormat
Application.DisplayAlerts = True
.Close SaveChanges:=False
End With
With olApp.CreateItem(0)
'specify the CC, BCC, Subject, Body below
.To = sws.Range("S2").Value
.CC = sws.Range("S4").Value & ";" & Join(MutualMails, ";")
.BCC = ""
.Subject = swbName & " for " & sws.Range("S1")
.Body = "Dear " & sws.Range("S3")
.Attachments.Add dFilePath
.Display
'.Send
End With
Kill dFilePath
End If
Next sws
With Application
.EnableEvents = True
.ScreenUpdating = True
End With
MsgBox "Emails sent.", vbInformation
End Sub`
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论