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

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

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()

&#39;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$(&quot;temp&quot;) &amp; &quot;\&quot;
  If Val(Application.Version) &lt; 12 Then
    xFileExt = &quot;.xls&quot;: xFileFormatNum = -4143
  Else
    xFileExt = &quot;.xlsm&quot;: xFileFormatNum = 52
  End If

  Set xOlApp = CreateObject(&quot;Outlook.Application&quot;)
  For Each xWs In ThisWorkbook.Worksheets
    If xWs.Range(&quot;S2&quot;).Value Like &quot;?*@?*.?*&quot; Then
      xWs.Copy
      Set xWb = ActiveWorkbook
      xFileName = xWs.Name &amp; &quot; - &quot; _
                   &amp; VBA.Left(ThisWorkbook.Name, VBA.InStr(ThisWorkbook.Name, &quot;.&quot;) - 1) &amp; &quot; &quot;
      Set xMailObj = xOlApp.CreateItem(0)
      xWb.Sheets.Item(1).Range(&quot;S2&quot;).Value = &quot;&quot;
      With xWb
        .SaveAs xTempFilePath &amp; xFileName &amp; xFileExt, FileFormat:=xFileFormatNum
        With xMailObj



        &#39;specify the CC, BCC, Subject, Body below
            .To = xWs.Range(&quot;S2&quot;).Value
            .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;
            .BCC = &quot;&quot;
            .Subject = ThisWorkbook.Name &amp; &quot; for &quot; &amp; xWs.Range(&quot;S1&quot;)
            .Body = &quot;Dear &quot; &amp; xWs.Range(&quot;S3&quot;)
            .Attachments.Add xWb.FullName


            .Display
        End With
        .Close SaveChanges:=False
      End With
      Set xMailObj = Nothing
      Kill xTempFilePath &amp; xFileName &amp; 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 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:

    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( _
&quot;RSimmons@oldmutual.com&quot;, &quot;SMfeka@ oldmutual.com&quot;, _
&quot;MBehari@ oldmutual.com&quot;, &quot;LFurlong@ oldmutual.com&quot;, _
&quot;KPerumal2@ oldmutual.com&quot;, &quot;IDeVries@ oldmutual.com&quot;, _
&quot;BEllis@ oldmutual.com&quot;, &quot;AMuller4@ oldmutual.com&quot;)
Dim dTempFolderPath As String: dTempFolderPath = Environ$(&quot;temp&quot;) &amp; &quot;\&quot;
Dim dFileExtension As String, dFileFormat As Long
If Val(Application.Version) &lt; 12 Then
dFileExtension = &quot;.xls&quot;: dFileFormat = -4143
Else
dFileExtension = &quot;.xlsm&quot;: dFileFormat = 52
End If
If ActiveWorkbook Is Nothing Then Exit Sub &#39; 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, &quot;.&quot;)
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(&quot;Outlook.Application&quot;)
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(&quot;S2&quot;).Value Like &quot;?*@?*.?*&quot; Then
sws.Copy
Set dwb = Workbooks(Workbooks.Count)
dFileName = sws.Name &amp; &quot; - &quot; &amp; sBaseName &amp; &quot; &quot; &#39; why the space?
dFilePath = dTempFolderPath &amp; dFileName &amp; dFileExtension
With dwb
.Sheets(1).Range(&quot;S2&quot;).Value = &quot;&quot;
Application.DisplayAlerts = False &#39; overwrite, no confirmation
.SaveAs dFilePath, dFileFormat
Application.DisplayAlerts = True
.Close SaveChanges:=False
End With
With olApp.CreateItem(0)
&#39;specify the CC, BCC, Subject, Body below
.To = sws.Range(&quot;S2&quot;).Value
.CC = sws.Range(&quot;S4&quot;).Value &amp; &quot;;&quot; &amp; Join(MutualMails, &quot;;&quot;)
.BCC = &quot;&quot;
.Subject = swbName &amp; &quot; for &quot; &amp; sws.Range(&quot;S1&quot;)
.Body = &quot;Dear &quot; &amp; sws.Range(&quot;S3&quot;)
.Attachments.Add dFilePath
.Display
&#39;.Send
End With
Kill dFilePath
End If
Next sws
With Application
.EnableEvents = True
.ScreenUpdating = True
End With
MsgBox &quot;Emails sent.&quot;, 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( _
&quot;RSimmons@oldmutual.com&quot;, &quot;SMfeka@ oldmutual.com&quot;, _
&quot;MBehari@ oldmutual.com&quot;, &quot;LFurlong@ oldmutual.com&quot;, _
&quot;KPerumal2@ oldmutual.com&quot;, &quot;IDeVries@ oldmutual.com&quot;, _
&quot;BEllis@ oldmutual.com&quot;, &quot;AMuller4@ oldmutual.com&quot;)
Dim dTempFolderPath As String: dTempFolderPath = Environ$(&quot;temp&quot;) &amp; &quot;\&quot;
Dim dFileExtension As String, dFileFormat As Long
If Val(Application.Version) &lt; 12 Then
dFileExtension = &quot;.xls&quot;: dFileFormat = -4143
Else
dFileExtension = &quot;.xlsm&quot;: dFileFormat = 52
End If
If ActiveWorkbook Is Nothing Then Exit Sub &#39; 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, &quot;.&quot;)
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(&quot;Outlook.Application&quot;)
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(&quot;S2&quot;).Value Like &quot;?*@?*.?*&quot; Then
sws.Copy
Set dwb = Workbooks(Workbooks.Count)
dFileName = sws.Name &amp; &quot; - &quot; &amp; sBaseName &amp; &quot; &quot; &#39; why the space?
dFilePath = dTempFolderPath &amp; dFileName &amp; dFileExtension
With dwb
.Sheets(1).Range(&quot;S2&quot;).Value = &quot;&quot;
Application.DisplayAlerts = False &#39; overwrite, no confirmation
.SaveAs dFilePath, dFileFormat
Application.DisplayAlerts = True
.Close SaveChanges:=False
End With
With olApp.CreateItem(0)
&#39;specify the CC, BCC, Subject, Body below
.To = sws.Range(&quot;S2&quot;).Value
.CC = sws.Range(&quot;S4&quot;).Value &amp; &quot;;&quot; &amp; Join(MutualMails, &quot;;&quot;)
.BCC = &quot;&quot;
.Subject = swbName &amp; &quot; for &quot; &amp; sws.Range(&quot;S1&quot;)
.Body = &quot;Dear &quot; &amp; sws.Range(&quot;S3&quot;)
.Attachments.Add dFilePath
.Display
&#39;.Send
End With
Kill dFilePath
End If
Next sws
With Application
.EnableEvents = True
.ScreenUpdating = True
End With
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:

确定