VBA Application.Run错误(此工作簿中的宏可能不可用或可能禁用了所有宏)

时间:2020-01-28 05:13:58

标签: excel vba

新问题:

我的原始问题已得到解答,解决方案非常好。但是,这引起了另一个问题。这是处理流程:

macroFile1 Sub1(调用macroFile2 Sub3并传递参数)-> macroFile2 Sub3(进行一些处理,并期望将多个值返回给macroFile1Sub1)

macroFile1 Sub1能够调用macroFile2 Sub3,但是macroFile2 Sub3无法返回值。我尝试了以下方法:

方法1:

'(macroFile1)
returnCodes = Application.Run("'" & macroFileName & "'!pasteCapture", _
thisWorkbookName, selectedFile, cmntText, workingSheet, cmntRowNum, cmntColNum, imgRowNum, imgColNum, size, clrTyp)

执行时出错:编译错误:无法分配给数组

方法2:

'(macroFile1)          
Sub1 
'(This sub is going to call macroFile2 Sub3, and macroFile2 Sub3 is going to do some process, and then return values to the following Sub2.)

Application.Run "'" & macroFileName & "'!pasteCapture", _
thisWorkbookName, selectedFile, cmntText, workingSheet, cmntRowNum, cmntColNum, imgRowNum, imgColNum, size, clrTyp
'thisWorkbookName is the absolute path of the file containing macroFile1. I am passing this to macroFile2 Sub3 so that I can call macroFile1 Sub2 from macroFile2 Sub3.

End Sub

Sub2 
'(I wrote this sub so that I can receive return codes from macroFile2 Sub3. This sub is right below macroFile1 Sub1.)

Sub getReturnCodes(extError, fileNotFound, opnError, worksheetNotFound, rowNotNumeric, rowOutOfScope, colNotNumeric, colOutOfScope, sizeNotNumeric, sizeOutOfScope, incorrectClrTyp, success)

If success = 0 Then
    MsgBox "Successful", vbOKOnly
Else
    MsgBox "Error", vbOKOnly
End If

End Sub

'(macroFile2)
Sub3 
'(This sub will receive multiple arguments from macroFile1 Sub1, process them, and call macroFile1 Sub2 to pass multiple return values.)

'Some process...

Application.Run "'" & calledBy & "'!getReturnCodes", _
extError, fileNotFound, opnError, worksheetNotFound, rowNotNumeric, rowOutOfScope, colNotNumeric, colOutOfScope, sizeNotNumeric, sizeOutOfScope, incorrectClrTyp, success

End Sub

执行时出错:该宏可能在此工作簿中不可用,或者所有宏都可能被禁用

请帮助。我想将多个值作为返回值传递给macroFile1。我可以将它们传递给Sub1或Sub2。

原始问题:

从另一个宏文件调用宏时出现错误。 我看过很多类似的文章,并尝试了建议的解决方案;但是,我仍然遇到错误。

代码如下:

'The following program is supposed to call another program

Sub screenCapture_Click()

Dim selectedFile As String 'Full path of the selected file

Const rowNum As Integer = 1
Const colNum As Integer = 1
Const workingSheet As Integer = 1
Const size As Variant = 100
Const clrTyp As Variant = 1

With Application.FileDialog(msoFileDialogOpen)
        .AllowMultiSelect = False

        If .Show = False Then
            Exit Sub
        Else
            selectedFile = .SelectedItems(1)
            Application.Run ("'ScreenCapture子プログラム.xlsm'!pasteCapture (" & selectedFile & "," & rowNum & "," & colNum & "," & workingSheet & "," & size & "," & clrTyp & ")") **
            'This is where the error occors. Application.Run is able to open 'ScreenCapture子プログラム.xlsm', but it is not able to call the macro...**
        End If
End With

End Sub

'The above program calls the following program

Sub pasteCapture(selectedFile, rowNum, colNum, workingSheet, size, clrTyp)

Dim capture As Image 'Captured image
Dim capturesFile As Workbook

EnterCommentUserForm.Show
.
.
.

End Sub

你能帮我吗?

1 个答案:

答案 0 :(得分:0)

应该看起来像这样:

Application.Run "'ScreenCapture子プログラム.xlsm'!pasteCapture", _
                  selectedFile, rowNum, colNum, workingSheet, size, clrTyp

这是一个有用的参考:https://www.rondebruin.nl/win/s9/win001.htm

请注意,带有宏调用的工作簿必须已经打开。

相关问题