Search in sources :

Example 1 with Range

use of com.sun.jna.platform.win32.COM.util.office.word.Range in project jna by java-native-access.

the class MSExcel method insertValue.

public void insertValue(String range, String value) throws COMException {
    Range pRange = new Range(this.getAutomationProperty("Range", this.getActiveSheet(), new VARIANT(range)));
    this.setProperty("Value", pRange, new VARIANT(value));
}
Also used : VARIANT(com.sun.jna.platform.win32.Variant.VARIANT)

Example 2 with Range

use of com.sun.jna.platform.win32.COM.util.office.word.Range in project jna by java-native-access.

the class Wordautomation_KB_313193_Mod method main.

public static void main(String[] args) throws IOException {
    // Initialize COM Subsystem
    Ole32.INSTANCE.CoInitializeEx(Pointer.NULL, Ole32.COINIT_MULTITHREADED);
    // Initialize Factory for COM object creation
    Factory fact = new Factory();
    try {
        // oEndOfDoc is a predefined bookmark
        final String oEndOfDoc = "\\endofdoc";
        /* \endofdoc is a predefined bookmark */
        // Start word application
        ComWord_Application word = fact.createObject(ComWord_Application.class);
        ComIApplication wordApp = word.queryInterface(ComIApplication.class);
        // Make word visible/invisible (invisible is default)
        wordApp.setVisible(true);
        // Create an empty document (signiture of depends on bindings)
        ComIDocument doc = wordApp.getDocuments().Add();
        Helper.sleep(5);
        //Insert a paragraph at the beginning of the document.
        Paragraph para1 = doc.getContent().getParagraphs().Add(VARIANT_MISSING);
        para1.getRange().setText("Heading 1");
        para1.getRange().getFont().setBold(1);
        //24 pt spacing after paragraph.
        para1.getFormat().setSpaceAfter(24F);
        para1.getRange().InsertParagraphAfter();
        //Insert a paragraph at the end of the document.
        Paragraph para2 = doc.getContent().getParagraphs().Add(doc.getBookmarks().Item(oEndOfDoc).getRange());
        para2.getRange().setText("Heading 2");
        para2.getFormat().setSpaceAfter(6F);
        para2.getRange().InsertParagraphAfter();
        //Insert another paragraph.
        Paragraph para3 = doc.getContent().getParagraphs().Add(doc.getBookmarks().Item(oEndOfDoc).getRange());
        para3.getRange().setText("This is a sentence of normal text. Now here is a table:");
        para3.getRange().getFont().setBold(0);
        para3.getFormat().setSpaceAfter(24F);
        para3.getRange().InsertParagraphAfter();
        //Insert a 3 x 5 table, fill it with data, and make the first row
        //bold and italic.
        Table table = doc.getTables().Add(doc.getBookmarks().Item(oEndOfDoc).getRange(), 3, 5, VARIANT_MISSING, VARIANT_MISSING);
        table.getRange().getParagraphFormat().setSpaceAfter(6F);
        for (int r = 1; r <= 3; r++) {
            for (int c = 1; c <= 5; c++) {
                String strText = "r" + r + "c" + c;
                table.Cell(r, c).getRange().setText(strText);
            }
        }
        table.getRows().Item(1).getRange().getFont().setBold(1);
        table.getRows().Item(1).getRange().getFont().setItalic(1);
        //Add some text after the table.
        Paragraph para4 = doc.getContent().getParagraphs().Add(doc.getBookmarks().Item(oEndOfDoc).getRange());
        para4.getRange().InsertParagraphBefore();
        para4.getRange().setText("And here's another table:");
        para4.getFormat().setSpaceAfter(24F);
        para4.getRange().InsertParagraphAfter();
        //Insert a 5 x 2 table, fill it with data, and change the column widths.
        table = doc.getTables().Add(doc.getBookmarks().Item(oEndOfDoc).getRange(), 5, 2, VARIANT_MISSING, VARIANT_MISSING);
        table.getRange().getParagraphFormat().setSpaceAfter(6F);
        for (int r = 1; r <= 5; r++) {
            for (int c = 1; c <= 2; c++) {
                String strText = "r" + r + "c" + c;
                table.Cell(r, c).getRange().setText(strText);
            }
        }
        //Change width of columns 1 & 2
        table.getColumns().Item(1).setWidth(wordApp.InchesToPoints(2F));
        table.getColumns().Item(2).setWidth(wordApp.InchesToPoints(3F));
        //Keep inserting text. When you get to 7 inches from top of the
        //document, insert a hard page break.
        Range wrdRng;
        float dPos = wordApp.InchesToPoints(7F);
        doc.getBookmarks().Item(oEndOfDoc).getRange().InsertParagraphAfter();
        do {
            wrdRng = doc.getBookmarks().Item(oEndOfDoc).getRange();
            wrdRng.getParagraphFormat().setSpaceAfter(6F);
            wrdRng.InsertAfter("A line of text");
            wrdRng.InsertParagraphAfter();
        } while (dPos >= (Float) wrdRng.getInformation(WdInformation.wdVerticalPositionRelativeToPage));
        wrdRng.Collapse(WdCollapseDirection.wdCollapseEnd);
        wrdRng.InsertBreak(WdBreakType.wdPageBreak);
        wrdRng.Collapse(WdCollapseDirection.wdCollapseEnd);
        wrdRng.InsertAfter("We're now on page 2. Here's my chart:");
        wrdRng.InsertParagraphAfter();
        //Insert a chart and change the chart.
        InlineShape oShape = doc.getBookmarks().Item(oEndOfDoc).getRange().getInlineShapes().AddOLEObject("MSGraph.Chart.8", "", Boolean.FALSE, Boolean.FALSE, VARIANT_MISSING, VARIANT_MISSING, VARIANT_MISSING, VARIANT_MISSING);
        //Demonstrate use of late bound oChart and oChartApp objects to
        //manipulate the chart object with MSGraph.
        IDispatch oChart = oShape.getOLEFormat().getObject();
        IDispatch oChartApp = oChart.getProperty(IDispatch.class, "Application");
        //Change the chart type to Line
        oChart.setProperty("ChartType", XlChartType.xlLine.getValue());
        //Update the chart image and quit MSGraph.
        oChartApp.invokeMethod(Void.class, "Update");
        oChartApp.invokeMethod(Void.class, "Quit");
        //... If desired, you can proceed from here using the Microsoft Graph 
        //Object model on the oChart and oChartApp objects to make additional
        //changes to the chart.
        //Set the width of the chart.
        oShape.setWidth(wordApp.InchesToPoints(6.25f));
        oShape.setHeight(wordApp.InchesToPoints(3.57f));
        //Add text after the chart.
        wrdRng = doc.getBookmarks().Item(oEndOfDoc).getRange();
        wrdRng.InsertParagraphAfter();
        wrdRng.InsertAfter("THE END.");
        File tempFile = Helper.createNotExistingFile("KB_313193_", ".pdf");
        doc.ExportAsFixedFormat(tempFile.getAbsolutePath(), WdExportFormat.wdExportFormatPDF, Boolean.FALSE, WdExportOptimizeFor.wdExportOptimizeForOnScreen, WdExportRange.wdExportAllDocument, null, null, WdExportItem.wdExportDocumentContent, Boolean.FALSE, Boolean.TRUE, WdExportCreateBookmarks.wdExportCreateNoBookmarks, Boolean.TRUE, Boolean.FALSE, Boolean.TRUE, VARIANT_MISSING);
        System.out.println("Output written to: " + tempFile.getAbsolutePath());
        doc.Close(WdSaveOptions.wdDoNotSaveChanges, VARIANT_MISSING, VARIANT_MISSING);
        wordApp.Quit();
    } finally {
        fact.disposeAll();
        Ole32.INSTANCE.CoUninitialize();
    }
}
Also used : InlineShape(com.sun.jna.platform.win32.COM.util.office.word.InlineShape) Table(com.sun.jna.platform.win32.COM.util.office.word.Table) ComIDocument(com.sun.jna.platform.win32.COM.util.office.word.ComIDocument) Factory(com.sun.jna.platform.win32.COM.util.Factory) WdExportRange(com.sun.jna.platform.win32.COM.util.office.word.WdExportRange) Range(com.sun.jna.platform.win32.COM.util.office.word.Range) Paragraph(com.sun.jna.platform.win32.COM.util.office.word.Paragraph) ComWord_Application(com.sun.jna.platform.win32.COM.util.office.word.ComWord_Application) IDispatch(com.sun.jna.platform.win32.COM.util.IDispatch) File(java.io.File) ComIApplication(com.sun.jna.platform.win32.COM.util.office.word.ComIApplication)

