【问题标题】:Excel Macro not working properly when called from custom button从自定义按钮调用时 Excel 宏无法正常工作
【发布时间】:2015-01-22 16:19:59
【问题描述】:

我在 Excel 2010 中创建了一个宏,当从“宏”对话框或 VB 窗口调用时效果很好。那时一切都很好。现在,我尝试了自定义功能区,并使用自定义 UI 编辑器创建了一个新选项卡,使用自定义图标来调用我的宏。而且还不行。

对宏的调用有效,宏似乎运行正常,按应有的方式扫描每张工作表,寻找 cmets 并对其进行操作,但是当它完成时,几乎没有一个 cmets 按应有的方式进行修改。

这就是我的问题:当我“正常”运行宏时,它按计划工作,只有当我尝试从其自定义图标调用它时,它才没有做它应该做的事情(虽然看起来仍然点击时)。

有人知道可能出了什么问题吗?

我不认为这是代码,正如我所说,从宏或 VB 窗口调用时它工作正常


编辑:正如我所说,我不认为代码是问题,因为它执行没有错误(它只是没有做它应该做的事情),但根据要求,我在这里发布:

Sub ImportCommentsFromWord(control As IRibbonControl)

Dim xComment As Comment
Dim xSheet As Worksheet
Dim wApp As Object

'Opens Word if not already open
On Error Resume Next
Set wApp = GetObject(, "Word.Application")
If Err.Number <> 0 Then
Err.Clear
Set wApp = CreateObject("Word.Application")
End If

wApp.Visible = False

For Each xSheet In ActiveWorkbook.Worksheets

'Activates each sheet one after another
xSheet.Activate
sName = xSheet.Name
expName = Application.ActiveWorkbook.Path + "\" + sName + ".docx"

'Checks if there are comments in active sheet
For Each xComment In xSheet.Comments
    CommsInSheet = 1
Next

If CommsInSheet = 1 Then

    'Opens the translated document to import comments into the sheet
    wApp.Documents.Open (expName)
    wApp.Selection.ClearFormatting
    wApp.Selection.Find.MatchWildcards = False
    wApp.Selection.WholeStory
    wApp.Selection.MoveLeft
    FileEnd = 0
    'Imports comments until end of file is reached
    While FileEnd = 0
        wApp.Selection.ExtendMode = True
        wApp.Selection.MoveRight
        With wApp.Selection.Find
            .Text = "^l"
        End With
        wApp.Selection.Find.Execute
        DestCell = Mid(wApp.Selection.Text, 2, Len(wApp.Selection.Text) - 2)
        wApp.Selection.ExtendMode = False
        wApp.Selection.MoveRight
        wApp.Selection.ExtendMode = True
        With wApp.Selection.Find
            .Text = "^l"
        End With
        wApp.Selection.Find.Execute
        wApp.Selection.ExtendMode = False
        DestComm = Left(wApp.Selection.Text, Len(wApp.Selection.Text) - 1)
        wApp.Selection.MoveRight
        wApp.Selection.MoveLeft
        wApp.Documents.Add DocumentType:=0
        wApp.Selection.Text = DestComm
        With wApp.Selection.Find
            .Text = "^p"
            .Replacement.Text = Chr(10)
        End With
        wApp.Selection.Find.Execute Replace:=wdReplaceAll
        wApp.Selection.WholeStory
        DestComm = Left(wApp.Selection.Text, Len(wApp.Selection.Text) - 1)
        wApp.ActiveDocument.Close savechanges:=False
        If Right(DestComm, 11) = "END_OF_FILE" Then
            DestComm = Left(DestComm, Len(DestComm) - 11)
            FileEnd = 1
        End If
        xSheet.Range(DestCell).Comment.Text Text:=DestComm
    Wend

    'Closes the Word document
    wApp.ActiveDocument.Close savechanges:=False

End If

CommsInSheet = 0

Next

wApp.Visible = True
Set wApp = Nothing

End Sub

【问题讨论】:

  • 戴上眼罩很难提供帮助。发布代码的一些相关部分,注意代码失败的代码行。
  • 已发布,但我仍然认为问题在于我忘记某处的某些设置

标签: excel vba custom-controls


【解决方案1】:

没关系,我自己找到了解决方案:问题出在自定义功能区的 xml 代码中,它调用了错误的宏,所以它当然没有按预期工作......

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多