简体   繁体   中英

Java app that runs an VBA macro works only on my computer

I created a Java app that does a few things:

1.based on some input it generates some text - works on other computers

2.based on an input it modifies a excel file - work on other computers

3.it runs a macro from the excel file that it modified earlier - works only on my computer

This is the part of the code that edis the excel file and runs the macro:

try {

    File myFile = new File(atmScriptGeneratorPathTxt.getText() + "\\ATM Script Generator.xlsm");
    FileInputStream fis = new FileInputStream(myFile);
    XSSFWorkbook myWorkBook = new XSSFWorkbook(fis);
    XSSFSheet mySheet = myWorkBook.getSheetAt(0);
    Cell myCell = null;
    myCell= mySheet.getRow(2).getCell(2);
    myCell.setCellValue(newRNCCDRFolderTxt.getText());

    fis.close();
    FileOutputStream output_file =new FileOutputStream(new File(atmScriptGeneratorPathTxt.getText() + "\\ATM Script Generator.xlsm"));
    myWorkBook.write(output_file);
    output_file.close();
    myWorkBook.close();

    String macroName = "'ATM Script Generator.xlsm'!ATM_Script_Generator.ATM_Script_Generator";             
    ComThread.InitSTA();
    ActiveXComponent excel = new ActiveXComponent("Excel.Application");

    try {
        //This will open the excel if the property is set to true    

        Dispatch workbooks = excel.getProperty("Workbooks") .toDispatch();
        Dispatch workBook = Dispatch.call(workbooks, "Open", myFile.getAbsolutePath()).toDispatch();                                                                           
        // Calls the macro
        final Variant result = Dispatch.call(excel, "Run", new Variant(macroName));
        System.out.println(result);
        // 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 error) {
            error.printStackTrace();
        } finally {

            excel.invoke("Quit", new Variant[0]);
            ComThread.Release();
        }
    }
    catch(Exception err) {
        System.out.println(err);
    }
}

These are the libraries that I am using for this app:

import org.apache.poi.ss.usermodel.Cell;

import org.apache.poi.xssf.usermodel.XSSFSheet;

import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import com.jacob.activeX.ActiveXComponent;

import com.jacob.com.ComThread;

import com.jacob.com.Dispatch;

import com.jacob.com.Variant;

Does anyone knows why this app works only on my computer? If there is any more info that someone needs in order to figure out what is the problem, let me know.

Jacob Libray contains two .dll files:

jacob-1.18-x64.dll

jacob-1.18-x86.dll

I copied this two .dll files to: C:\\Program Files\\Java\\jre1.8.0_111\\bin and it worked after.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM