locked
Microsoft.Office.Interop.Word RRS feed

  • Question

  • User-1542157572 posted

    Hi all i am working on the Microsoft.Office.Interop.Word COM component. I need to find the line number of a found text. How do i go about doing this?. Please help me.

    Thursday, June 12, 2008 6:17 AM

All replies

  • User-319574463 posted

    Are you trying to use Word from an ASP.NET web page. It can be done (I have done it), but you will great problems setting all the required permissions to get it to work. This is why Word is not supported running behind an ASP.NET web page.

    Thursday, June 12, 2008 7:42 AM
  • User-1542157572 posted

    I am using windows application.I have the following requirements

    1. I need to find the text in the MS word document.

    2. I need to get the line number of that text.

    3. In the next line i need to write some text.

    Thursday, June 12, 2008 11:54 PM
  • User-319574463 posted

    >I am using windows application.I have the following requirements

    Are you using Winforms or Webforms?

    Which version or versions of word are you using? 

    Sunday, June 15, 2008 9:26 AM
  • User-1542157572 posted

    HI i am using Winforms, MS office word 2003 version, and i am using the namespace MS word 11.0 object library. Please do help me in resolving this issue..its very urgent

    Monday, June 16, 2008 5:42 AM
  • User-319574463 posted

    >Hi I am using Winforms

    As you are using WinForms, you should be posting your question on the MSDN forums rather than here where it is for WebForms.

    One of the problems you will probably face is that of Com+ activation permissions. To set these, go:

    1. Start, Run, enter MMC, OK
    2. File, Add/Remove Snap-in
    3. Click Add
    4. Select Component Services, Add, Close
    5. Click OK
    6. Save you MMC for alter use.

    When you get a COM error, not the object class and find it in the COM explorer. Once found you can fix the permissions.

    I urge you to do this using a virtual machine if at all possible as you if make a copy of the VM before you start, you can very easily roll back.

     

    Monday, June 16, 2008 7:20 AM
  • Monday, June 16, 2008 7:27 AM
  • User-319574463 posted

    ...or even at http://msdn.microsoft.com/de-de/library/microsoft.office.interop.word.find(VS.80).aspx

     

    Also look at http://www.codeproject.com/KB/cs/Word_Automation.aspx - this has an example of a programmatic find

    Monday, June 16, 2008 7:31 AM
  • User-1542157572 posted

    How do i know about the object model on the word document

     

    Monday, June 16, 2008 8:16 AM
  • User-319574463 posted

    >How do I know about the object model on the word document

    You will need to explore the Microsoft Documentation.

    Have you tried the example code from CodeProject? 

    Tuesday, June 17, 2008 7:28 AM
  • User-1542157572 posted
    string str = @"D:\Canarys\Project\doc1.doc";

    object str1 = str;

    object missing = Missing.Value;

    Word.ApplicationClass wordApp = new Word.ApplicationClass();

    Word.Document doc = wordApp.Documents.Open(ref str1, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);

    doc.Activate();

    object str2 = "Microsoft.Office.Interop.Word";

    //wordApp.Selection.Find.Execute(ref str2,ref missing,ref missing,ref missing,ref missing,ref missing,ref missing,ref missing,ref missing,ref missing,ref missing,ref missing,ref missing,ref missing,ref missing);

    //MessageBox.Show(Convert.ToString(wordApp.Selection.Find.Execute(ref str2,ref missing,ref missing,ref missing,ref missing,ref missing,ref missing,ref missing,ref missing,ref missing,ref missing,ref missing,ref missing,ref missing,ref missing)));

    //doc.Range(

    //Word.Sections sec1 = doc.Sections;

    //Word.Range rng = sec1.Range;

    //Word.Section sec2 = doc.Sections.Add();

    //Word.InlineShape ils = new Word.InlineShape();

    //ils = doc.InlineShapes.AddPicture(@"C:\Documents and Settings\geethan\My Documents\My Pictures\HLD.JPG", true, true, rng);

    //doc.Save();

    object wdWrap = Microsoft.Office.Interop.Word.WdFindWrap.wdFindStop;

    //object missing = System.Reflection.Missing.Value;

    object replaceAll = Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll;

     

    Word.
    Range storyRange;foreach (Word.Range oRange in doc.StoryRanges)

    {

    storyRange = oRange.Duplicate;

    do

    {

    Word.
    Range findRange = storyRange.Duplicate;if (findRange.Text != "")

    {

    findRange.Find.Text = "Microsoft.Office.Interop.Word";

    //findRange.Find.Replacement.Text = "";

    findRange.Find.Wrap = (Word.WdFindWrap)wdWrap;

    findRange.Find.Execute(

    ref missing, ref missing, ref missing, ref missing, ref missing,

    ref missing, ref missing, ref missing, ref missing, ref missing,

    ref missing, ref missing, ref missing, ref missing, ref missing);

    object unit=Word.WdUnits.wdWord;//.wdCharacter;

    object count = 1;

    object type = Word.WdBreakType.wdLineBreak;

    if (findRange.Find.Found == true)

    {

    //object rng = findRange.GoTo(Microsoft.Office.Interop.Word.WdGoToItem.wdGoToLine,.Duplicate;

     

    findRange.Move(ref unit,ref count);//.MoveEnd(ref uni,ref count);//.Duplicate;//findRange.GoToNext(Microsoft.Office.Interop.Word.WdGoToItem.wdGoToLine);

    findRange.GoToNext(Microsoft.Office.Interop.Word.WdGoToItem.wdGoToLine);

    object rng1 = findRange.Duplicate;

    findRange.InlineShapes.AddPicture(@"C:\Documents and Settings\geethan\My Documents\My Pictures\HLD.JPG", ref missing, ref missing, ref rng1);//.GoToNext(Microsoft.Office.Interop.Word.WdGoToItem.wdGoToLine);

    }

    }

    storyRange = storyRange.NextStoryRange;

    }
    while (storyRange != null);

    }

    Wednesday, June 18, 2008 3:32 AM
  • User-319574463 posted

    Geetha What do you need done with your code listing?

     

    Wednesday, June 18, 2008 7:08 AM
  • User-1542157572 posted

    HI can u please tell me how to use this word PIA in ASP.net web page.Its very urgent

    Tuesday, July 8, 2008 12:27 AM
  • User-319574463 posted

     Did you install the PIA according to the instructions?

    Have you run Word interactively on the Server? 

    Tuesday, July 8, 2008 1:59 AM
  • User-1542157572 posted

    Yes i downloaded the oxppia.exe and run them and then installed register.bat this made the Microsoft.Office.Interop.Word.dll to install in GAC. But  i am not able to refer this in the website project. Please do help me

    Tuesday, July 8, 2008 2:17 AM
  • User-319574463 posted

     First question - does Word run on that machine?

    Second question - when you add the reference within the project, are selecting the COM tab and waiting for it to populate?

    I suggest that you put the Word Document reasding code into a separate class project. This will allow you test it using a WinForms test harness and / or unit test it before calling from your web project.

    Tuesday, July 8, 2008 2:40 AM
  • User-1542157572 posted

    Yes the word application(2003) is running on this machine.

    Yes i am referening the Microsoft object 11.o object library from COM tab.

    I created new project(Winforms) and when i  add the reference i got Microsoft.Office.Core dll.

    Tuesday, July 8, 2008 2:51 AM
  • User-319574463 posted

     What are the symptoms of it not working?
    Have you checked the event logs? If there is an activation problem, it should be logged in the even t log.

    Tuesday, July 8, 2008 2:55 AM
  • User-1542157572 posted

    When i refer from the dll from using browse tad and refer the Microsoft.Office.Interop.Word dll from the location where i extracted the oxppia file then the reference will be added to windows application but not to the website.

    Tuesday, July 8, 2008 3:03 AM
  • User-1542157572 posted

    HI  i am able to refer to the dll to my website also my problem is resolved now. One more thing do i need to always download this oxppia.exe and then register them or will these PIAs be installed during word installation if so how do i go about doing this. Please let me know about this since in the server i just refffered to the COM object directly without downloading and register them

    Tuesday, July 8, 2008 5:34 AM
  • User-319574463 posted

     I do not recall the PIA being automatically installed when Word is installed. For each installation, you will need to specify at the minimum:

    1. Install Word
    2. Install PIA
    3. Start up word on the machine.
    4. Check for COM+ permission messages in the event logs. 
    My recollection for setting this up is the need to set a lot of permissions in the COM+ explorer. This is best done using a virtual machine as you can easily revert to the start situation and repeat the process until you have reduced the setup to the essential changes.
    Tuesday, July 8, 2008 7:30 AM
  • User-1542157572 posted

    Hi thank you i am able to refer to the dll(Microsoft office 11.0 object library) finally. Now i have got an exception in the code saying "the command is not available" I ill send you the code i didn get this.

    1    protected void btnUpload_Click(object sender, EventArgs e)
    2        {
    3            if (FileUpload.HasFile)
    4            {
    5                if (FileUpload.PostedFile.FileName.EndsWith(".doc"))
    6                {
    7                    string strFilename = FileUpload.PostedFile.FileName;
    8                    object objFileName=strFilename;
    9                    object missing =Missing.Value;               
    10                   Word.ApplicationClass wordApp = new Word.ApplicationClass();
    11                   Word.Document doc = wordApp.Documents.Open(ref objFileName, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
    12                   doc.Activate();
    13                   object objWrap=Word.WdFindWrap.wdFindStop;
    14                   Word.Range storyRange;
    15                   foreach (Word.Range rng in doc.StoryRanges)
    16                   {
    17                       storyRange = rng.Duplicate;
    18                       do
    19                       {
    20                           Word.Range findRange = storyRange.Duplicate;
    21                           if (findRange.Text != "")
    22                           {
    23                               findRange.Find.Text = "Yours truly,";                        
    24                               findRange.Find.Wrap=(Word.WdFindWrap)objWrap;
    25                               (Here i will get the Exception)findRange.Find.ExecuteOld(ref missing, ref missing, ref missing, ref missing, ref missing,ref missing, ref missing, ref missing, ref missing, ref missing,ref missing);//, ref missing, ref missing, ref missing, ref missing);
    26                               if (findRange.Find.Found == true)
    27                               {
    28                                   object objUnit = Word.WdUnits.wdWord;
    29                                   object count=1;
    30                                   object rngAdd = findRange.Duplicate;
    31                                   findRange.Move(ref objUnit, ref count);
    32                                   findRange.GoToNext(Word.WdGoToItem.wdGoToLine);
    33                                   findRange.InlineShapes.AddPicture(@"C:\Documents and Settings\support\Desktop\canaryslogo.jpg", ref missing, ref missing, ref rngAdd);
    34                               }
    35                           }
    36                           storyRange = findRange.NextStoryRange;
    37                       }while(storyRange!=null);
    38                   }
    39               }
    40           }
    41   }
    
    Tuesday, July 8, 2008 8:38 AM
  • User-319574463 posted

     Lines 8 through 38 need to be within a method in a class project

    Tuesday, July 8, 2008 9:14 AM
  • User-1542157572 posted

    This error i got when i open a document which is protected. When i remove the protection it works fine. Is there any method to execute this without removing the protection. Or can i place this image in a place which is a macro(image)

     

    Wednesday, July 9, 2008 2:46 AM
  • User-319574463 posted

     By protected, do you mean the file is read-only?

    Wednesday, July 9, 2008 2:54 AM
  • User-1542157572 posted

    No not readonly my document consists of formfields and macros(image). This formfields are enabled only when we protect the document based on filling in form restriction. I now want to add an image to the Image macro programmatically.

    Wednesday, July 9, 2008 3:20 AM
  • User-319574463 posted

     If it is internal to the document, then you will need to further delve into the Word Object Model and either post a further question on this forum and/or post a question on one of the MSDN forums.

    Wednesday, July 9, 2008 3:55 AM
  • User-1542157572 posted

    Hi the above code which i have snet is inserting an image above the text. Can u please let me know how  to insert it below a text.

    Thursday, July 10, 2008 1:16 AM
  • User-319574463 posted

     Again you will need to delve into the word Object model and find a way to move the insertion point down one line.

    Thursday, July 10, 2008 1:56 AM