Example 3 with Range

use of com.sun.jna.platform.win32.COM.util.office.word.Range in project jna by java-native-access.

the class VariantTest method testVariantConstructors.

public void testVariantConstructors() {
    VARIANT variant;
    // skipped: BSTRByReference constructor
    // skipped: empty constructor
    // skipped: pointer constructor
    // skipped: IDispatch constructor
    String testString = "TeST$รถ";
    BSTR bstr = OleAuto.INSTANCE.SysAllocString(testString);
    variant = new VARIANT(bstr);
    assertThat(variant.getValue(), instanceOf(BSTR.class));
    assertThat(((BSTR) variant.getValue()).getValue(), equalTo(testString));
    assertThat(variant.stringValue(), equalTo(testString));
    variant = new VARIANT(testString);
    assertThat(variant.getValue(), instanceOf(BSTR.class));
    assertThat(((BSTR) variant.getValue()).getValue(), equalTo(testString));
    assertThat(variant.stringValue(), equalTo(testString));
    OleAuto.INSTANCE.SysFreeString(bstr);
    OleAuto.INSTANCE.SysFreeString((BSTR) variant.getValue());
    BOOL boolTrue = new WinDef.BOOL(true);
    variant = new VARIANT(Variant.VARIANT_TRUE);
    assertThat(variant.getValue(), instanceOf(VARIANT_BOOL.class));
    assertThat(((VARIANT_BOOL) variant.getValue()).shortValue(), equalTo((short) 0xFFFF));
    assertThat(variant.booleanValue(), equalTo(true));
    variant = new VARIANT(boolTrue);
    assertThat(variant.getValue(), instanceOf(VARIANT_BOOL.class));
    assertThat(((VARIANT_BOOL) variant.getValue()).shortValue(), equalTo((short) 0xFFFF));
    assertThat(variant.booleanValue(), equalTo(true));
    int testInt = 4223;
    LONG testIntWin = new LONG(testInt);
    variant = new VARIANT(testIntWin);
    assertThat(variant.getValue(), instanceOf(LONG.class));
    assertThat(((LONG) variant.getValue()).intValue(), equalTo(testInt));
    assertThat(variant.intValue(), equalTo(testInt));
    variant = new VARIANT(testInt);
    assertThat(variant.getValue(), instanceOf(LONG.class));
    assertThat(((LONG) variant.getValue()).intValue(), equalTo(testInt));
    assertThat(variant.intValue(), equalTo(testInt));
    short testShort = 23;
    SHORT testShortWin = new SHORT(testShort);
    variant = new VARIANT(testShortWin);
    assertThat(variant.getValue(), instanceOf(SHORT.class));
    assertThat(((SHORT) variant.getValue()).shortValue(), equalTo(testShort));
    assertThat(variant.shortValue(), equalTo(testShort));
    variant = new VARIANT(testShort);
    assertThat(variant.getValue(), instanceOf(SHORT.class));
    assertThat(((SHORT) variant.getValue()).shortValue(), equalTo(testShort));
    assertThat(variant.shortValue(), equalTo(testShort));
    long testLong = 4223L + Integer.MAX_VALUE;
    variant = new VARIANT(testLong);
    assertThat(variant.getValue(), instanceOf(LONGLONG.class));
    assertThat(((LONGLONG) variant.getValue()).longValue(), equalTo(testLong));
    assertThat(variant.longValue(), equalTo(testLong));
    Date testDate = new Date(2042 - 1900, 2, 3, 23, 0, 0);
    variant = new VARIANT(testDate);
    assertThat(variant.getValue(), instanceOf(DATE.class));
    assertThat(variant.dateValue(), equalTo(testDate));
    byte testByte = 42;
    BYTE testByteWin = new BYTE(testByte);
    CHAR testByteWin2 = new CHAR(testByte);
    variant = new VARIANT(testByte);
    assertThat(variant.getValue(), instanceOf(BYTE.class));
    assertThat(((BYTE) variant.getValue()).byteValue(), equalTo(testByte));
    assertThat(variant.byteValue(), equalTo(testByte));
    variant = new VARIANT(testByteWin);
    assertThat(variant.getValue(), instanceOf(BYTE.class));
    assertThat(((BYTE) variant.getValue()).byteValue(), equalTo(testByte));
    assertThat(variant.byteValue(), equalTo(testByte));
    variant = new VARIANT(testByteWin2);
    assertThat(variant.getValue(), instanceOf(CHAR.class));
    assertThat(((CHAR) variant.getValue()).byteValue(), equalTo(testByte));
    assertThat(variant.byteValue(), equalTo(testByte));
    variant = new VARIANT(testByteWin2);
    assertThat(variant.getValue(), instanceOf(CHAR.class));
    assertThat(((CHAR) variant.getValue()).byteValue(), equalTo(testByte));
    assertThat(variant.byteValue(), equalTo(testByte));
    double testDouble = 42.23;
    variant = new VARIANT(testDouble);
    assertThat(variant.getValue(), instanceOf(Double.class));
    // If this fails introduce comparison with range
    assertThat(variant.doubleValue(), equalTo(testDouble));
    float testFloat = 42.23f;
    variant = new VARIANT(testFloat);
    assertThat(variant.getValue(), instanceOf(Float.class));
    // If this fails introduce comparison with range
    assertThat(variant.floatValue(), equalTo(testFloat));
    char testChar = 42 + Short.MAX_VALUE;
    variant = new VARIANT(testChar);
    assertThat(variant.getValue(), instanceOf(USHORT.class));
    assertThat(((USHORT) variant.getValue()).intValue(), equalTo((int) testChar));
    assertThat(variant.intValue(), equalTo((int) testChar));
}
Also used : VARIANT_BOOL(com.sun.jna.platform.win32.OaIdl.VARIANT_BOOL) CHAR(com.sun.jna.platform.win32.WinDef.CHAR) VARIANT(com.sun.jna.platform.win32.Variant.VARIANT) Date(java.util.Date) SHORT(com.sun.jna.platform.win32.WinDef.SHORT) USHORT(com.sun.jna.platform.win32.WinDef.USHORT) BSTR(com.sun.jna.platform.win32.WTypes.BSTR) DATE(com.sun.jna.platform.win32.OaIdl.DATE) LONGLONG(com.sun.jna.platform.win32.WinDef.LONGLONG) VARIANT_BOOL(com.sun.jna.platform.win32.OaIdl.VARIANT_BOOL) BOOL(com.sun.jna.platform.win32.WinDef.BOOL) BYTE(com.sun.jna.platform.win32.WinDef.BYTE) USHORT(com.sun.jna.platform.win32.WinDef.USHORT) LONG(com.sun.jna.platform.win32.WinDef.LONG) LONGLONG(com.sun.jna.platform.win32.WinDef.LONGLONG)

