VBA代码在工作簿中运行,但在PERSONAL.XLSB中不运行。

huangapple go评论76阅读模式
英文:

VBA code works in a workbook but not in PERSONAL.XLSB

问题

我已经在工作簿上创建了VBA代码,该代码将该工作簿中的每个工作表作为附件发送到指定的电子邮件收件人。它运行得很好。我希望这段代码可供其他工作表使用,因此我将工作代码添加到了我的PERSONAL.XLSB中。但是,当我将相同的代码复制到PERSONAL.XLSB时,它不起作用。甚至在原来有效的工作表上也不起作用,因为我将代码移到了Personal.XLSB中。

我在Personal.xlsb中有其他宏也在工作,所以我知道我正确地使用了我的个人宏工作簿。

  1. Sub Mail_Every_Worksheet()
  2. '由ExtendOffice更新
  3. Dim xWs As Worksheet
  4. Dim xWb As Workbook
  5. Dim xFileExt As String
  6. Dim xFileFormatNum As Long
  7. Dim xTempFilePath As String
  8. Dim xFileName As String
  9. Dim xOlApp As Object
  10. Dim xMailObj As Object
  11. On Error Resume Next
  12. With Application
  13. .ScreenUpdating = False
  14. .EnableEvents = False
  15. End With
  16. xTempFilePath = Environ$("temp") & "\"
  17. If Val(Application.Version) < 12 Then
  18. xFileExt = ".xls": xFileFormatNum = -4143
  19. Else
  20. xFileExt = ".xlsm": xFileFormatNum = 52
  21. End If
  22. Set xOlApp = CreateObject("Outlook.Application")
  23. For Each xWs In ThisWorkbook.Worksheets
  24. If xWs.Range("S2").Value Like "?*@?*.*" Then
  25. xWs.Copy
  26. Set xWb = ActiveWorkbook
  27. xFileName = xWs.Name & " - " _
  28. & Left(ThisWorkbook.Name, InStr(ThisWorkbook.Name, ".") - 1) & " "
  29. Set xMailObj = xOlApp.CreateItem(0)
  30. xWb.Sheets.Item(1).Range("S2").Value = ""
  31. With xWb
  32. .SaveAs xTempFilePath & xFileName & xFileExt, FileFormat:=xFileFormatNum
  33. With xMailObj
  34. '在下面指定CCBCC、主题和正文
  35. .To = xWs.Range("S2").Value
  36. .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"
  37. .BCC = ""
  38. .Subject = ThisWorkbook.Name & " for " & xWs.Range("S1")
  39. .Body = "Dear " & xWs.Range("S3")
  40. .Attachments.Add xWb.FullName
  41. .Display
  42. End With
  43. .Close SaveChanges:=False
  44. End With
  45. Set xMailObj = Nothing
  46. Kill xTempFilePath & xFileName & xFileExt
  47. End If
  48. Next
  49. Set xOlApp = Nothing
  50. With Application
  51. .ScreenUpdating = True
  52. .EnableEvents = True
  53. End With
  54. 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.

  1. Sub Mail_Every_Worksheet()
  2. &#39;Updateby ExtendOffice
  3. Dim xWs As Worksheet
  4. Dim xWb As Workbook
  5. Dim xFileExt As String
  6. Dim xFileFormatNum As Long
  7. Dim xTempFilePath As String
  8. Dim xFileName As String
  9. Dim xOlApp As Object
  10. Dim xMailObj As Object
  11. On Error Resume Next
  12. With Application
  13. .ScreenUpdating = False
  14. .EnableEvents = False
  15. End With
  16. xTempFilePath = Environ$(&quot;temp&quot;) &amp; &quot;\&quot;
  17. If Val(Application.Version) &lt; 12 Then
  18. xFileExt = &quot;.xls&quot;: xFileFormatNum = -4143
  19. Else
  20. xFileExt = &quot;.xlsm&quot;: xFileFormatNum = 52
  21. End If
  22. Set xOlApp = CreateObject(&quot;Outlook.Application&quot;)
  23. For Each xWs In ThisWorkbook.Worksheets
  24. If xWs.Range(&quot;S2&quot;).Value Like &quot;?*@?*.?*&quot; Then
  25. xWs.Copy
  26. Set xWb = ActiveWorkbook
  27. xFileName = xWs.Name &amp; &quot; - &quot; _
  28. &amp; VBA.Left(ThisWorkbook.Name, VBA.InStr(ThisWorkbook.Name, &quot;.&quot;) - 1) &amp; &quot; &quot;
  29. Set xMailObj = xOlApp.CreateItem(0)
  30. xWb.Sheets.Item(1).Range(&quot;S2&quot;).Value = &quot;&quot;
  31. With xWb
  32. .SaveAs xTempFilePath &amp; xFileName &amp; xFileExt, FileFormat:=xFileFormatNum
  33. With xMailObj
  34. &#39;specify the CC, BCC, Subject, Body below
  35. .To = xWs.Range(&quot;S2&quot;).Value
  36. .CC = xWs.Range(&quot;S4&quot;).Value &amp; &quot;;RSimmons@oldmutual.com;SMfeka@oldmutual.com;MBehari@oldmutual.com;LFurlong@oldmutual.com;KPerumal2@oldmutual.com;IDeVries@oldmutual.com;BEllis@OLDMUTUAL.COM;AMuller4@oldmutual.com&quot;
  37. .BCC = &quot;&quot;
  38. .Subject = ThisWorkbook.Name &amp; &quot; for &quot; &amp; xWs.Range(&quot;S1&quot;)
  39. .Body = &quot;Dear &quot; &amp; xWs.Range(&quot;S3&quot;)
  40. .Attachments.Add xWb.FullName
  41. .Display
  42. End With
  43. .Close SaveChanges:=False
  44. End With
  45. Set xMailObj = Nothing
  46. Kill xTempFilePath &amp; xFileName &amp; xFileExt
  47. End If
  48. Next
  49. Set xOlApp = Nothing
  50. With Application
  51. .ScreenUpdating = True
  52. .EnableEvents = True
  53. End With
  54. 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
  1. Dim swb as Workbook: Set swb = ActiveWorkbook

