【问题标题】:export table to outlook from excel that is blackberry friendly将表格从黑莓友好的 excel 导出到 Outlook
【发布时间】:2012-01-25 23:25:13
【问题描述】:

所以我从互联网上的不同地方收集了一些代码来将表格导出到 Outlook 并自动发送出去,我刚刚注意到这封电子邮件并不是真正的黑莓友好(主要是因为电子邮件正文被发送为HTML 而不是“富文本”,请记住该表也需要为富格式)。我缺少一行代码,它会告诉 Outlook 以“富文本”的形式发送电子邮件,但我不知道它是什么。谁能帮我?请参阅下面附上的完整代码(提前对代码的长度表示歉意)。

谢谢 P

Sub Mail_Sheet_Outlook_Body()
    调暗为范围
    将 OutApp 调暗为对象
    将 OutMail 作为对象变暗
    有应用程序
        .EnableEvents = 假
        .ScreenUpdating = 假
    结束于

    设置 rng = 无
    设置 rng = Selection.SpecialCells(xlCellTypeVisible)

    设置 OutApp = CreateObject("Outlook.Application")
    设置 OutMail = OutApp.CreateItem(0)

    出错时继续下一步
    使用 OutMail
        .To = "email@email.com"
        .CC = ""
        .BCC = ""
        .Subject = "在此处输入主题文本"
        .HTMLBody = RangetoHTML(rng)
       '.Attachments.Add ("c:\temp\" & ActiveSheet.Range("DateSerial").Value & ".pdf")
        。展示
    结束于
    错误转到 0

    有应用程序
        .EnableEvents = 真
        .ScreenUpdating = True
    结束于

    设置 OutMail = 无
    设置 OutApp = 无
结束子

函数 RangetoHTML(rng As Range)
' 适用于 Excel 2000、Excel 2002、Excel 2003、Excel 2007、Excel 2010、Outlook 2000、Outlook 2002、Outlook 2003、Outlook 2007 和 Outlook 2010。
    将 fso 调暗为对象
    调暗为对象
    将 TempFile 调暗为字符串
    将 TempWB 调暗为工作簿

    TempFile = Environ$("temp") & "/" & Format(现在,"dd-mm-yy h-mm-ss") & ".htm"

    ' 复制范围并创建一个工作簿来接收数据。
    rng.复制
    设置 TempWB = Workbooks.Add(1)
    使用 TempWB.Sheets(1)
        .Cells(1).PasteSpecial Paste:=8
        .Cells(1).PasteSpecial xlPasteValues, , False, False
        .Cells(1).PasteSpecial xlPasteFormats, , False, False
        .Cells(1).选择
        Application.CutCopyMode = False
        出错时继续下一步
        .DrawingObjects.Visible = True
        .DrawingObjects.Delete
        错误转到 0
    结束于

    ' 将工作表发布到 .htm 文件。
    使用 TempWB.PublishObjects.Add(_
         SourceType:=xlSourceRange, _
         文件名:=临时文件,_
         工作表:=TempWB.Sheets(1).名称,_
         来源:=TempWB.Sheets(1).UsedRange.Address,_
         HtmlType:=xlHtmlStatic)
        .发布(真)
    结束于

    ' 将 .htm 文件中的所有数据读入 RangetoHTML 子例程。
    设置 fso = CreateObject("Scripting.FileSystemObject")
    设置 ts = fso.GetFile(TempFile).OpenAsTextStream(1, -2)
    RangetoHTML = ts.ReadAll
    ts.关闭
    RangetoHTML = 替换(RangetoHTML, "align=center x:publishsource=", "align=left x:publishsource=")

    ' 关闭 TempWB。
    TempWB.Close savechanges:=False

    ' 删除.htm 文件。
    杀死临时文件

    设置 ts = 无
    设置 fso = 无
    设置 TempWB = 无
结束功能

