Outlook, adding text to a mail at cursor position

Add-in Express™ Support Service
That's what is more important than anything else

Outlook, adding text to a mail at cursor position
 
Bus




Posts: 7
Joined: 2016-07-26
Hi all,

I want to code a text block system for Outlook.

What is the correct way to insert some text at the cursor position of a mail?

Best regards

Michael
Posted 26 Jul, 2016 05:48:17 Top
Andrei Smolin


Add-in Express team


Posts: 14137
Joined: 2006-05-11
Hello Michael,

You retrieve Inspector.WordEditor and cast it to Word.Document. Then you find the current cursor position using Word.Document.Application.Selection.Range. Finally, you use the properties/methods that Word.Range provides to insert the text.

Regards from Belarus (GMT+3),

Andrei Smolin
Add-in Express Team Leader
Posted 26 Jul, 2016 06:37:04 Top
Bus




Posts: 7
Joined: 2016-07-26
Thank you, it works:


procedure TAddInModule.AddText(AText: string);
var
  LInspector: _Inspector;
  LDocument: WordDocument;
begin
  LInspector := OutlookApp.ActiveInspector;
  if Assigned(LInspector) then begin
    LDocument := LInspector.WordEditor as WordDocument;
    LDocument.Application.Selection.InsertAfter(AText);
    LDocument := nil;
  end;
  LInspector := nil;
end;



But when I don't open a separate Window, but answer directly in the Outlook Main Window, LInspector is nil. _Explorer doesn't have the property WordEditor.

Do you have an idea?
Posted 26 Jul, 2016 09:05:06 Top
Andrei Smolin


Add-in Express team


Posts: 14137
Joined: 2006-05-11
Michael,

Try getting the selected item and call GetInspector for it. After you modify the item you'll need to save it.

Are you sure that you need to modify the email shown in the PreviewPane? Are you going to undo the modifications at some stage? If so, how?

Regards from Belarus (GMT+3),

Andrei Smolin
Add-in Express Team Leader
Posted 26 Jul, 2016 09:15:03 Top
Bus




Posts: 7
Joined: 2016-07-26
Try getting the selected item and call GetInspector for it.


Do you have a delphi code example?

I didn't have found a single occurance of GetInspector in your complete source code. Is it a last binding function?
Posted 27 Jul, 2016 08:22:54 Top
Andrei Smolin


Add-in Express team


Posts: 14137
Joined: 2006-05-11
Michael,

GetInspector belongs to the object model of Outlook.

Please check this google search.

Regards from Belarus (GMT+3),

Andrei Smolin
Add-in Express Team Leader
Posted 27 Jul, 2016 08:32:03 Top
Bus




Posts: 7
Joined: 2016-07-26
Ok, works.

Complete code:


procedure TAddInModule.AddText(AText: string; AOffset: integer = 0);
var
  LInspector: _Inspector;
  LExplorer: _Explorer;
  LDocument: WordDocument;
  LSelection: Selection;
  LMail: _MailItem;
  LItem: IDispatch;
  i: integer;
begin
  try
    LInspector := OutlookApp.ActiveInspector;
    if not Assigned(LInspector) then begin
      LExplorer := OutlookApp.ActiveExplorer;
      if Assigned(LExplorer) then begin
        LSelection := LExplorer.Selection;
        if Assigned(LSelection) then begin
          if LSelection.Count > 0 then begin
            LItem := LSelection.Item(1);
            if Assigned(LItem) then begin
              LItem.QueryInterface(IID__MailItem, LMail);
              LInspector := LMail.GetInspector;
            end;
          end;
        end;
      end;
    end;
    if Assigned(LInspector) then begin
      LDocument := LInspector.WordEditor as WordDocument;
      LDocument.Application.Selection.InsertAfter(AText);
      i := LDocument.Application.Selection.Start + Length(AText) + AOffset;
      LDocument.Application.Selection.SetRange(i, i);
      LDocument := nil;
    end;
  finally
    LInspector := nil;
    LExplorer := nil;
    LDocument := nil;
    LSelection := nil;
    LMail := nil;
    LItem := nil;
  end;
end;

Posted 27 Jul, 2016 09:14:31 Top
Andrei Smolin


Add-in Express team


Posts: 14137
Joined: 2006-05-11
Thank you!

Regards from Belarus (GMT+3),

Andrei Smolin
Add-in Express Team Leader
Posted 27 Jul, 2016 09:23:51 Top
Bus




Posts: 7
Joined: 2016-07-26
Ok, new problem, with the folling line in the code above:

              LInspector := LMail.GetInspector; 


I have added the same TadxOlForm as a pane to the Explorer and the Inspector and call with buttons from there AddText. Works.



If I call AddText from the Inspector, it works and it doesn't touch the functionality in the Explorer. All is fine.

If I call AddText from the Explorer, AddText works. But If I add Text while no Inspector window is open, new opened Inspector Windows appear without the pane. The problem doesn't occur if at least one Inspector window is open while call AddText in the explorer.

The problem is caused by the line "LInspector := LMail.GetInspector;". If I delete this line, the error doesn't occur (but then AddText doesn't work in the Explorer).
Posted 03 Aug, 2016 05:40:17 Top
Andrei Smolin


Add-in Express team


Posts: 14137
Joined: 2006-05-11
Hello Michael,

We've reproduced the issue. It is now filed under #9325 in our bug-tracking DB. When I have news on this issue, I'll let you know. This may occur in 2-4 weeks.

Regards from Belarus (GMT+3),

Andrei Smolin
Add-in Express Team Leader
Posted 03 Aug, 2016 09:10:13 Top