Hi and thanks for the response.
I’ve already tried this with an RTF Office document without success.
I still believe the problem to be at this line asposeDoc.Save(stm, Aspose.Words.SaveFormat.Rtf);
The 1st method in action is AutomateDocument. In here we get the selected template. Get the byte array of it and then pass that into the Aspose Document constructor. We then do bookmark replacement. Next, depending on whether this is an email or word doc we instantiate a WordControl passing in the working Aspose Document(doc).
In the WordControl constructor we save the Aspose Document to a MemoryStream and load the RichTextBox.
private void AutomateDocument(ListBox selectedDoc)
{
selectedTemplate = (DocumentTemplate)selectedDoc.SelectedItem;
string fileName = selectedTemplate.URL;
string noteComment = selectedTemplate.Name;
object oTrue = true;
object oFalse = false;
if (!File.Exists(fileName))
{
MessageBox.Show(string.Format("Unable to locate template: {0}", fileName));
return;
}
// Check the directory exists we want to save to
if (!Directory.Exists(_case.CaseFolder))
{
// Directory doesn’t exist so create it
Directory.CreateDirectory(_case.CaseFolder);
//Directory.CreateDirectory(System.Configuration.ConfigurationManager.AppSettings["SharedAppDataFolderInvuDocsWrite"]);
}
try
{
//We have to use two filestreams as a simple File.Copy did not carry across any attachemnts, don’t ask me why!
using (FileStream fs = File.Open(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
using (FileStream fsSave = new FileStream(dest, FileMode.Create))
{
bytFile = new byte[fs.Length];
fs.Read(bytFile, 0, bytFile.Length);
fsSave.Write(bytFile, 0, bytFile.Length);
fsSave.Flush();
}
}
Aspose.Words.License lic = new Aspose.Words.License();
lic.SetLicense(\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*);
//using (FileStream fs = File.Open(dest, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
//{
doc = new Aspose.Words.Document(new System.IO.MemoryStream(bytFile));
//Aspose.Words.Document doc1 = new Aspose.Words.Document("W:\Default templates\WSA Standard Report Template.doc");
//doc.Save("C:\Temp\WSAfrombytearray.doc", Aspose.Words.SaveFormat.Rtf);
//Populate the bookmarks
foreach (Aspose.Words.Bookmark b in doc.Range.Bookmarks)
{
// Check to see we are actually processing a bookmark requiring programmatical text replacement
if (b.Name.ToLower().Contains("hmw"))
{
DocumentTemplateBookmark dtb = new DocumentTemplateBookmark(b.Name);
ProcessReflectedTypes(dtb, b, fileName);
}
}
doc.AcceptAllRevisions();
memStr.Write(bytFile, 0, bytFile.Length);
memStr.Position = 0;
if (selectedTemplate.IsEmail)
{
doc.Save(dest);
}
else
{
doc.Save(memStr, Aspose.Words.SaveFormat.Rtf);
}
}
catch (IOException ex)
{
Logging.LogBadLogin(\_currentUser.NTUserID, this.Name, ex.StackTrace);
MessageBox.Show(ex.Message, "File Access Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (selectedTemplate.IsEmail)
{
mm = SetupMailMessage();
mm.Subject = selectedTemplate.Name;
//mm.Save(dest, Aspose.Network.Mail.MailMessageSaveType.OutlookMessageFormat);
email = new OfficeEditors.EmailControl(dest, memStr, mm);
Form emailForm = new Form();
emailForm.WindowState = FormWindowState.Maximized;
emailForm.Controls.Add(email);
email.Dock = DockStyle.Fill;
emailForm.Text = "HML Online Email Viewer";
emailForm.ShowDialog();
}
else
{
word = new OfficeEditors.WordControl(doc, memStr, dest);
Form wordForm = new Form();
wordForm.WindowState = FormWindowState.Maximized;
wordForm.Controls.Add(word);
word.Dock = DockStyle.Fill;
wordForm.Text = "HML Online Word Viewer";
wordForm.ShowDialog();
}
public WordControl(Document asposeDoc, MemoryStream document, string fileName)
{
InitializeComponent();
setup();
_fileName = fileName;
System.IO.MemoryStream stm = new System.IO.MemoryStream();
asposeDoc.Save(stm, Aspose.Words.SaveFormat.Rtf);
stm.Position = 0;
rtbDoc.LoadFile(stm, RichTextBoxStreamType.RichText);
}