子例程中的功能未完全执行

问题描述 投票:0回答:1

我正在尝试使用html发布过程(在函数内)附加电子邮件正文(Excel范围)。这工作正常,只要我不尝试从html文件的创建格式化底层工作表。

“###”标记代码中的重要行。

在此代码片段中,函数从底层子函数启动(RangetoHTNL(rng))。

    With OutMail
    .To = rec
    .cc = cc
    .BCC = ""
    .Subject = "Convertibles - Execution " & cover.Range("D4").Value 
    .HTMLBody = StrBody & RangetoHTML(rng) '#### Here the function get launched #####
    .Attachments.Add (filename)
    .Display   'or use .Send
End With

现在奇怪的是,该函数正常工作,直到我想要更改列宽。在此行之后,算法“跳出”函数并执行基础子,而不首先完成整个函数。

在下面找到该函数的代码片段,我在Excel Sheet中进行一些格式化,然后应该以HTML格式发布。

With TempWB.Sheets(1)
    .Cells(1).PasteSpecial Paste:=8
    .Cells(1).PasteSpecial xlPasteValues, , False, False
    .Cells(1).PasteSpecial xlPasteFormats, , False, False

    row = .Range("A" & Rows.Count).End(xlUp).row
    lastcolumn = .Cells(6, Columns.Count).End(xlToLeft).Column
    default = .Range(.Cells(6, 1), .Cells(6, lastcolumn)).Width '-> Default width of whole table Range("A:?")
    ReDim myArray(row)
    For i = 1 To row
    myCell = .Cells(i, 1).text
    mysize = getLabelPixel(myCell)
    myArray(x) = mysize
    Next i

    Max = WorksheetFunction.Max(myArray)
    max_width = Max / con
    default_width = default / con

    If max_width > default_width Then
    prop = max_width / lastcolumn

    '####### until here the function is executed. Then it jumps out #######

    .Columns("1:" & lastcolumn).Width = prop
    .Columns("2:" & lastcolumn).Columns.AutoFit
    '###### without these two lines the function is executed properly #####

    End If
    .Cells(1).Select
    Application.CutCopyMode = False
On Error Resume Next
    .DrawingObjects.Visible = True
    .DrawingObjects.Delete
On Error GoTo 0
End With

有人曾经经历过这样的事吗?我已经尝试过其他版本的编码(例如,不使用“With” - Statement方法) - 没有任何帮助。

excel vba function outlook interrupt
1个回答
2
投票

我的猜测是你在调用函数的子例程中放置了on error resume next,然后在调整列大小时发生错误(如果其中一个变量无效,可能会溢出)。

确保在函数调用之前放置on error goto 0,然后调试函数并检查变量是否有效。

/ edit:既然我们遇到了错误,那就让我们来看看吧。我知道这里发生错误:

.Columns("1:" & lastcolumn).Width = prop

看起来不对,试试这个(确保lastcolumnprop有效):

.Cells(1, lastcolumn).ColumnWidth = prop
© www.soinside.com 2019 - 2024. All rights reserved.