Aggregations

VARIANT (com.sun.jna.platform.win32.Variant.VARIANT)2 Factory (com.sun.jna.platform.win32.COM.util.Factory)1 IDispatch (com.sun.jna.platform.win32.COM.util.IDispatch)1 ComIApplication (com.sun.jna.platform.win32.COM.util.office.word.ComIApplication)1 ComIDocument (com.sun.jna.platform.win32.COM.util.office.word.ComIDocument)1 ComWord_Application (com.sun.jna.platform.win32.COM.util.office.word.ComWord_Application)1 InlineShape (com.sun.jna.platform.win32.COM.util.office.word.InlineShape)1 Paragraph (com.sun.jna.platform.win32.COM.util.office.word.Paragraph)1 Range (com.sun.jna.platform.win32.COM.util.office.word.Range)1 Table (com.sun.jna.platform.win32.COM.util.office.word.Table)1 WdExportRange (com.sun.jna.platform.win32.COM.util.office.word.WdExportRange)1 DATE (com.sun.jna.platform.win32.OaIdl.DATE)1 VARIANT_BOOL (com.sun.jna.platform.win32.OaIdl.VARIANT_BOOL)1 BSTR (com.sun.jna.platform.win32.WTypes.BSTR)1 BOOL (com.sun.jna.platform.win32.WinDef.BOOL)1 BYTE (com.sun.jna.platform.win32.WinDef.BYTE)1 CHAR (com.sun.jna.platform.win32.WinDef.CHAR)1 LONG (com.sun.jna.platform.win32.WinDef.LONG)1 LONGLONG (com.sun.jna.platform.win32.WinDef.LONGLONG)1 SHORT (com.sun.jna.platform.win32.WinDef.SHORT)1