然后使用一个不同的变量来引用新的工作簿:dwb

快速修复

  1. Sub MailEveryWorksheet()
  2. Dim MutualMails(): MutualMails = Array( _
  3. "RSimmons@oldmutual.com", "SMfeka@ oldmutual.com", _
  4. "MBehari@ oldmutual.com", "LFurlong@ oldmutual.com", _
  5. "KPerumal2@ oldmutual.com", "IDeVries@ oldmutual.com", _
  6. "BEllis@ oldmutual.com", "AMuller4@ oldmutual.com")
  7. Dim dTempFolderPath As String: dTempFolderPath = Environ$("temp") & "\"
  8. Dim dFileExtension As String, dFileFormat As Long
  9. If Val(Application.Version) < 12 Then
  10. dFileExtension = ".xls": dFileFormat = -4143
  11. Else
  12. dFileExtension = ".xlsm": dFileFormat = 52
  13. End If
  14. If ActiveWorkbook Is Nothing Then Exit Sub ' no visible workbooks open
  15. Dim swb As Workbook: Set swb = ActiveWorkbook
  16. Dim swbName As String: swbName = swb.Name
  17. Dim DotPosition As Long: DotPosition = InStrRev(swbName, ".")
  18. Dim sBaseName As String
  19. If DotPosition = 0 Then
  20. sBaseName = swbName
  21. Else
  22. sBaseName = Left(swbName, DotPosition - 1)
  23. End If
  24. Dim olApp As Object: Set olApp = CreateObject("Outlook.Application")
  25. With Application
  26. .ScreenUpdating = False
  27. .EnableEvents = False
  28. End With
  29. Dim dwb As Workbook, sws As Worksheet
  30. Dim dFileName As String, dFilePath As String
  31. For Each sws In swb.Worksheets
  32. If sws.Range("S2").Value Like "?*@?*.?*" Then
  33. sws.Copy
  34. Set dwb = Workbooks(Workbooks.Count)
  35. dFileName = sws.Name & " - " & sBaseName & " " ' why the space?
  36. dFilePath = dTempFolderPath & dFileName & dFileExtension
  37. With dwb
  38. .Sheets(1).Range("S2").Value = ""
  39. Application.DisplayAlerts = False ' overwrite, no confirmation
  40. .SaveAs dFilePath, dFileFormat
  41. Application.DisplayAlerts = True
  42. .Close SaveChanges:=False
  43. End With
  44. With olApp.CreateItem(0)
  45. 'specify the CC, BCC, Subject, Body below
  46. .To = sws.Range("S2").Value
  47. .CC = sws.Range("S4").Value & ";" & Join(MutualMails, ";")
  48. .BCC = ""
  49. .Subject = swbName & " for " & sws.Range("S1")
  50. .Body = "Dear " & sws.Range("S3")
  51. .Attachments.Add dFilePath
  52. .Display
  53. '.Send
  54. End With
  55. Kill dFilePath
  56. End If
  57. Next sws
  58. With Application
  59. .EnableEvents = True
  60. .ScreenUpdating = True
  61. End With
  62. MsgBox "Emails sent.", vbInformation
  63. End Sub
