java - 在 Java 中嵌入 Office

标签 java ms-office swt embed

我正在尝试使用 SWT 使用以下代码将 Office 2007/2010 应用程序嵌入到 Java 应用程序中:

import java.awt.Canvas;
import javax.swing.JFrame;

import org.eclipse.swt.SWT;
import org.eclipse.swt.awt.SWT_AWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.ole.win32.*;
import org.eclipse.swt.widgets.*;

public class EmbeddingTest extends Canvas {
    private void initOleViewer(String target) {
        Display display = new Display();
        Shell shell = SWT_AWT.new_Shell(display, this);
        shell.setLayout(new FillLayout());

        OleFrame oleFrame = new OleFrame(shell, SWT.NONE);

        OleControlSite oleControlSite = new OleControlSite(oleFrame, SWT.NONE, "Word.Document");
        oleControlSite.doVerb(OLE.OLEIVERB_INPLACEACTIVATE);

        OleAutomation word = new OleAutomation(oleControlSite);

        int[] applicationId = word.getIDsOfNames(new String[]{"Application"});
        Variant property = word.getProperty(applicationId[0]);
        OleAutomation application = property.getAutomation();

        int[] documentId = application.getIDsOfNames(new String[]{"Documents"});            
        property = application.getProperty(documentId[0]);
        OleAutomation documents = property.getAutomation();

        shell.open();
        Variant[] arguments = new Variant[] { new Variant(target) };
        int[] automationIDs = documents.getIDsOfNames(new String[]{"Open", "FileName"});
        documents.invokeNoReply(automationIDs[0], arguments, new int[]{automationIDs[1]});

        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
    }

    public static void main(String[] args) {
        JFrame jFrame = new JFrame("Embedding Test");
        jFrame.setVisible(true);

        EmbeddingTest viewer = new EmbeddingTest();
        jFrame.add(viewer);
        jFrame.setSize(600, 600);

        viewer.initOleViewer(args[0]);
    }
}

当我不尝试在文档对象上调用“打开”时,Word 成功嵌入到应用程序中,但整个文件菜单被禁用。当我调用“打开”时,应用程序崩溃并出现以下错误 (DISP_E_EXCEPTION):

Exception in thread "main" org.eclipse.swt.SWTException: Action can not be performed. result = -2147352567
 at org.eclipse.swt.ole.win32.OLE.error(Unknown Source)
 at org.eclipse.swt.ole.win32.OleAutomation.invokeNoReply(Unknown Source)
 at EmbeddingTest.initOleViewer(EmbeddingTest.java:68)
 at EmbeddingTest.main(EmbeddingTest.java:88)

有谁知道如何解决此问题或在 Java 中嵌入 Office 应用程序的替代解决方案?谢谢!



更新:

分别查询 'Open' 和 'FileName' 的 ID 会为 'FileName' 返回 null,因此它不正确。我也试过没有命名参数但没有成功:

documents.invokeNoReply(automationIDs[0], arguments);

最佳答案

您为什么不进行任何错误处理、结果检查或断言?请记住,getIDsOfNames(..) 将默默地失败并为无法识别的名称返回 null 值。

在捕获异常后尝试打印 documents.getLastError() 的值。

关于java - 在 Java 中嵌入 Office,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3278552/

相关文章:

java - 允许对 void 调用返回语句

java - Hibernate 5 忽略@Table 模式参数

java - 我可以向 JAX-RS 方法添加自定义注释以验证访问吗?

vba - 如何在 Word 2007 VBA 中访问某种 OnLoad 事件?

java - 如何从 Java 中的图形中读取坐标?

.net docx/xlsx 模板引擎

c# - 如何更改在预览 Pane 中显示或打开的 MailItem

java - SWT:表丢失选择

java - 使用 SWT 浏览器小部件时如何从 Java 设置 Mozilla 首选项?

java - 使用数学矩阵的 EditingSupport 构建 JFace TableViewer