VB的一个问题,一个是编辑器,另一个是多个地址

时间:2015-10-22 20:15:39

标签: excel-vba vba excel

我差不多完成了我的电子邮件VB,但有几个问题:

首先是我在一个应该打开VB编辑器的提示上有一个取消按钮,但目前它没有。我试过了

If vbCancel Then Application.VBE.MainWindow.Visible = True and If vbCancel Then Application.SendKeys "%{F11}", True

两者都不打开编辑器。

第二个问题是我似乎无法弄清楚如何将消息从C2或D2发送给多个用户?我认为它与粗体的偏移量有关,但我不确定。希望这几乎完成并且接近。谢谢你:)。

VB

Private Sub Workbook_Open()
Dim sR As String
Dim sFile As String
Sheets("Email").Activate
Range("A1").Select
If MsgBox("Are there any issues to report", vbYesNoCancel) = vbYes Then
            Range("D2").Value = "x"
            MsgBox ("Please select an issue and save"), vbExclamation
Else
     Range("C2").Value = "x"
If vbCancel Then Application.SendKeys "%{F11}", True


'define path
MyFileCopy = "L:\NGS\HLA LAB\total quality management\QC & QA\DOSE reports\DOSE reporting form Attachment.xlsx"

' create connection, check condition, send email
Set OutApp = CreateObject("Outlook.Application")
Set WS = ThisWorkbook.Sheets("Email")
With WS
    Set Rng = .Range("A2", .Range("A" & .Rows.Count).End(xlUp))
End With

 For Each c In Rng

Msg = "For " & WS.Cells(2, 2) & Chr(14) & Chr(14)
For i = 3 To 4
    If LCase(WS.Cells(c.Row, i)) = "x" Then
        Msg = Msg & "   -" & WS.Cells(1, i) & Chr(14)
    End If
Next

        Set OutMail = OutApp.CreateItem(0)

        With OutMail
            **.To = c.Offset(, 0)**
            .CC = ""
            .BCC = ""
            .Subject = "Daily Operational Safety Briefing"
            .Body = Msg
            If Range("D2").Value & Chr(14) = "x" Then .Attachments.Add MyFileCopy, 1
            .Send
        End With

Next c

'confirm message sent, clear sheet, and delete copy
MsgBox "The data has been emailed sucessfully.", vbInformation
Range("C2:D2").ClearContents
Kill MyFileCopy

Set OutMail = Nothing
Set OutApp = Nothing

' Exit and do not save
Application.Quit
ThisWorkbook.Close SaveChanges:=False

End If
End Sub

1 个答案:

答案 0 :(得分:0)

我有一些代码可以显示双窗口状态 - 屏幕的一半 Excel ,另一半 VBA

Sub MAIN()
   Call dual
End Sub

Sub dual()
    Dim h As Long
    With Application
        .WindowState = xlMaximized
        x = Application.ActiveWindow.Width \ 2
        h = Application.ActiveWindow.Height
        .WindowState = xlNormal
        .Left = 545
        .Height = 550
        .Width = x * 0.9
        .Top = 0
        With .VBE.MainWindow
            .Width = 1.4 * x
            .Left = 0
            .Top = 0
            .Height = 740
            .WindowState = vbext_ws_Normal
        End With
    End With
End Sub

"魔术数字" 545,550,740 与我年迈的戴尔笔记本电脑的外形有关。

也许代码可以根据您的需要进行调整。