英文:

Use Variables to Distinguish Between Workbooks (Send Emails)

  • ThisWorkbook is a reference to the workbook containing this code which is PERSONAL.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 is Workbook(Workbooks.Count)), then you cannot safely refer to the initial ActiveWorkbook. Therefore use a variable to reference the initial ActiveWorkbook:

    1. Dim swb as Workbook: Set swb = ActiveWorkbook

    and a different one to reference the new workbooks: dwb.

A Quick Fix

<!-- language: lang-vb -->

  1. Sub MailEveryWorksheet()
  2. Dim MutualMails(): MutualMails = Array( _
  3. &quot;RSimmons@oldmutual.com&quot;, &quot;SMfeka@ oldmutual.com&quot;, _
  4. &quot;MBehari@ oldmutual.com&quot;, &quot;LFurlong@ oldmutual.com&quot;, _
  5. &quot;KPerumal2@ oldmutual.com&quot;, &quot;IDeVries@ oldmutual.com&quot;, _
  6. &quot;BEllis@ oldmutual.com&quot;, &quot;AMuller4@ oldmutual.com&quot;)
  7. Dim dTempFolderPath As String: dTempFolderPath = Environ$(&quot;temp&quot;) &amp; &quot;\&quot;
  8. Dim dFileExtension As String, dFileFormat As Long
  9. If Val(Application.Version) &lt; 12 Then
  10. dFileExtension = &quot;.xls&quot;: dFileFormat = -4143
  11. Else
  12. dFileExtension = &quot;.xlsm&quot;: dFileFormat = 52
  13. End If
  14. If ActiveWorkbook Is Nothing Then Exit Sub &#39; no visible workbooks open
  15. Dim swb As Workbook: Set swb = ActiveWorkbook
  16. Dim swbName As String: swbName = swb.Name
  17. Dim DotPosition As Long: DotPosition = InStrRev(swbName, &quot;.&quot;)
  18. Dim sBaseName As String
  19. If DotPosition = 0 Then
  20. sBaseName = swbName
  21. Else
  22. sBaseName = Left(swbName, DotPosition - 1)
  23. End If
  24. Dim olApp As Object: Set olApp = CreateObject(&quot;Outlook.Application&quot;)
  25. With Application
  26. .ScreenUpdating = False
  27. .EnableEvents = False
  28. End With
  29. Dim dwb As Workbook, sws As Worksheet
  30. Dim dFileName As String, dFilePath As String
  31. For Each sws In swb.Worksheets
  32. If sws.Range(&quot;S2&quot;).Value Like &quot;?*@?*.?*&quot; Then
  33. sws.Copy
  34. Set dwb = Workbooks(Workbooks.Count)
  35. dFileName = sws.Name &amp; &quot; - &quot; &amp; sBaseName &amp; &quot; &quot; &#39; why the space?
  36. dFilePath = dTempFolderPath &amp; dFileName &amp; dFileExtension
  37. With dwb
  38. .Sheets(1).Range(&quot;S2&quot;).Value = &quot;&quot;
  39. Application.DisplayAlerts = False &#39; overwrite, no confirmation
  40. .SaveAs dFilePath, dFileFormat
  41. Application.DisplayAlerts = True
  42. .Close SaveChanges:=False
  43. End With
  44. With olApp.CreateItem(0)
  45. &#39;specify the CC, BCC, Subject, Body below
  46. .To = sws.Range(&quot;S2&quot;).Value
  47. .CC = sws.Range(&quot;S4&quot;).Value &amp; &quot;;&quot; &amp; Join(MutualMails, &quot;;&quot;)
  48. .BCC = &quot;&quot;
  49. .Subject = swbName &amp; &quot; for &quot; &amp; sws.Range(&quot;S1&quot;)
  50. .Body = &quot;Dear &quot; &amp; sws.Range(&quot;S3&quot;)
  51. .Attachments.Add dFilePath
  52. .Display
  53. &#39;.Send
  54. End With
  55. Kill dFilePath
  56. End If
  57. Next sws
  58. With Application
  59. .EnableEvents = True
  60. .ScreenUpdating = True
  61. End With
  62. MsgBox &quot;Emails sent.&quot;, vbInformation
  63. End Sub

