Skip to content

Commit

Permalink
Merge pull request #649 from matthiasblaesing/msoffice_enhancement
Browse files Browse the repository at this point in the history
Update msoffice samples with two MSDN demos and test compatibility
  • Loading branch information
dblock committed May 2, 2016
2 parents 0db246e + 25729de commit 0296115
Show file tree
Hide file tree
Showing 68 changed files with 3,288 additions and 515 deletions.
3 changes: 2 additions & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ Features
* [#640](https://github.com/java-native-access/jna/pull/640): Add `com.sun.jna.platform.win32.Psapi.GetPerformanceInfo()`, `com.sun.jna.platform.win32.Kernel32.GetTickCount64()`, and `com.sun.jna.platform.win32.Kernel32.SetErrorMode()` - [@dbwiddis](https://github.com/dbwiddis).
* [#642](https://github.com/java-native-access/jna/pull/642): COM calls with variable number of arguments (varargs) are now supported - [@SevenOf9Sleeper](https://github.com/SevenOf9Sleeper).
* [#644](https://github.com/java-native-access/jna/pull/644): New ant target 'install' for installing JNA artifacts in local m2-repository - [@SevenOf9Sleeper](https://github.com/SevenOf9Sleeper).
* [#649](https://github.com/java-native-access/jna/pull/649): Bugfix msoffice sample and add two samples taken from MSDN and translated from VisualBasic to Java - [@matthiasblaesing](https://github.com/matthiasblaesing).

Bug Fixes
---------
Expand All @@ -66,7 +67,7 @@ Bug Fixes
* [#602](https://github.com/java-native-access/jna/pull/602): Make sure SID related memory is properly released once no longer required [@lgoldstein](https://github.com/lgoldstein).
* [#610](https://github.com/java-native-access/jna/pull/610): Fixed issue #604: Kernel32#GetLastError() always returns ERROR_SUCCESS [@lgoldstein](https://github.com/lgoldstein).
* [#633](https://github.com/java-native-access/jna/pull/633): Restore default usage of platform native encoding for Java strings passed to native functions (was hard-coded to UTF-8 in 4.0 and later) [@amake](https://github.com/amake)
* [#634](https://github.com/java-native-access/jna/pull/634): Improve BSTR handling - [@matthiasblaesing](https://github.com/matthiasblaesing).
* [#634](https://github.com/java-native-access/jna/pull/634): Improve BSTR handling and add `SysStringByteLen` and `SysStringLen` to `com.sun.jna.platform.win32.OleAuto` - [@matthiasblaesing](https://github.com/matthiasblaesing).

Release 4.2.1
=============
Expand Down
Binary file removed contrib/msoffice/jnatest.xls
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
import java.io.IOException;
import java.util.ArrayList;

import com.sun.jna.platform.win32.COM.COMUtils;
import com.sun.jna.platform.win32.COM.COMUtils.COMInfo;
import java.io.File;

public class COMInfoUtil {

public static void main(String[] args) {
FileWriter writer = null;
try {
String filename = "C:\\TEMP\\CLSIDs.txt";
String filename = new File(Helper.tempDir, "CLSIDs.txt").getAbsolutePath();
ArrayList<COMInfo> comInfos = COMUtils.getAllCOMInfoOnSystem();
writer = new FileWriter(filename);

Expand Down
75 changes: 75 additions & 0 deletions contrib/msoffice/src/com/sun/jna/platform/win32/COM/Helper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@

package com.sun.jna.platform.win32.COM;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;

public class Helper {
public static final File tempDir = new File(System.getProperty("java.io.tmpdir"));

/**
* Sleep for specified seconds.
*
* @param seconds
*/
public static void sleep(int seconds) {
try {
Thread.sleep(seconds * 1000L);
} catch (InterruptedException ex) {
// Ignore
}
}

/**
* Extract data contained in classpath into a system accessible target file.
*
* @param localPath
* @param target
* @throws IOException
*/
public static void extractClasspathFileToReal(String localPath, File target) throws IOException {
InputStream is = null;
OutputStream os = null;
try {
is = Helper.class.getResourceAsStream(localPath);
os = new FileOutputStream(target);

int read;
byte[] buffer = new byte[20480];

while((read = is.read(buffer)) > 0) {
os.write(buffer, 0, read);
}

} finally {
if(is != null) {
try {
is.close();
} catch(Exception ex) {}
}
if(os != null) {
try {
os.close();
} catch(Exception ex) {}
}
}
}

/**
* Create a temporary file, that does not exist.
*
* @param prefix
* @param suffix
* @return
* @throws IOException
*/
public static File createNotExistingFile(String prefix, String suffix) throws IOException {
File tempFile = Files.createTempFile(prefix, suffix).toFile();
tempFile.delete();
return tempFile;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ public void newExcelBook() throws COMException {
this.invokeNoReply("Add", getWorkbooks());
}

public void openExcelBook(String filename, boolean bVisible)
throws COMException {
// OpenDocument
public void openExcelBook(String filename) throws COMException {
this.invokeNoReply("Open", getWorkbooks(), new VARIANT(filename));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
package com.sun.jna.platform.win32.COM.office;

import java.io.File;
import com.sun.jna.Pointer;

import com.sun.jna.platform.win32.COM.COMException;
import com.sun.jna.platform.win32.COM.Helper;
import com.sun.jna.platform.win32.Ole32;
import com.sun.jna.platform.win32.WinDef.LONG;
import java.io.File;
import java.io.IOException;

public class MSOfficeDemo {

/**
* @param args
*/
public static void main(String[] args) {
new MSOfficeDemo();
}

private String currentWorkingDir = new File("").getAbsolutePath()
+ File.separator;

public MSOfficeDemo() {
//this.testMSWord();
this.testMSExcel();
public static void main(String[] args) throws IOException {
Ole32.INSTANCE.CoInitializeEx(Pointer.NULL, Ole32.COINIT_MULTITHREADED);
try {
MSOfficeDemo demo = new MSOfficeDemo();
demo.testMSExcel();
demo.testMSWord();
} finally {
Ole32.INSTANCE.CoUninitialize();
}
}

public void testMSWord() {
public void testMSWord() throws IOException {
File demoDocument = null;
MSWord msWord = null;

// http://msdn.microsoft.com/en-us/library/office/ff839952(v=office.15).aspx
Expand All @@ -42,82 +42,92 @@ public void testMSWord() {
System.out.println("MSWord version: " + msWord.getVersion());

msWord.setVisible(true);
// msWord.newDocument();
msWord.openDocument(currentWorkingDir + "jnatest.doc", true);

Helper.sleep(5);

demoDocument = Helper.createNotExistingFile("jnatest", ".doc");
Helper.extractClasspathFileToReal("/com/sun/jna/platform/win32/COM/util/office/resources/jnatest.doc", demoDocument);

msWord.openDocument(demoDocument.getAbsolutePath());
msWord.insertText("Hello from JNA! \n\n");
// wait 10sec. before closing
Thread.currentThread().sleep(1000);
Helper.sleep(10);
// save in different formats
// pdf format is only supported in MSWord 2007 and above
msWord.SaveAs("C:\\TEMP\\jnatestSaveAs.doc", wdFormatDocument);
msWord.SaveAs("C:\\TEMP\\jnatestSaveAs.pdf", wdFormatPDF);
msWord.SaveAs("C:\\TEMP\\jnatestSaveAs.rtf", wdFormatRTF);
msWord.SaveAs("C:\\TEMP\\jnatestSaveAs.html", wdFormatHTML);
System.out.println("Wrinting files to: " + Helper.tempDir);
msWord.SaveAs(new File(Helper.tempDir, "jnatestSaveAs.doc").getAbsolutePath(), wdFormatDocument);
msWord.SaveAs(new File(Helper.tempDir, "jnatestSaveAs.pdf").getAbsolutePath(), wdFormatPDF);
msWord.SaveAs(new File(Helper.tempDir, "jnatestSaveAs.rtf").getAbsolutePath(), wdFormatRTF);
msWord.SaveAs(new File(Helper.tempDir, "jnatestSaveAs.html").getAbsolutePath(), wdFormatHTML);
// close and save the document
msWord.closeActiveDocument(false);
msWord.newDocument();
// msWord.openDocument(currentWorkingDir + "jnatest.doc", true);
msWord.insertText("Hello from JNA! \n Please notice that JNA can control MS Word via the new COM interface! \nHere we are creating a new word document and we save it to the 'TEMP' directory!");
// save with no user prompt
msWord.SaveAs("C:\\TEMP\\jnatestNewDoc1.docx", wdFormatDocumentDefault);
msWord.SaveAs("C:\\TEMP\\jnatestNewDoc2.docx", wdFormatDocumentDefault);
msWord.SaveAs("C:\\TEMP\\jnatestNewDoc3.docx", wdFormatDocumentDefault);
msWord.SaveAs(new File(Helper.tempDir, "jnatestNewDoc1.docx").getAbsolutePath(), wdFormatDocumentDefault);
msWord.SaveAs(new File(Helper.tempDir, "jnatestNewDoc2.docx").getAbsolutePath(), wdFormatDocumentDefault);
msWord.SaveAs(new File(Helper.tempDir, "jnatestNewDoc3.docx").getAbsolutePath(), wdFormatDocumentDefault);
// close and save the document
msWord.closeActiveDocument(false);
// open 3 documents
msWord.openDocument("C:\\TEMP\\jnatestNewDoc1.docx", true);
msWord.openDocument(new File(Helper.tempDir, "jnatestNewDoc1.docx").getAbsolutePath());
msWord.insertText("Hello some changes from JNA!\n");
msWord.openDocument("C:\\TEMP\\jnatestNewDoc2.docx", true);
msWord.openDocument(new File(Helper.tempDir, "jnatestNewDoc2.docx").getAbsolutePath());
msWord.insertText("Hello some changes from JNA!\n");
msWord.openDocument("C:\\TEMP\\jnatestNewDoc3.docx", true);
msWord.openDocument(new File(Helper.tempDir, "jnatestNewDoc3.docx").getAbsolutePath());
msWord.insertText("Hello some changes from JNA!\n");
// save the document and prompt the user
msWord.Save(false, wdPromptUser);
// wait then close word
msWord.quit();
} catch(InterruptedException ie) {
ie.printStackTrace();
} catch (COMException e) {
if (e.getExcepInfo() != null) {
System.out
.println("bstrSource: " + e.getExcepInfo().bstrSource);
System.out.println("bstrDescription: "
+ e.getExcepInfo().bstrDescription);
System.out.println("bstrSource: " + e.getExcepInfo().bstrSource);
System.out.println("bstrDescription: " + e.getExcepInfo().bstrDescription);
}

// print stack trace
e.printStackTrace();

if (msWord != null)
} finally {
if (msWord != null) {
msWord.quit();
}

if(demoDocument != null && demoDocument.exists()) {
demoDocument.delete();
}
}
}

public void testMSExcel() {
public void testMSExcel() throws IOException {
File demoDocument = null;
MSExcel msExcel = null;

try {
msExcel = new MSExcel();
System.out.println("MSExcel version: " + msExcel.getVersion());
msExcel.setVisible(true);
// msExcel.newExcelBook();
msExcel.openExcelBook(currentWorkingDir + "jnatest.xls", true);

Helper.sleep(5);

demoDocument = Helper.createNotExistingFile("jnatest", ".xls");
Helper.extractClasspathFileToReal("/com/sun/jna/platform/win32/COM/util/office/resources/jnatest.xls", demoDocument);

msExcel.openExcelBook(demoDocument.getAbsolutePath());
msExcel.insertValue("A1", "Hello from JNA!");
// wait 10sec. before closing
Thread.currentThread().sleep(10000);
Helper.sleep(10);
// close and save the active sheet
msExcel.closeActiveWorkbook(true);
msExcel.setVisible(true);
// msExcel.newExcelBook();
msExcel.openExcelBook(currentWorkingDir + "jnatest.xls", true);

msExcel.newExcelBook();
msExcel.insertValue("A1", "Hello from JNA!");
// close and save the active sheet
msExcel.closeActiveWorkbook(true);
} catch (Exception e) {
e.printStackTrace();

if (msExcel != null)
} finally {
if (msExcel != null) {
msExcel.quit();
}

if (demoDocument != null && demoDocument.exists()) {
demoDocument.delete();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@ public void newDocument() throws COMException {
this.invokeNoReply("Add", getDocuments());
}

public void openDocument(String filename, boolean bVisible)
throws COMException {
// OpenDocument
public void openDocument(String filename) throws COMException {
this.invokeNoReply("Open", getDocuments(), new VARIANT(filename));
}

Expand Down Expand Up @@ -70,7 +68,6 @@ public ActiveDocument getActiveDocument() {
}

public Documents getDocuments() {
// GetDocuments
Documents pDocuments = new Documents(this.getAutomationProperty(
"Documents", this.getApplication().getIDispatch()));

Expand Down
Loading

0 comments on commit 0296115

Please sign in to comment.