Satyen

I am creating a MS Word Addin and want to access the content of the document as byte array. I tried doing SaveAs but then when I tried to read (FileStream) it gives me error back saying that 'The process cannot access the file because it is being used by another process'

Any ideas how to resolve this issue Note - I don;t want to close the document.



Re: Visual Studio Tools for Office How to get Document Content in byte array

Cindy Meister

Word has two possibilities

1. copy the content to the Clipboard, then stream it off the Clipboard

2. Read the document Range's XML

The Word object model provides no facility for streaming an opened document's content (the Word binary file format) directly. A third possibility would be to "Print to file" then stream the file from disk.






Re: Visual Studio Tools for Office How to get Document Content in byte array

Malleswar

Hi Satyen,

I have same type of feature for my project. I have to read currently opened document content (Word, PowerPoint, Excel) into streams. I need this for Outlook 2003 and 2007.

Can you please guide me or provide any link for this

Thanks in advance




Re: Visual Studio Tools for Office How to get Document Content in byte array

Hirén

Hi,

I am facing the same situation here, please inform me if anyone knows how to convert the Active Document content in an array of bytes.

Hiren





Re: Visual Studio Tools for Office How to get Document Content in byte array

Satyen

Hi there,

Since I could not find any elegant soution to that, so I ended up making a copy of the file, and then reading it back as stream. Note I had to copy the tempfile otherwise I was getting the access denied error (as the tempfile was still opend by the Active Workbook)

Note the following code snippet I used for Microsoft Excel - but you get the idea ...

Code Snippet

private void button_Save_Click(object sender, EventArgs e)
{
try
{

....
string keyName = textBox_KeyName.Text.ToString().Trim();

// get the content of the Active workbook in byte array
byte[] contents = GetWorkbookContents(keyName);

// display the success message to the user

...
}
catch (Exception ex)
{
this._application.StatusBar = ex.Message;
}
}

Code Snippet

private byte[] GetWorkbookContents(string keyName)
{
object objFile = Path.Combine(Path.GetTempPath(), keyName);
string tempFile = (string)objFile;

object missing = System.Reflection.Missing.Value;

((Microsoft.Office.Interop.Excel.Workbook)_application.ActiveWorkbook).SaveAs(objFile,
missing,
missing,
missing,
missing,
missing,
Excel.XlSaveAsAccessMode.xlShared,
missing,
missing,
missing,
missing,
missing);

FileInfo fi = new FileInfo(tempFile);
String tempFileCopy = (Path.Combine(Path.GetTempPath(), "copy_" + keyName));
fi.CopyTo(tempFileCopy, true);
byte[] contents = File.ReadAllBytes((string)tempFileCopy);
File.Delete(tempFileCopy);

return contents;
}

Hope that helps...

Thanks - Satyen





Re: Visual Studio Tools for Office How to get Document Content in byte array

Hirén

Hi Satyen,

Thats exactly how I am proceeding right now, but it does not seem to be the ideal way, as I would not like the file to be stored onto the local machine upon selecting this option.

Is there a way to directly stream the document content into an array of bytes, without first writing it onto the disk (i.e. in memory )

Any help would be appreciated.

-Hiren





Re: Visual Studio Tools for Office How to get Document Content in byte array

Satyen

Hi

As Cindy Meister has mentioned, you can copy the conetnt in a clipboard and then read the stream, something like below -


((Microsoft.Office.Interop.Word.Document)_document).ActiveWindow.Selection.WholeStory();
((Microsoft.Office.Interop.Word.Document)_document).ActiveWindow.Selection.Copy();

IDataObject data = Clipboard.GetDataObject();

string text = data.GetData(DataFormats.Html).ToString();

But you might lose some formatting while converting as Word might contain other formats.

Hope that helps...

- Satyen





Re: Visual Studio Tools for Office How to get Document Content in byte array

Malleswar

Hi Satyen,

Thank u very much for ur code. But when u save that file that file will be saved. even it will not be deleted.

File.Delete(tempFileCopy);

what is the use of this line of code. Actually I want to make the old file to be open not my temporary file. Is it possible in any way.

Can you plz guide me

Thanks in advance,