答案2

得分: 0

  1. Sub MailEveryWorksheet()
  2. Dim MutualMails(): MutualMails = Array( _
  3. "RSimmons@oldmutual.com", "SMfeka@oldmutual.com", _
  4. "MBehari@oldmutual.com", "LFurlong@oldmutual.com", _
  5. "KPerumal2@oldmutual.com", "IDeVries@oldmutual.com", _
  6. "BEllis@oldmutual.com", "AMuller4@oldmutual.com")
  7. Dim dTempFolderPath As String: dTempFolderPath = Environ$("temp") & "\"
  8. Dim dFileExtension As String, dFileFormat As Long
  9. If Val(Application.Version) < 12 Then
  10. dFileExtension = ".xls": dFileFormat = -4143
  11. Else
  12. dFileExtension = ".xlsm": dFileFormat = 52
  13. End If
  14. If ActiveWorkbook Is Nothing Then Exit Sub ' no visible workbooks open
  15. Dim swb As Workbook: Set swb = ActiveWorkbook
  16. Dim swbName As String: swbName = swb.Name
  17. Dim DotPosition As Long: DotPosition = InStrRev(swbName, ".")
  18. Dim sBaseName As String
  19. If DotPosition = 0 Then
  20. sBaseName = swbName
  21. Else
  22. sBaseName = Left(swbName, DotPosition-1)
  23. End If
  24. Dim olApp As Object: Set olApp = CreateObject("Outlook.Application")
  25. With Application
  26. .ScreenUpdating = False
  27. .EnableEvents = False
  28. End With
  29. Dim dwb As Workbook, sws As Worksheet
  30. Dim dFileName As String, dFilePath As String
  31. For Each sws In swb.Worksheets
  32. If sws.Range("S2").Value Like "?*@?*.?*" Then
  33. sws.Copy
  34. Set dwb = Workbooks(Workbooks.Count)
  35. dFileName = sws.Name & " - " & sBaseName & " " ' why the space?
  36. dFilePath = dTempFolderPath & dFileName & dFileExtension
  37. With dwb
  38. .Sheets(1).Range("S2").Value = ""
  39. Application.DisplayAlerts = False ' overwrite, no confirmation
  40. .SaveAs dFilePath, dFileFormat
  41. Application.DisplayAlerts = True
  42. .Close SaveChanges:=False
  43. End With
  44. With olApp.CreateItem(0)
  45. 'specify the CC, BCC, Subject, Body below
  46. .To = sws.Range("S2").Value
  47. .CC = sws.Range("S4").Value & ";" & Join(MutualMails, ";")
  48. .BCC = ""
  49. .Subject = swbName & " for " & sws.Range("S1")
  50. .Body = "Dear " & sws.Range("S3")
  51. .Attachments.Add dFilePath
  52. .Display
  53. '.Send
  54. End With
  55. Kill dFilePath
  56. End If
  57. Next sws
  58. With Application
  59. .EnableEvents = True
  60. .ScreenUpdating = True
  61. End With
  62. MsgBox "Emails sent.", vbInformation
  63. End Sub
