dinotom
dinotom

Reputation: 5162

cant set bookmark range in C# version using Interop.Word

I am converting a vb.net set of helpers to c#. The following snippet works fine in VB.Net

para2 = oDoc.Content.Paragraphs.Add(oDoc.Bookmarks.Item("\endofdoc").Range)

where para2 is oftype Word.Paragraph

the converted c# code to set the bookmark range which i see in numerous articles is

var bookmarkRange= oDoc.Bookmarks.get_Item((object)"\\endofdoc").Range;
var para2 = oDoc.Content.Paragraphs.Add(bookmarkRange);

ReSharper says this needs to use an indexed property, which when changed converts that to

var bookmarkRange = oDoc.Bookmarks.Item[(object)"\\endofdoc"].Range;

Neither version builds, the error is

Error 1 'Microsoft.Office.Interop.Word.Bookmarks' does not contain a definition for 'Item' and no extension method 'Item' accepting a first argument of type 'Microsoft.Office.Interop.Word.Bookmarks' could be found (are you missing a using directive or an assembly reference?) F:\BATLGroup\AzureStorageContainer\AzureStorageContainer\AzureStorageContainer\AzureHelpers\OfficeAppHelpers.cs 19 44 AzureStorageContainer

I am referencing

Assembly Microsoft.Office.Interop.Word C:\Program Files (x86)\Microsoft Visual Studio 14.0\Visual Studio Tools for Office\PIA\Office15\Microsoft.Office.Interop.Word.dll

Any help appreciated.

Extra Credit:

  do
        {
            bookmarkRange.ParagraphFormat.SpaceAfter = 6;
            bookmarkRange.InsertAfter("A line of text");
            bookmarkRange.InsertParagraphAfter();
        } while (pos >= bookmarkRange.Information[WdInformation.wdVerticalPositionRelativeToPage]);

This section is erring on bookmarkRange.Information. It is saying you cant compare an int and an object. wdVertical.... is supposed to return a number indicating distance from top of page.

Upvotes: 0

Views: 1886

Answers (1)

DanL
DanL

Reputation: 999

The C# equivalent of oDoc.Bookmarks.Item("\endofdoc").Range would be:

oDoc.Bookmarks["\\endofdoc"].Range

... and this is because C# doesn't implement the Item property like VB does, according to this link:

[...] The C# language uses the keyword to define the indexers instead of implementing the Item property. Visual Basic implements Item as a default property, which provides the same indexing functionality.

Update:

The line bookmarkRange.Information[WdInformation.wdVerticalPositionRelativeToPage] actually return a float, so you'll need to cast it to compare it, e.g.:

pos >= (float)bookmarkRange.Information[WdInformation.wdVerticalPositionRelativeToPage]);

That is assuming pos is of a type comparable to float.

Upvotes: 1

Related Questions