How to get cursor position in word using c++, without hidden chars

  • Thread starter Kristian de Lichtenberg
  • Start date
K

Kristian de Lichtenberg

Okay,so first of all, sorry if this is the wrong forum. If anyone can direct
me to a better one, I'll swiftly remove the thread the best I can :)

Anyway, heres the thing.

I work on a program that reads text from word for handicapped school
children. Since it also reads from other programs, it keeps the text of each
line in a buffer, and reads from where the cursor is.

Now, to the question.

In word, I can call Selection.Range.Start in the word api, but this returns
the position of the cursor with hidden chars. This confuses my program and I
need the cursor position wihout the hidden text, since they will not be read
anyway.

Now, as I can get the text without the hidden chars, I thought I'd just get
the text before the cursor, and count the length of it. This however I have
not been able to, as all calls to setrange have failed.

So to repeat the key question here: How do I get the number of chars on the
current line before the cursor, not including hidden chars.

Oh and in C++ without MFC and without word.h

I suppose there is a simple solution, but screaming and kicking and yelling
at the screen so far produced very little tangible results.. :)

Here's an example. Its called with start of current text line, and current
position with hidden chars. The point was then to extract the text between
these (as I can get text withouht hidden chars) and then count the length.
However, setrange doesn't work...

int CAppHandler::getRangeTextLength(int start, int end, int previousend){
int res=-1;

//VARIANT vtParam; VariantInit(&vtParam);
VARIANT vtResult; VariantInit(&vtResult);
//VARIANT vtResult1; VariantInit(&vtResult1);
VARIANT vtSelection; VariantInit(&vtSelection);
//VARIANT vtActiveWindow; VariantInit(&vtActiveWindow);

VARIANT varStart, varEnd;

VariantInit(&varStart);
VariantInit(&varEnd);

String text="";

HRESULT hr;

AuxLog::log(T("getting range"));

hr = getPropertyPath(T("ActiveDocument.Range"), &vtSelection);
if (FAILED(hr)) goto cleanup;

LPDISPATCH rangePtr2 = vtSelection.pdispVal;
AuxLog::log(T("getting range - duplicating: "));

hr = GetProperty(rangePtr2,L"Duplicate" ,&vtSelection);
if (FAILED(hr)) goto cleanup;
LPDISPATCH rangePtr = vtSelection.pdispVal;

varStart.lVal = start;
varEnd.lVal = end;

AuxLog::log(T("getting range - setting range: "))
hr=autoWrap(rangePtr,T("ActiveDocument.Range.SetRange"),DISPATCH_METHOD,&vtResult,2,varEnd, varStart);
if (FAILED(hr)) goto cleanup;

AuxLog::log(T("getting range - getting text: "));
text = getPropertyString("text",rangePtr);
res = text.length();


varStart.lVal = end;
varEnd.lVal = previousend;

hr=autoWrap(rangePtr,T("SetRange"),DISPATCH_METHOD,&vtResult,2,varStart,varEnd);
if (FAILED(hr)) goto cleanup;

cleanup:
VariantClear(&vtSelection);
//VariantClear(&vtParam);
VariantClear(&vtResult);
VariantClear(&varStart);
VariantClear(&varEnd);
//VariantClear(&vtActiveWindow);
return res;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top