Looking for an sample code for writing a vector of strings in MS word

hi, I couldn’t find a sample to see how to create a MS word file and write a queue of strings in it by Aspose.word for java, would you recommend something please? thanks

Hi there,

Thanks for your inquiry.

Please see the article here which shows you how to insert document elements using the DocumentBuilder.

If you have any other queries please feel free to ask.

Thanks,

thanks for reply but I wrote this code and I got these exceptions, would you tell me where are mistakes?


com.aspose.words.UnsupportedFileFormatException: Unknown file format.
at com.aspose.words.Document.a(Unknown Source)
at com.aspose.words.Document.(Unknown Source)
at com.aspose.words.Document.(Unknown Source)
at test.main(test.java:8)



import com.aspose.words.;
import com.aspose.words.documentexplorer.;

public class test {

public static void main(String[] args){
try{
Document doc = new Document(“a.doc”);
DocumentBuilder builder = new DocumentBuilder(doc);
builder.write(“Hello World!”);}
catch (Exception e){
e.printStackTrace();
}
}
}

Hi there,

Thanks for this additional information.

Could you please attach the document "a.doc" here for testing and I will provide you with some further information.

Thanks,

a.doc is an empty MS word doc , I just wanna open an already existed doc and insert a string

Hi there,

Thanks for your inquiry.

As I can see your attached document is actually empty. If you want to use a blank template as a document then you can simply call the document constructor with no parameters like this: Document doc = new Document();

Thanks,

thanks I could fix the exceptions, the problem was the a.doc, I opened a MS word and saved a new a.doc and the problem is solved, it’s running properly but without any output! I can’t see any “Hello World!” in the a.doc after running program, why couldn’t I save it?

Hi there,

Thanks for your inquiry.

In order to save the changes made with your code above, you need to call the Document.Save method at the end of your code. Passing the same path that you used to load in your document will override the original document with the new document.

The reason why your original template was throwing that exception is because an MS Word document is not like a .txt file as even when it's empty it will still have some data describing settings etc. Because of this an empty .doc file is invalid.

Please take a look at documentation here regarding loading and saving documents in Aspose.Words.

If you have any further questions please feel free to ask.

Thanks,

thanks for quiet informative and quick replies :slight_smile: I fixed them .