英文:

`Sub MailEveryWorksheet()

  1. Dim MutualMails(): MutualMails = Array( _
  2. &quot;RSimmons@oldmutual.com&quot;, &quot;SMfeka@ oldmutual.com&quot;, _
  3. &quot;MBehari@ oldmutual.com&quot;, &quot;LFurlong@ oldmutual.com&quot;, _
  4. &quot;KPerumal2@ oldmutual.com&quot;, &quot;IDeVries@ oldmutual.com&quot;, _
  5. &quot;BEllis@ oldmutual.com&quot;, &quot;AMuller4@ oldmutual.com&quot;)
  6. Dim dTempFolderPath As String: dTempFolderPath = Environ$(&quot;temp&quot;) &amp; &quot;\&quot;
  7. Dim dFileExtension As String, dFileFormat As Long
  8. If Val(Application.Version) &lt; 12 Then
  9. dFileExtension = &quot;.xls&quot;: dFileFormat = -4143
  10. Else
  11. dFileExtension = &quot;.xlsm&quot;: dFileFormat = 52
  12. End If
  13. If ActiveWorkbook Is Nothing Then Exit Sub &#39; no visible workbooks open
  14. Dim swb As Workbook: Set swb = ActiveWorkbook
  15. Dim swbName As String: swbName = swb.Name
  16. Dim DotPosition As Long: DotPosition = InStrRev(swbName, &quot;.&quot;)
  17. Dim sBaseName As String
  18. If DotPosition = 0 Then
  19. sBaseName = swbName
  20. Else
  21. sBaseName = Left(swbName, DotPosition-1)
  22. End If
  23. Dim olApp As Object: Set olApp = CreateObject(&quot;Outlook.Application&quot;)
  24. With Application
  25. .ScreenUpdating = False
  26. .EnableEvents = False
  27. End With
  28. Dim dwb As Workbook, sws As Worksheet
  29. Dim dFileName As String, dFilePath As String
  30. For Each sws In swb.Worksheets
  31. If sws.Range(&quot;S2&quot;).Value Like &quot;?*@?*.?*&quot; Then
  32. sws.Copy
  33. Set dwb = Workbooks(Workbooks.Count)
  34. dFileName = sws.Name &amp; &quot; - &quot; &amp; sBaseName &amp; &quot; &quot; &#39; why the space?
  35. dFilePath = dTempFolderPath &amp; dFileName &amp; dFileExtension
  36. With dwb
  37. .Sheets(1).Range(&quot;S2&quot;).Value = &quot;&quot;
  38. Application.DisplayAlerts = False &#39; overwrite, no confirmation
  39. .SaveAs dFilePath, dFileFormat
  40. Application.DisplayAlerts = True
  41. .Close SaveChanges:=False
  42. End With
  43. With olApp.CreateItem(0)
  44. &#39;specify the CC, BCC, Subject, Body below
  45. .To = sws.Range(&quot;S2&quot;).Value
  46. .CC = sws.Range(&quot;S4&quot;).Value &amp; &quot;;&quot; &amp; Join(MutualMails, &quot;;&quot;)
  47. .BCC = &quot;&quot;
  48. .Subject = swbName &amp; &quot; for &quot; &amp; sws.Range(&quot;S1&quot;)
  49. .Body = &quot;Dear &quot; &amp; sws.Range(&quot;S3&quot;)
  50. .Attachments.Add dFilePath
  51. .Display
  52. &#39;.Send
  53. End With
  54. Kill dFilePath
  55. End If
  56. Next sws
  57. With Application
  58. .EnableEvents = True
  59. .ScreenUpdating = True
  60. End With
  61. MsgBox &quot;Emails sent.&quot;, vbInformation

End Sub`

huangapple
  • 本文由 发表于 2023年6月15日 17:41:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76481194.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定