title: LibreOffice UNO: Setting Styles (anwer can be provided in Java, VB, Python, C++, any language using UNO API)
body: <p>I have an issue where I'm simply trying to add new text and then apply a LibreOffice style to it. I want to add text and have it follow a particular style ("Heading 1", "Heading 2", etc).</p>
  
  <p>Adding text to the document does work, changing the style does work however the last style set is applied to the entire document and not just the last String. I need some way to limit the selection to the String. I think I need an XTextRange and the Style belongs to that and not the cursor... but don't know how to create new XTextRanges containing only my latest String... clearly not certain, and advice would be most welcome. </p>
  
  <p><strong>EDIT</strong>: Although the following code is in Java I am more than willing at accept a solution using any programming language, the UNO APIs are similar enough that I could convert a solution from another language. I have the feeling there are more VB macro writers for OOo/LO than Java developers, then again maybe a C++ or Python developer has a solution. I should think to write out a document changing styles would be a pretty basic requirement!</p>
  
  <pre><code>com.sun.star.text.XText xText = this.xDoc.getText();
  //create a cursor object
  com.sun.star.text.XTextCursor xTCursor = xText.createTextCursor();
  this.writeResume(xText, xTCursor);
  </code></pre>
  
  <p>method to write resume... you will see where I attempt to change the style with the <em>changeStyle</em> method</p>
  
  <pre><code>private void writeResume(com.sun.star.text.XText xText, com.sun.star.text.XTextCursor xTCursor) {
      TestData resume = new TestData();
      List&lt;Company&gt; companies = resume.getCompanies();
      this.changeStyle(xTCursor, "Heading 1");
      xText.insertString(xTCursor, "Professional Experience\n", false);
      xTCursor.collapseToEnd();
      this.changeStyle(xTCursor, "Heading 2");
      Company company = companies.get(0);
      String date = dformat.format(company.getStartDate().getTime()) + " - " + dformat.format(company.getEndDate().getTime());
      xText.insertString(xTCursor, company.getName() + "," + company.getLocation() + "\t" + date + "\n", false);
      xTCursor.collapseToEnd();
      this.changeStyle(xTCursor, "Heading 3");
      xText.insertString(xTCursor, "Test Point 1\n", false);
      xText.insertString(xTCursor, "Test Point 2\n", false);
      xText.insertString(xTCursor, "Test Point 3\n", false);
  }
  </code></pre>
  
  <p>The changeStyle method</p>
  
  <pre><code>public void changeStyle(com.sun.star.text.XTextCursor xTCursor, String styleName) {
      XPropertySet xCursorProps = UnoRuntime.queryInterface(XPropertySet.class, xTCursor);
      try {
          xCursorProps.setPropertyValue("ParaStyleName", styleName);
      } catch (UnknownPropertyException | PropertyVetoException | IllegalArgumentException | WrappedTargetException ex) {
          Logger.getLogger(ResumeWriter.class.getName()).log(Level.SEVERE, null, ex);
      }
  }
  </code></pre>