Word Bookmarks and XMLNode/s

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

Word Bookmarks and XMLNode/s
 
Rafal Piotrowski


Guest


Hi there,
I have never wrote a Word Addin nor any core for Office apps at all, so forgive my ignorance.

I will be starting a new project that will have to do produce a Word addin. The plugin will have to import data into Word document (using external web services to get data), change the word document according to some criteria and then export data into XML. User will be able to add tables (taking data from web services or other datasources) and charts and place them into word document. The issue here is that tables and charts will have to be as images, and also if a user request to refresh the chart/table the addin will have to recreate it using most recent data.

My question being; How can I programatically add bookmarks or XMLNodes to word document using your product? Is it in any way related to AddIn Express or is it totaly Office related question.

BTW, The target office is 2002, XP and 2003.

Hope it is clear.

Many thanks in advance,
Rafal
Posted 20 Jun, 2005 05:28:28 Top
Sven Heitmann




Posts: 193
Joined: 2004-08-23
Hi Rafal,

to add Bookmarks to a Document you have to use the method
Application.ActiveDocument.Bookmarks.Add ...

This is totaly Office related.

Perhaps there is some easy way using some VBA methods, I don't know.
However you could also use .NET. There is some library to create Charts:
http://zedgraph.sourceforge.net/

ADX can help you to easier interact with Word when you develop your product as COM-Addin. All other actions are performed via the Word Object Library, wich is the same VBA provides. The benefit from using .NET is the access to other APIs like XML, ADO.NET, Streams to the Internet etc.

You could also manage Word from a .NET Application.
Collect the data in your Application, create a Word instance and perform the action you need.
This way Word is used in the background.

It is your choice which fits better to your problem.
Best regards,

Sven Heitmann
Posted 20 Jun, 2005 05:50:18 Top
Rafal Piotrowski


Guest


Hi Sven,

So if I will be using Office 2003 I will have to reference the correct interop dll and then use Word/Excel objects? In other words, I will be able to manipulate the document or spreadsheet objects just like I would be using code behind the document itself?
And as long as I do not use any version specific functionality, my addin will be office independent; Is that correct?

regards
rp
Posted 20 Jun, 2005 06:14:07 Top
Sergey Grischenko


Add-in Express team


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

In order to support Office 2000, 2002 and 2003 in one add-in you should use the Interop.Office.dll created for Office 2000. As long as you don't use any version specific functionality your add-in will work in any version of Office.
Posted 20 Jun, 2005 07:52:29 Top
Sven Heitmann




Posts: 193
Joined: 2004-08-23
Hi Rafal,

like Sergey mentioned you have to use the interop for the lowest Office Version you want to support. In your case for Office 2000. Then it will be Version "independent" and should work vor 200, XP and 2003.
However you should know that Office 2003 has some new Features specially for XML.

The Code you are writting, looks like in VBA, the algorithm and Objects are the same. You have only some limitation (from language scope) when using C#, that means no "with" and no optional Parameters like in VB.NET or VBA
Also you have to delete the references to COM-Objects to prevent memory leaks.

You can take a look into the ADX examples and comparethem to the VBA code you would use.
Best regards,

Sven Heitmann
Posted 20 Jun, 2005 08:50:23 Top
Rafal Piotrowski


Guest


great

yes, I have seen that with C# one will have to use special value parameter if want to ommit passing value for it. I also know that there are a lot of functions that takes more then 6 params, therefore making your C# function call unreasticly long and ugly. In this case VB.NET (although I don like VB) seems to be more siuted for that kind of software. Can you share your experience and point me to the correct language I should use. I do know C# pretty well, so learning VB.NET would be not so hard since the APIs are the same.

Regards
rp
Posted 20 Jun, 2005 10:28:37 Top
Sven Heitmann




Posts: 193
Joined: 2004-08-23
To be honest I've never used VB.NET so I can not say if it is really better to work with.
If you have time to take a look if it fits better to get a readable code then try it.
I've worked with C# and it is a little bit more code as in VBA but not that much. You have to use Type.Missing a lot. What I really dislike most of the functions in Word need a ref parameter (often from type object) so you have to declare new objects. I don't know if it is the same when using VB.NET.
However Excel doesn't need ref parameters, so I think it is related to Word.

For example the Code to add a Bookmark in VBA would be

With Application
  .ActiveDocument.Bookmarks.Add("MyBookmark",.Selection.Range)
End With


and the Code in C#

//oApplication is Definied somewhere else
Word.Document oDocument = null;
Word.Bookmarks oBookmarks = null;
Word.Selection oSelection = null;
object oRange = null;

oDocument = oApplication.ActiveDocument;
oBookmarks = oDocument.Bookmarks;
oSelection = oApplication.Selection;
oRange = oSelection.Range;
oBookmarks.Add("MyBookmark",ref oRange);

//release COM-Objects to prevend Memory Leaks
//without Word will stay in Memory sometimes, even when closed
Marshal.ReleaseComObject(oRange);
Marshal.ReleaseComObject(oSelection);
Marshal.ReleaseComObject(oBookmarks);
Marshal.ReleaseComObject(oDocument);
Best regards,

Sven Heitmann
Posted 21 Jun, 2005 01:13:41 Top
Rafal Piotrowski


Guest


Hi Sven,
Many thanks for the example.

BTW did MS said that they are trying to make things easier.... well doesn't look that way.

Regards,
rp
Posted 21 Jun, 2005 04:07:54 Top
Sven Heitmann




Posts: 193
Joined: 2004-08-23
I think its pretty easy to develop a COM Addin but thats the magic of ADX ;)

In a few years when MS releases a Office with integrated support for C# then things will be easier ;)

At the moment I try to stay with VBA where possible because it is the nativ macro language for Office. When you take a look to the functions with about 10-15 parameters you see what I mean.
My Clients complain about the performance of Word... the Startup of the .NET Runtime and JIT Compilation of the Addin and Assemblies is really slow... wit hVBA you don't have such problems (however you have some other)

I compared the startuptime with and without COM-Addin (C#)
Without it is up in less than 1s, with Addin first time 8s, than 2s.
I am not sure how to increase the performance... I know with ngen you can create pre JIT Compiled native images of an Assembly...

However there are some missing features to COM Addins from Office e.g. you can not bind Shortcuts to a COM-Addin function so you need some Callback...
I don't want to talk the Comamndbars saved in Templates and the trouble I get specially to remove them from mulituser PCs ;)

You see there is a lot to do and I thing that Yukon provides a way to run C# Code directly inside the database is a good sign that Microsoft uses it's own product in some way :)

At least we will see what the next Office and Longhorn will bring to .NET Developers.

Just my 2 Cents.
Best regards,

Sven Heitmann
Posted 21 Jun, 2005 04:47:16 Top