从自定义按钮调用时,Excel宏无法正常工作

时间:2015-01-22 16:20:00

标签: excel vba custom-controls

我在Excel 2010中创建了一个宏,当从“宏”对话框或VB窗口调用时,它可以正常工作。那时候一切都很好。现在,我尝试了自定义色带,并使用自定义UI编辑器创建一个新选项卡,使用自定义图标调用我的宏。它并不好。

对宏的调用工作,宏似乎运行正常,扫描每张工作表,查找注释并对其进行操作,但是当它完成时,几乎没有任何注释被修改为本来应该。

这就是我的问题:当我运行宏"通常"时,它按计划工作,只有当我尝试从其自定义图标调用它时才会这样做#39;做他们应该做的事情(虽然点击时仍然看似)。

任何人都知道可能出现什么问题?

我不认为它是代码,正如我所说,它从Macros或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

1 个答案:

答案 0 :(得分:0)

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