LateBindingApi.Core.COMObject does not contain a definition for 'Text'

Jul 10, 2012 at 2:32 PM

Hi there

I'm currently trying to switch from an Interop approach to using NetOffice assemblies for interaction with Word from our C#.NET application. The old approach did work perfectly except for the Office different versions problem which drove me to try out NetOffice in the first place.

We're basically opening a Word document which acts as a template with placeholders with a defined Pattern (e.g. {{Placeholder}} ). The method where I encounter the problem iterates through the StoryRanges found in the document and builds a Dictionary of all placeholders found. For that we use the Range.Find method which with the Interop approach allowed to get through to Parent elements and access the Text property there. See the following code snippet for our solution:

[...]
using Word = NetOffice.WordApi;
using Office = NetOffice.OfficeApi;
using NetOffice.WordApi.Enums;
using LateBindingApi.Core;

[...]

        private Dictionary<string, string> GetPlaceHolders(Word.Document document) {
            Dictionary<string, string> phDict = new Dictionary<string, string>();
            object missing = Type.Missing;
            object replaceNone = WdReplace.wdReplaceNone;

            GotoPage(document, 1);

            foreach (Word.Range tmpRange in document.StoryRanges) {
                tmpRange.Find.Text = PlaceHolderPattern;
                tmpRange.Find.Forward = true;
                tmpRange.Find.MatchWildcards = true;

                tmpRange.Find.Execute(missing, missing, missing,
                                    missing, missing, missing, missing,
                                    missing, missing, missing, replaceNone,
                                    missing, missing, missing, missing);
                while (tmpRange.Find.Found) {
                    if (tmpRange.Find.Parent != null) {
                        if (!phDict.ContainsKey(tmpRange.Find.Parent.Text)) {
                            phDict.Add(tmpRange.Find.Parent.Text, "");
                        }
                        tmpRange.Find.Execute(missing, missing, missing,
                                    missing, missing, missing, missing,
                                    missing, missing, missing, replaceNone,
                                    missing, missing, missing, missing);
                    }
                }
            }
            return phDict;
        }

[...]

This gives me the following error:

'LateBindingApi.Core.COMObject' does not contain a definition for 'Text' and no extension method 'Text' accepting a first argument of type 'LateBindingApi.Core.COMObject' could be found (are you missing a using directive or an assembly reference?)

Could anyone give me a clue as to how I can access this property or if this has not been implemented yet in the NetOffice project?

Many thanks!

Coordinator
Jul 11, 2012 at 10:49 AM
Edited Jul 11, 2012 at 10:50 AM

hello,

the problem is the following code line:

phDict.Add(tmpRange.Find.Parent.Text, "");

the Word.Find class contains a Parent property. the Parent property can be a Word.Selection object or a Word.Range object at runtime and i'm wondering about this code works with fine the Interop Assemblies.

this Parent property is COMObject in NetOffice 1.4.1 and Object in NetOffice 1.5.1 because VB developers can use latebinding kicks.

the easiest way is to convert to Parent at runtime:

phDict.Add((tmpRange.Find.Parent as Excel.Range).Text, "");

BTW: dont forget to dispose your object to keep the memory usage low. now worries, its easy to use: Dispose COM proxies

Sebastian

Jul 17, 2012 at 1:29 PM

Hi Sebastian

many thanks for your reply, this is the solution. I'm using Word tables, so it's Word.Range in my case.

Now I've managed to switch pretty much everything to Word 2003, except of course unsupported features like PDF export.

Cheers
Dominik