Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

COM initialization must be done before accessing COM methods #645

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

import java.io.File;

import com.sun.jna.platform.win32.COM.COMException;
import com.sun.jna.platform.win32.Ole32;
import com.sun.jna.platform.win32.WinDef.LONG;
import com.sun.jna.platform.win32.WinNT.HRESULT;
import com.sun.jna.platform.win32.COM.COMException;
import com.sun.jna.platform.win32.COM.COMUtils;

public class MSOfficeDemo {

Expand All @@ -18,8 +21,14 @@ public static void main(String[] args) {
+ File.separator;

public MSOfficeDemo() {
//this.testMSWord();
this.testMSExcel();
HRESULT hr = Ole32.INSTANCE.CoInitializeEx(null, Ole32.COINIT_MULTITHREADED);
COMUtils.checkRC(hr);
try {
// this.testMSWord();
this.testMSExcel();
} finally {
Ole32.INSTANCE.CoUninitialize();
}
}

public void testMSWord() {
Expand Down Expand Up @@ -113,6 +122,7 @@ public void testMSExcel() {
msExcel.insertValue("A1", "Hello from JNA!");
// close and save the active sheet
msExcel.closeActiveWorkbook(true);
msExcel.quit();
} catch (Exception e) {
e.printStackTrace();

Expand Down