在发送电子邮件之前,在excel的范围内搜索给定名称

时间:2014-04-23 18:19:25

标签: vba

我在outlook中创建一个宏来发送一个包含一些特定信息的eamil。但是,只有excel表单列表中的某些人才能发送该电子邮件。当他们在该宏上点击“发送”时,它需要打开Excel工作表,并在列表中列出该人员时进行修改。如果他不是,它应该只是给他一个错误“你没有资格发送此消息”。

我可以使用下面的代码打开excel文件。但我不知道如何进行检查(名称列在Sheet1上的C1:C100),以确定发送人员列在那里。

以下是我的代码:

[Dim strFldr As String
Dim OutMail As Object
Dim xlApp As Object
strFldr = "C:\\users-d\gxg063\Gift\test\"
Set xlApp = CreateObject("Excel.Application")
xlApp.Application.Visible = True
xlApp.Workbooks.Open strFldr & "\RegionalAuthority.xlsx"] 

1 个答案:

答案 0 :(得分:0)

Let me know how this works out - you'll need a reference to Excel in your Outloook VBE

Sub TestSub()
    Dim strFldr As String
    Dim OutMail As Object
    Dim xlApp As Excel.Application
    Dim xlWb As Workbook
    Dim xlWs As Worksheet
    Dim r As Range
    Dim User As String
    Dim c As Range
    strFldr = "C:\\users-d\gxg063\Gift\test\"
    Set xlApp = New Excel.Application
    Set xlWb = xlApp.Workbooks.Open(strFldr & "\RegionalAuthority.xlsx")
    Set xlWs = xlWb.Worksheets("Sheet1")
    Set r = xlWs.Range("C1:C100")
    User = (Environ$("Username"))

    For Each c In r
        If c = User Then
            'Call your Send Macro here
            Exit For
        End If
    Next c

    xlApp.Visible = True
    Set xlApp = Nothing
    Set xlWb = Nothing
    Set xlWs = Nothing
    End Sub