VBA-Outlook 应用杂记

1)在OutLook中创建应用对象为:dim otlApplication as OutLook.application

                           set otlApplication=CreateObject("Outlook.appliction")
2)在OutLook中创建命名空间: dim nameSpace1 as namespace
                          set nameSpace1=otlApplication.getNamespace("MAPI")'这里的MAPI是指支持 的唯一数据源是 MAPI,允许访问存储在用户的邮件存储区中的所有Outlook 数据。

3)接着创建Folder文件: dim folder1 as folder    dim folder2 as folder
                     set folder1= nameSpace1.getDefaultFolder(olFolderInbox) '获取默认的根文件夹,inbox收件箱

 


4)可以根据folder1.folders.count可以得到所有的子文件夹的个数,然后利用循环进行遍历,去的各个文件,如下遍历求出各个子文件夹的名字:
            for k=0 to folder1.folders.count
                set folder2 = folder1.folders(k)
                msgbox folder2.name'直接使用弹出对话框来显示
                debug.print folder2.name'使用本地窗口进行输出
             next
5)在OutLook中创建邮件:可以直接为邮件赋值,也可以通过Application对象进行创建:
    第一种:dim mailItem1 as MailItem

             set mailItem1=folder1.items(n)'n必须小于或等于folder1.items.count
    第二种(推荐):set mailItem1=application.createitem("olMailItem")
6)在OutLook中的Explorers集合对象,代表打开的主窗体对象
        Msgbox Application.activeExplorer.Selction.count'这里的count是显示你选择的主窗体
7)在OutLook中的Inspectors
         Msgbox Application.inspectors.count'这里的count是显示你打开的子窗体的个数
8)将EXCEL内数据粘贴到邮件正本中

  

 1 Public Function RangetoHTML(rng As Range)
 2     Dim fso As Object
 3     Dim ts As Object
 4     Dim TempFile As String
 5     Dim TempWB As Workbook
 6     '创建临时文件的路径
 7     TempFile = Environ$("temp") & "/" & Format(Now, "dd-mm-yy h-mm-ss") & ".htm" 
 8 
 9     rng.Copy
10     '将rng的数据粘贴到临时文件路径下的EXCEL
11     Set TempWB = Workbooks.Add(1)
12     With TempWB.Sheets(1)
13         .Cells(1).PasteSpecial Paste:=8
14         .Cells(1).PasteSpecial xlPasteValues, , False, False
15         .Cells(1).PasteSpecial xlPasteFormats, , False, False
16         .Cells(1).Select
17         Application.CutCopyMode = False
18         On Error Resume Next
19         .DrawingObjects.Visible = True
20         .DrawingObjects.Delete
21         On Error GoTo 0
22     End With
23     '将临时文件中的数据转换成HTML格式
24     With TempWB.PublishObjects.Add(SourceType:=xlSourceRange, _
25          Filename:=TempFile, _
26          Sheet:=TempWB.Sheets(1).Name, _
27          Source:=TempWB.Sheets(1).UsedRange.Address, _
28          HtmlType:=xlHtmlStatic)
29 
30         .Publish (True)
31 
32     End With
33     '调用FSO 读取转换成HTML格式的内容,贴到邮件正文中
34     Set fso = CreateObject("Scripting.FileSystemObject")
35     Set ts = fso.GetFile(TempFile).OpenAsTextStream(1, -2)
36     RangetoHTML = ts.ReadAll
37     ts.Close
38     '将正本居中替换成靠左显示
39     RangetoHTML = Replace(RangetoHTML, "align=center x:publishsource=", "align=left x:publishsource=")
40 
41     TempWB.Close SaveChanges:=False
42 Kill TempFile
43 Set ts = Nothing
44     Set fso = Nothing
45     Set TempWB = Nothing
46 End Function
View Code
posted @ 2020-01-17 17:14  零哭谷  阅读(990)  评论(0编辑  收藏  举报