PDA

View Full Version : word automation in late binding using C#



saisreelu
02-24-2005, 02:17 AM
hi,

i need a code for word 97 automation using C#.
:think:



plz...take it as highpriority n send me.



:bug:

TonyJollans
02-24-2005, 08:02 AM
Hi saisreelu,

Welcome to VBAX!

Do please be aware that everybody who helps here by providing answers does so on a completely voluntary basis; they have their own lives and their own priorities. You are asking for free help - have the grace to accept any you get.

As for your question, COM Addins only came along with Word 2000 and I'm afraid I don't know how to do it with 97. Can you give any more details of what you need to achieve - it may be that what you want can be done in another way.

Howard Kaikow
02-24-2005, 08:28 PM
Hi saisreelu,

Welcome to VBAX!

Do please be aware that everybody who helps here by providing answers does so on a completely voluntary basis; they have their own lives and their own priorities. You are asking for free help - have the grace to accept any you get.

As for your question, COM Addins only came along with Word 2000 and I'm afraid I don't know how to do it with 97. Can you give any more details of what you need to achieve - it may be that what you want can be done in another way.

One can automate Office via C# without using a COM addin.

However, MSFT only released PIAs for Word 2002 and up.
I would not recommend trying to use a .NET language with anything earlier than Office XP, and, well, most folkes know how I feel about Office 97.,

Automating Office 97 would be better done with VB 6.

If you must use the .NET languages, then I suggest upgrading to Office 2003 and also getting VSTO (Visual Studio Tools for Office).

saisreelu
02-25-2005, 01:21 AM
hi,
through earlybinding i started.i god error Query interface failed for Word._Application() failed.
this is the problem.

through latebinding properties are not exposed.

can u give any suggestion

saisreelu
02-25-2005, 01:24 AM
private Type WordType;

private Object WordApp;

private Object WordSelection ;

private Object[] strTemp;

private Object[] objTrue ;

private Object WordFont;

private Object WordView;

private Object WordParagraphFormat ;

Object[] objMissing ;

private Object WordDoc;

private Object Doc;

strTemp = new Object[1];

objTrue = new Object[1];

objMissing = new Object[1];

objMissing[0] = System.Reflection.Missing.Value;

WordType = System.Type.GetTypeFromProgID("Word.Application");







//late bind MS Word COM

WordApp = Activator.CreateInstance (WordType);



Object[] objFalse = new Object[1];

objFalse[0] =true;

WordType.InvokeMember("Visible", BindingFlags.SetProperty, null, WordApp,objFalse);

//generate one document

WordDoc = WordApp.GetType().InvokeMember( "Documents",

BindingFlags.GetProperty, null, WordApp, null );

Doc = WordDoc.GetType().InvokeMember( "Add",

BindingFlags.InvokeMethod, null, WordDoc, null );



















public void InsertHeader()

{



Object WordActiveWindow = WordApp.GetType().InvokeMember( "ActiveWindow", BindingFlags.GetProperty, null, WordApp, null );

Object WordActivePane = WordApp.GetType().InvokeMember( "ActivePane", BindingFlags.GetProperty, null, WordActiveWindow, null );

Object WordView = WordApp.GetType().InvokeMember( "View", BindingFlags.GetProperty, null, WordActivePane, null );



Object[] objWdSeedCurrentPageHeader = new Object[1];

objWdSeedCurrentPageHeader[0] = 9;

WordType.InvokeMember("SeekView", BindingFlags.SetProperty,

null, WordView, objWdSeedCurrentPageHeader);



Object WordParagraphFormat =

WordSelection.GetType().InvokeMember(

"ParagraphFormat", BindingFlags.GetProperty, null,

WordSelection, null );

Object[] objWdAlignParagraphCenter = new Object[1];

objWdAlignParagraphCenter[0] = 1;

WordType.InvokeMember("Alignment", BindingFlags.SetProperty,

null, WordParagraphFormat, objWdAlignParagraphCenter);

strTemp[0] = "I am Head around here.";

WordType.InvokeMember("TypeText", BindingFlags.InvokeMethod,

null, WordSelection,strTemp);

}



Here The problem is it is not exposing the properties .so I ?ve a problem to insert the images ,tables n all.







This is through Early Binding i.e Including the reference MicroSoft Word 8.0 Object library (Type library 8.0).

private Word._Application word;

private Word._Document doc;

Private Word.Paragraph para1;

private Word.Paragraph para2;

Word.Range range ;

Word.Table table;

private Word.Shape myshape;

Word.InlineShape shape;

object ranger ;

private object mymissing =

System.Reflection.Missing.Value;

private object myeod = "\\endofdoc";

private object falseref = false;

private object trueref = true;



public void CreateDoc()

{

//Starting Word

word = new Word.ApplicationClass();

word.Visible= true;



//Creating a new document

doc= word.Documents.Add(ref mymissing,ref mymissing);

}





public void InsertImage(String path,float height,float

width,float brightness,float contrast)

{



range=doc.Bookmarks.get_Item(ref myeod).Range;

ranger=doc.Bookmarks.get_Item(ref myeod).Range;

shape=range.InlineShapes .AddPicture (path,ref

falseref,ref trueref,ref ranger);

shape.Height= word.InchesToPoints(height);

shape.Width=word.InchesToPoints(width);

if(brightness!=0)

shape.PictureFormat.Brightness =brightness;



if(contrast!=0)

shape.PictureFormat .Contrast =contrast;

range= doc.Bookmarks.get_Item(ref myeod).Range;

range.InsertParagraphAfter();

}





I am getting this type of error:

An unhandled exception of type 'System.InvalidCastException' occurred in sample.exe

Additional information: QueryInterface for interface Word._Application failed.

I tried to type this error in Google also.

Howard Kaikow
02-25-2005, 03:21 AM
hi,
through earlybinding i started.i god error Query interface failed for Word._Application() failed.
this is the problem.

through latebinding properties are not exposed.

can u give any suggestion

I expect the problems are due to there being no PIA for Word 97.

There are issues with the Application object in earlier versions of Word.

There is an MSDN article titled Programming Microsoft Word 2002 and Excel 2002 with Microsoft Visual C#


I do not know the URL, but you should take a look ar that article.

Also, see http://support.microsoft.com/default.aspx?scid=kb;en-us;Q302902

saisreelu
02-25-2005, 05:34 AM
hi,
i will implement wat u said.
thanks for ur reply.
bye