Appeler une macro à partir de Java en utilisant Jacob 1.18


J'ai une macro définie dans un fichier Excel et je veux l'invoquer à partir d'un programme Java utilisant Jacob 1.18 jar et dll.

Ce qui suit est le segment de code que j'utilise actuellement.

import java.io.File;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;


public class TestJacob {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        File file = new File("C:\\TestJacob\\TestExcel_copy.xlsm");
        String macroName = "TestMacro";
        callExcelMacro(file, macroName);

    }

    private static void callExcelMacro(File file, String macroName) {
        ComThread.InitSTA(true);
        final ActiveXComponent excel = new ActiveXComponent("Excel.Application");
        try{
            excel.setProperty("EnableEvents", new Variant(false));

            Dispatch workbooks = excel.getProperty("Workbooks")
                    .toDispatch();

            Dispatch workBook = Dispatch.call(workbooks, "Open",
                    file.getAbsolutePath()).toDispatch();

            // Calls the macro
            Variant V1 = new Variant( file.getName() + macroName);
            Variant result = Dispatch.call(excel, "Run", V1);

            // Saves and closes
            Dispatch.call(workBook, "Save");

            com.jacob.com.Variant f = new com.jacob.com.Variant(true);
            Dispatch.call(workBook, "Close", f);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            excel.invoke("Quit", new Variant[0]);
            ComThread.Release();
        }
    }
}

Ce qui suit est l'exception que je reçois lors de l'appel de la macro .

com.jacob.com.ComFailException: Invoke of: Run
Source: Microsoft Excel
Description: Cannot run the macro 'TestExcel_copy.xlsmTestMacro'. The macro may not be available in this workbook or all macros may be disabled.

    at com.jacob.com.Dispatch.invokev(Native Method)
    at com.jacob.com.Dispatch.invokev(Dispatch.java:625)
    at com.jacob.com.Dispatch.callN(Dispatch.java:453)
    at com.jacob.com.Dispatch.call(Dispatch.java:541)
    at TestJacob.callExcelMacro(TestJacob.java:38)
    at TestJacob.main(TestJacob.java:16)

J'ai également activé les macros dans le fichier Excel en suivant les étapes .

  1. Fichier - > Options
  2. Centre de confiance - > Paramètres du centre de confiance
  3. Paramètres de macro
  4. Activé "Activer toutes les macros" et " approuver l'accès au modèle d'objet du projet VBA
  5. appuyez sur " OK "
Author: Community, 2016-01-13

3 answers

Vous devez changer votre appel de macro en

Variant result = Dispatch.call(excel, "Run", new Variant("\'"+file.getName()+"\'"+ macroName));

Parce que dans Excel, le nom du fichier est entre guillemets, il ne trouve donc pas votre nom de macro sans " \ '"

 3
Author: Alann, 2016-04-22 15:27:08

Pas besoin d'ajouter de fichier.getname() lors de l'appel de la macro. Seul le macroname suffit.

Utiliser

Variant result = Dispatch.call(excel, "Run", new Variant(macroName));
 0
Author: kamal pamnani, 2017-02-22 04:49:08

Pour être exact :

Dispatch.call(excel, "Run", new Variant("\'"+file.getName()+"\'!" + macroName));
 0
Author: user3052963, 2017-12-23 21:44:07