【问题讨论】:

    标签: excel vba outlook


    【解决方案1】:

    回答的背景

    这个问题的目的是能够在一封电子邮件中发送一个 Excel 表格,该表格可以在 Blackberry 上阅读。在问题的原因被确定为 Excel 的 PublishObjects 之前,有许多红鲱鱼。问题可能是大量不必要的 CSS 格式或单元格和字体的大小(以磅为单位),但无论是什么问题,Blackberry 的显示引擎都无法正确呈现 HTML。

    我创建了一段相当简单的 VBA 代码来从一个范围创建一个 HTML 表格。它将值、粗体、斜体、字体颜色、背景颜色和列宽从 Excel 复制到 HTML 表中。虽然第一个版本没有满足 macutan 的全部要求,但它证明了该方法是可行的:具有最少格式的 HTML/CSS 表格可以在 Blackberry 上正确且美观地显示。

    当我进一步改进以满足 macutan 的要求时,我发现其他智能手机也存在同样的问题,并且普遍需要这样的例程。我继续开发例程,目的是最终版本将所有 Excel 格式复制到 HTML 表中。

    代码加上指令很快就超过了 Stack Overflow 的 30,000 个字符的限制。我通过电子邮件将新版本传递给了 macutan。代码模块的文档如下。如果您查看我的个人资料,就会有一个电子邮件地址。我会应要求转发我的代码副本。

      ' Converts a rectangular range within an Excel worksheet to an Html document.
    
      ' RangeToHtml is the only routine in this module designed to be called from
      ' outside the module.  A typical call might be:
      '     Call RangeToHtml(Worksheets("Data"), 1, 1, RowBottom, ColRight, _
      '                                     BorderStyle=Separate, CellPadding=.25)
    
      ' The parameters of RangeToHtml are:
      '  * Wsht         A worksheet within an open workbook
      '  * RowTop       \
      '  * ColumnLeft   | Together these specify a rectangular range within
      '  * RowBottom    | Wsht.  This is the range to be converted to Html.
      '  * ColumnRight  /
      '  * Options      Zero or more strings of the form OptionName=OptionValue
      '                 specifying which, if any, of the available high-level,
      '                 formatting options are required.
    
      ' Individual options are checked.  "OptionName" must be the name of a documented
      ' option and "OptionValue" must be a permitted value for that option. No space is
      ' permitted before or after the equals sign. OptionNames and OptionValues are
      ' case insensitive; that is "BorderStyle=Collapse" and "borderstyle=collapse" are
      ' both acceptable and have the same meaning. However, options are not tested for
      ' duplicates. You can, for example, specify:
      '     "BorderStyle=Separate", "BorderStyle=Collapse".
      ' In this example, "BorderStyle=Separate" will overwrite the default collapse style
      ' and then "BorderStyle=Collapse" will overwrite "BorderStyle=Separate"
    
      ' The available options are:
      '   * BorderStyle=Collapse
      '   * BorderStyle=Separate
      '       With Html/Css, the cells of a table can touch so there appears to be a single
      '       border between adjacent cells or they can be separated by a small gap so it is
      '       obvious that each cell has its own border. If no BorderStyle is specified,
      '       "BorderStyle=Collapse" is the default which means cell borders touch.
      '   * CellPad=.nn
      '       If this option is omitted or if "CellPad=0" is specified, there is a minimal gap
      '       between the cell border and its contents. If the option is, for example,
      '       "CellPad=.25" then will be a gap of .25 ems around the cell contents. An "em" is
      '       a measure of length equal to the height of the cell's font.
      '   * TableWidth=nnn
      '       If this option is omitted or if "TableWidth=100" is specified, the table occupies
      '       the entire available display width.  If the option is, for example, "TableWidth=50",
      '       the table will occupy 50% of the available display width. "nnn" can be greater than
      '       100 if you wish the user to scroll to see the entire table.
    
      ' The table below lists the formats handled at cell and or in-cell level. The default value
      ' column is explained below.
      '   CELL-LEVEL            IN-CELL           DEFAULT VALUE
      '   bold                  bold              false
      '   italic                italic            false
      '   strikethrough         strikethrough     false
      '   underline single      underline single  no underline
      '   underline double      underline double  no underline
      '   underline accounting                    no underline
      '   font colour           font colour       black
      '   background colour                       white
      '   horizontal alignment                    left for string; right for numbers and dates
      '   font size             font size         11 or as set as the Excel application level
      '   font name             font name         Calibri or as set as the Excel application level
      '   vertical alignment                      bottom
    
      ' There is no Html/Css equivalent to double or accouting underlining so both are
      ' converted to single underline.
    
      ' Each format is checked for every cell within the range. Formatting information is only
      ' output to the Html/Css if a format has a non-default value so the output is as clean and
      ' crisp as possible. You need to be careful about font name and size. Many change the name
      ' and size for a worksheet but the routine has no access to the worksheet's standard name
      ' and size; it is the Excel standard that determines the default.
    
      ' Html/Css does not handle wrap text at the cell level.  If no Excel cell has wrap text set
      ' then the routine does not output any column size information and columns widths are
      ' entirely determined by the receiver's browser.  If any cell has wrap text set then column
      ' widths are set by percentage so their relationship depends on the Excel column widths.
    
      ' Excel borders are not checked so any borders within the worksheet will not be converted.
      ' Instead every cell is given a thin, grey border so ther Html looks like a default
      ' Excel worksheet.
    
      ' If the cell value is numeric, the number format is checked for colour names. If
      ' appropriate, the colour specified in the number format will overwrite the cell's font
      ' colour.  See UpdateHtmlFontColourForNumericFormat for more information.
    
      ' Excel's default vertical alignment is "bottom" but Html/Css's is "center". If a cell
      ' is a single line, the difference will not be apparent but the Html/Css will be bigger
      ' than necessary because of "vertical-align:bottom" for every cell. It may be worth
      ' setting every cell's vertical alignment to "center".
    
      ' Merged cells are converted to the Html equivalent.
    
      ' Most of the work is performed by the macro HtmlStyleTable. This routine returns two
      ' strings, Style and Table, which RangeToHtml wraps in an Html envelope to create the
      ' document returned as a string to the caller.  Style and Table will look like:
    
      ' Style:
      '      table {border-collapse:collapse;}
      '      td {border-style:solid; border-width:1px; border-color:#BFBFBF;}
      '      td.backclr-0000FF {background-color:#0000FF;}
      '      td.backclr-D7EAF4 {background-color:#D7EAF4;}
      '         :   :   :
      '      span.bold {font-weight:bold;}
      '      span.fontclr-0070C0 {color:#0070C0;}
      '    The first two entries in Style are fixed . All other entries depend on
      '    the formats that appear in the worksheets. The td.xxx entries are used for
      '    cell-level formats. The span.xxx entries are used for in-cell formats.
      '    "In-cell formats" refers to part of a cell value being, for example, bold
      '    and part being non-bold.
      ' Table:
      '     <table class="bord-collapse">
      '       <tr>
      '         <td rowspan=2 style="width:29.41%" class="back-D7F4EA bold hAlign-center fontsize-095 fontname-Arial">Product</td>
      '         <td rowspan=2 style="width:11.76%" class="back-D7F4EA bold hAlign-center fontsize-095 fontname-Arial">Position</td>
    
      '   Mar12  I coded something similar in response to a Stack Overflow question. The main
      '          difference is that with the earlier version the formatting styles (for example:
      '          "background-color:#D7EAF4;") were all within the individual TD elements' STYLE
      '          attribute. I believe having all the styles in a STYLE element and references
      '          them by name in the TD elements gives a cleaner document.
      '   ?????  Having answered the Stack Overflow question to the satisfaction of the OP, I
      '          continued developing the routine. It quickly became clear that a full
      '          conversion was not practical in VBA so I converted my code to VB before
      '          continuing. I have quite an attractive VB version but it is slower than I would
      '          like. There are many complaints on the internet about the speed of access to
      '          Excel from VB and some bizarre solutions. I discovered a different approach
      '          which meant a program could obtain a lot of information about a worksheet in a
      '          single call to the InterOp. That information was provided as a string for which
      '          the documentation was poor and often wrong. However, I did manage to decode the
      '          string. Sometime, I must bring all my separate routines together in a single
      '          program to give a fast, complete conversion.
      '   Jun16  A request for a copy of my original code meant I searched my archives for that
      '          code. I did not find that code but I realised I could write a better routine.
      '          All routines in this module were coded and debugged in stages. The first
      '          version handled three cell-level formats. The final version handled more
      '          formats at both cell and in-cell level.
      '   Aug16  Added HtmlStyleTable options.
    

    【讨论】:

      【解决方案2】:

      在 With 块中,将 BodyFormat 属性设置为 olFormatRichText

      With OutMail
      .BodyFormat = olFormatRichText
      ' rest of your code
      

      让我们知道这是否有效。

      编辑:

      设置HTMLBody 属性会将电子邮件转换为HTML。如果您将BodyFormat 属性设置为olFormatRichText,那么您将需要使用Body 属性,这将消除使用HTML 的可能性。

      所以我看不出有什么方法可以同时使用 HTML 表格设置电子邮件以富文本格式发送。

      【讨论】:

      • 没有变化,仍然作为 HTML 发送(在窗口标题上显示 HTML)。
      • 好的,设置.HTMLBody 正在改变正文格式。并且尝试更改正文格式很可能会破坏所有存在的 HTML。所以我认为你需要选择一种格式并坚持下去——你不能设置 HTMLBody 并且仍然有富文本电子邮件。
      • 当然,我明白了。由于某种原因,HTML 格式的电子邮件没有出现在黑莓中……我正在尝试寻找解决方案,但在网上找不到任何东西……
      • 谢谢JP,我同意你的回答,我还在努力寻找解决方案,谁能指导我。正如你所说,我将 .HTMLBody 更改为 .Body ,现在我在电子邮件中获得 HTML 代码(富文本格式)......有没有办法将表格粘贴进去?我不知道为什么我无法找到看起来如此简单的方法。
      • 我似乎记得几年前 Lotus Notes 的 RTF 是一种秘密的专有格式,与 Microsoft 的发布格式无关。如果我理解正确,黑莓使用 Lotus Notes 的 RTF。如果你在 JP 的帮助下成功地在 Microsoft 的 RTF 中输出了一个正文,它可能无济于事。黑莓接受 HTML。也许您的问题应该是关于创建 Blackberry 可接受的 HTML 消息。
      猜你喜欢
      • 2018-12-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-27
      • 2023-03-16
      • 2019-09-24
      • 2014-10-05
      相关资源
      最近更新 更多