active document, get as xml

Add-in Express™ Support Service
That's what is more important than anything else

active document, get as xml
 
Eric Kaufman




Posts: 5
Joined: 2007-06-08
Hey everyone. This is my first time developing applications inside of Office, and I can see why I've avoided it for so long :D

I was pretty impressed when I saw the get_XML method laying around, and when I saved a word doc to an image as an xml file, it was pretty neat that the binary contents got base64 encoded. Being able to treat a word document as a full-blooded .net class (XMLDocument) makes it a lot easier for me to move it around inside of my XML/SOAP world.

I'm having a bit of a problem getting the active document out to xml though. It seems like I can do ActiveDocument.SaveAs, but isn't it a bit cheesy to have to save the file to disk, and then scoop it up from there?

I picked up the premium edition of Add-in-Express, if that makes a difference. Basically any help or guidance towards how I can get Word / Excel documents in and out of xml would be terrific.

Thanks so much!

Eric
Posted 08 Jun, 2007 18:19:31 Top
Sergey Grischenko


Add-in Express team


Posts: 7187
Joined: 2004-07-05
Hi Eric.

The fact is that Word doesn't provide any methods to save its documents in a stream. So I would suggest you to save documents to temporary files and then transfer them to a format you need.



P.S. Note that we take up your forum requests in the order we receive them.
Besides, it may take us some time to investigate your issue. Please be sure we will let you know as soon as the best possible solution is found.
Posted 11 Jun, 2007 08:50:13 Top
Eric Kaufman




Posts: 5
Joined: 2007-06-08
Sergey;

Thanks for your quick and accurate response. Not being able to save to a stream will certainly restrict my options. Can I ask how to save a word document as xml to disk then? I've hunted around a while, and can only find VBA syntax ( http://msdn2.microsoft.com/en-us/library/aa220734(office.11).aspx ).

One of the options listed in there, and the one I'm looking for (that will base64 encode the binaries in the document), is "wdFormatXML".

That option, however, is not available to me in the enum of Word.WdSaveFormat

Whatever you think would be the best way to get a word document to XML, including the base64 encoding of binaries, I'm all ears. I'm using Word2007 as well, and can probably mandate that my users do as well if it comes down to it.

Eric
Posted 11 Jun, 2007 13:31:25 Top
Sergey Grischenko


Add-in Express team


Posts: 7187
Joined: 2004-07-05
Hi Eric.

What version of PIAs do you use in the add-in project?
Posted 12 Jun, 2007 09:23:52 Top
Eric Kaufman




Posts: 5
Joined: 2007-06-08
Visual Studio 2005\Projects\AdxProto2\OfficePIAs\Interop.Word.dll, marked as version 8.1.0.0

Interestingly, Interop.Outlook.dll is marked as 9.0.0.0, and Excel is 1.3.0.0

I also have 9.0.0.0 across the board for VSTO\9.0\Microsoft.VisualStudio.Tools.Office.[appname].dll


Again, this is my first time working in Office, so maybe I'm missing something obvious. I tried initially to convert the public member of WordApp to the VSTO Word version ( Microsoft.Office.Tools.Word.Document ), but I got a cast error. Maybe I have to save the thing to disk, then instaniate a new document, and load it from the saved one?

Or, I can bypass Microsoft.Office.Tools.Word.Document in entirety if there's another "xml / base64 the binaries" approach out there.
Posted 12 Jun, 2007 13:48:52 Top
Eric Kaufman




Posts: 5
Joined: 2007-06-08
Just as an update, here's a way that I *think* I have it working. Basically, I'm taking the open document, saving it to disk, then instantiating a new Microsoft.Office.Interop.Word.Application, which reads in the file, and allows me to access the ActiveDocument.WordOpenXML public member.

Not sure exactly how well this will work, but for now, it allows me (in Word 2007 at least), to see the base 64 encoded / xml "word document" as a string, which should be on the right path hopefully.

Any feedback / suggestions / ideas would be most welcome.

Also, is there any particular garbage collection stuff I need to look out for? I know I'm playing with COM to some degree.


Private Sub btnClick(ByVal s As Object, ByVal e As AddinExpress.MSO.IRibbonControl, ByVal pressed As Boolean) Handles AdxRibbonButton1.OnClick
Me.openAs2007Doc(Me.saveDocToDiskReturnFileLocation(Me.WordApp))
End Sub

'''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Function saveDocToDiskReturnFileLocation(ByVal doc As Word._Application) As String

Dim filePath As String = Me._docFolder & "\" & System.Guid.NewGuid().ToString() + ".doc"
Me.WordApp.ActiveDocument.SaveAs(CType(filePath, Object))

Return filePath

End Function
'''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Sub openAs2007Doc(ByVal fileName As String)

Dim app As New Microsoft.Office.Interop.Word.Application
Dim oConfirm As Object = False
Dim oReadOnly As Object = True

app.Documents.Open(CType(fileName, Object), oConfirm, oReadOnly)
app.Visible = False
app.WindowState = Microsoft.Office.Interop.Word.WdWindowState.wdWindowStateMinimize
app.DisplayAlerts = Microsoft.Office.Interop.Word.WdAlertLevel.wdAlertsNone

Dim myForm As New Form1
myForm.Activate()
myForm.Visible = True
myForm.TextBox1.Text = app.ActiveDocument.WordOpenXML

app = Nothing

End Sub

'''''''''''''''''''''''''''''''''''''''''''''''''''
Posted 12 Jun, 2007 18:08:09 Top
Sergey Grischenko


Add-in Express team


Posts: 7187
Joined: 2004-07-05
Hi Eric.

Are you going to support Word 2007 only? If so, the WordOpenXML property will work properly. However this property was introduced in Word 2007 only.
In early versions it doesn't exist.
Posted 13 Jun, 2007 08:16:53 Top
Eric Kaufman




Posts: 5
Joined: 2007-06-08
Okay, so what are my options for getting an XML document out of Word then, let's say on Office 2003?
Posted 19 Jun, 2007 18:52:26 Top
Sergey Grischenko


Add-in Express team


Posts: 7187
Joined: 2004-07-05
Hi Eric.

As I said earlier, you can save documents to temporary files and then transfer them to a format you need.
Posted 20 Jun, 2007 08:23:53 Top
Rory Hallman




Posts: 3
Joined: 2007-10-03
Sergey,

Do you have an example of how to transfer a word 2003 document to xml format?
Posted 03 Oct, 2007 15:15:04 Top