[英]Java app that runs an VBA macro works only on my computer
I created a Java app that does a few things: 我创建了一个执行以下操作的Java应用程序:
1.based on some input it generates some text - works on other computers 1.基于一些输入,它会生成一些文本-在其他计算机上可以使用
2.based on an input it modifies a excel file - work on other computers 2.根据输入修改Excel文件-在其他计算机上工作
3.it runs a macro from the excel file that it modified earlier - works only on my computer 3.它从先前修改的excel文件运行一个宏-仅在我的计算机上工作
This is the part of the code that edis the excel file and runs the macro: 这是excel文件并运行宏的部分代码:
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; 导入org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.xssf.usermodel.XSSFSheet; 导入org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook; 导入org.apache.poi.xssf.usermodel.XSSFWorkbook;
import com.jacob.activeX.ActiveXComponent; 导入com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread; 导入com.jacob.com.ComThread;
import com.jacob.com.Dispatch; 导入com.jacob.com.Dispatch;
import com.jacob.com.Variant; 导入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 Libray包含两个.dll文件:
jacob-1.18-x64.dll jacob-1.18-x64.dll
jacob-1.18-x86.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. 我将这两个.dll文件复制到:C:\\ Program Files \\ Java \\ jre1.8.0_111 \\ bin,此后它可以工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.