Consulting

Results 1 to 7 of 7

Thread: word automation in late binding using C#

  1. #1

    word automation in late binding using C#

    hi,

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




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




  2. #2
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    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.
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  3. #3
    VBAX Mentor
    Joined
    Sep 2004
    Location
    Nashua, NH, USA
    Posts
    489
    Location
    Quote Originally Posted by TonyJollans
    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).

  4. #4
    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

  5. #5
    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 ;

    privateobject mymissing =

    System.Reflection.Missing.Value;

    privateobject myeod = "\\endofdoc";

    privateobject falseref = false;

    privateobject trueref = true;



    publicvoid CreateDoc()

    {

    //Starting Word

    word = new Word.ApplicationClass();

    word.Visible=
    true;



    //Creating a new document

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

    }





    publicvoid 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.


  6. #6
    VBAX Mentor
    Joined
    Sep 2004
    Location
    Nashua, NH, USA
    Posts
    489
    Location
    Quote Originally Posted by saisreelu
    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...;en-us;Q302902

  7. #7

    thanks i will implement

    hi,
    i will implement wat u said.
    thanks for ur reply.
    bye

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •