question

venkateshpadmanabhan-5594 avatar image
0 Votes"
venkateshpadmanabhan-5594 asked JackJJun-MSFT edited

Error: Requested member of collection does not exist

Hi.
I am trying to replace the content of a template word file and save the file as a new file. This is a console application

I am using VS 2019 and C#. I have added the Microsoft.office.Interop.word dll.

Application App = new Application();
Document WordDoc;
WordDoc = App.Documents.Open(filepath); // This statement seems to be never ending . The file is 97-2003 word format file

WordDoc = App.Documents.Add(filepath); // Tried another way
WordDoc.Bookmarks["ABC"].Range.Text = "Test1" ; // want to replace the content ABC with Test1

I am not able to read the contents of the file . How to read the contents of the file and replace few text.

I have tried the Bookmark option, but getting error as system.runtime.interopservices.comexception - The requested member of the collection does not exist

How to fix this? Thanks

dotnet-csharp
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @venkateshpadmanabhan-5594
As the tag "office-word-itpro" focuses more on general issues of Word client, I would remove it.
Thanks for your understandings.

0 Votes 0 ·

@venkateshpadmanabhan-5594, based on your code, it seems that there is no bookmark called ABC in your doc file, you could try the following code to check if you have the bookmark.

   Application App = new Application();
         Document WordDoc;
         WordDoc = App.Documents.Open(@"1.doc"); // This statement seems to be never ending . The file is 97-2003 word format file
         var result = WordDoc.Bookmarks.Cast<Bookmark>().Where(i => i.Name == "ABC");
         if(result.Count()==0)
         {
             Console.WriteLine("there is no bookmark called ABC");
         }


0 Votes 0 ·

0 Answers