Share via

Getting 'Command failed' System.Runtime.InteropServices.COMException when calling Window.GetPoint()

Priyank Kotiyal 6 Reputation points
2022-04-22T12:51:17.383+00:00

Hi nerds,
I am facing issue when trying to call Window.GetPoint() function for getting the points of a range. And this is leading to word getting hang for sometime.

Function call : window.GetPoint(out int left, out int top, out int width, out int height, range);
The error : Message : Command failed, Stack Trace : at Microsoft.Office.Interop.Word.Window.GetPoint(Int32& ScreenPixelsLeft, Int32& ScreenPixelsTop, Int32& ScreenPixelsWidth, Int32& ScreenPixelsHeight, Object obj)

The use case we are using is we call this function during the scroll through the document and it starts getting hanged after sometime of scrolling.
We call it for some ranges in visible area plus some ranges above and below and I guess this exception is coming for fetching points of non visible ranges.

So my question is how can we handle this gracefully?

Also why word throws the error? Shouldn't it returns some -ve points or something that indicates range is not visible instead of error?
Also if there any property/method that we can use before calling it to only call if its visible/valid.

I have seen people suggested to scrollIntoView() but we don't want user to see scrolling and unexpected behavior.

How we can handle this error so that this doesn't causes word hang?

Thanks in advance!
Priyank

Microsoft 365 and Office | Development | Other

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.