How to collect more infos, save the file...?

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

How to collect more infos, save the file...?
 
iubito




Posts: 16
Joined: 2014-06-19
Hi!
Now I have my button in Excel, Word, PowerPoint, Outlook (Explorer and Inspector!)

I want to collect informations:

  • Word : first sentence or paragraph
  • Excel : first cell content
  • PowerPoint : first title
  • Outlook : From / To / Subject / Date


I want to know the document path and name.
I want to save the document, in a temp directory if still new (never saved)
I want to save the mail in a .MSG or .EML format, in a temp directory

I'm starting from the GetInfoString function (complete code below)

I didn't find yet the samples I need for Word/Excel/Powerpoint, but the first strange thing I bump into is for Outlook, with the OleVariant
OleVariant(ISelection.Item(1)).Subject

What are the other properties of the OleVariant object?
Can't find them, seems that OleVariant is a very generic object?

Regards,

Sylvain



function TAddInModule.GetInfoString(): string;
var
  er: ExcelRange;
  IWindow: IDispatch;
  IExplorer: _Explorer;
  IInspector: _Inspector;
  ISelection: Selection;
  S: WideString;
  CRLF: string;
begin
  CRLF := #10#13;
  Result := 'No document window found!';
  try
    // Word raises an exception if there's no document open
    IWindow := HostApp.ActiveWindow;
  except
  end;
  try
    case HostType of
      ohaExcel:
        if IWindow <> nil then
        begin
          try
            er := (IWindow as Excel2000.Window).ActiveCell;
           //relative address
            Result := 'The current cell is: '
              + er.AddressLocal[False, False, xlA1, EmptyParam, EmptyParam];
          finally
            er := nil;
          end;
        end;
      ohaWord:
        if IWindow <> nil then
        begin
          Result := 'The current selection contains '
            + IntToStr(
            (IWindow as Word2000.Window).Selection.Range.Words.Count)
            + ' words';
        end;
      ohaPowerPoint:
        if IWindow <> nil then
        begin
          Result := 'The current selection contains '
            + IntToStr(
            (IWindow as MSPpt2000.DocumentWindow).Selection.SlideRange.Count)
            + ' slides';
        end;
      ohaOutlook:
        begin
          //Fenêtre lecture d'un mail, contact, rdv
          IWindow.QueryInterface(IID__Inspector, IInspector);
          if Assigned(IInspector) then
          begin
            try
              Result := OleVariant(IInspector.CurrentItem).Subject;
            finally
              IInspector := nil;
            end;
          end
          else
          begin
            IWindow.QueryInterface(IID__Explorer, IExplorer);
            //IExplorer := OutlookApp.ActiveExplorer;
            if Assigned(IExplorer) then
            begin
              try
                try
                  ISelection := IExplorer.Selection;
                except
                  ISelection := nil;
                // skip Outlook exception (e.g. the Personal Folders folder)
                end;
                if Assigned(ISelection) then
                begin
                  try
                    if ISelection.Count > 0 then
                    begin
                      S := '';
                      try
                        S := OleVariant(ISelection.Item(1)).Subject;
                      finally
                      end;
                      if S <> '' then
                        Result := 'The subject is:' + CRLF + S;
                    end;
                  finally
                    ISelection := nil;
                  end;
                end;
              finally
                IExplorer := nil;
              end;
            end;
          end;
        end;
    else
      Result := 'The ' + AddinName
        + ' COM Add-in doesn''t support the current host application!';
    end;
  except
  end;
  IWindow := nil;
end;
Sylvain

DEV: Delphi 7, Win XP, Office 2007
TARGET: Win XP-7-2012 x64, Office 2007-2010
Posted 27 Jun, 2014 05:38:32 Top
Andrei Smolin


Add-in Express team


Posts: 18842
Joined: 2006-05-11
Hello Sylvain,

Yes, OleVaraint is a way to hold data of virtually any type, please see http://docwiki.embarcadero.com/RADStudio/XE5/en/Variant_Types.

OleVariant(ISelection.Item(1)).Subject is a way to access the Subject property of the underlying object - ISelection.Item(1) - via late binding.

You need to use the object models of these Office applications, see http://msdn.microsoft.com/en-us/library/aa293363%28v=office.10%29.aspx. Although this page refers to a seemingly outdated Office version, the fact is that current Office versions are almost 100% backward compatible with that version; and Add-in Express uses Office 2000 wrappers. If you ever need to access a property/class missing in that Office version, you'll use late binding.


Andrei Smolin
Add-in Express Team Leader
Posted 27 Jun, 2014 06:28:26 Top
iubito




Posts: 16
Joined: 2014-06-19
Thanks for the links, it helps! :)
OleVariant(ISelection.Item(1)).SenderName
OleVariant(ISelection.Item(1)).To
Sylvain

DEV: Delphi 7, Win XP, Office 2007
TARGET: Win XP-7-2012 x64, Office 2007-2010
Posted 30 Jun, 2014 06:22:46 Top
Andrei Smolin


Add-in Express team


Posts: 18842
Joined: 2006-05-11
You are welcome!


Andrei Smolin
Add-in Express Team Leader
Posted 30 Jun, 2014 06:29:20 Top
iubito




Posts: 16
Joined: 2014-06-19
MailItem has Subject, SenderMail and To.
other items only have Subject.

How can I determinate if my item is a MailItem ?

I tried
if ISelection.Item(I).ClassType.InheritsFrom(MailItem) then

but if yields that ClassType is unknown.
If I add System in the uses, it says me that System is already declared

Regards,
Sylvain

DEV: Delphi 7, Win XP, Office 2007
TARGET: Win XP-7-2012 x64, Office 2007-2010
Posted 30 Jun, 2014 06:48:36 Top
Andrei Smolin


Add-in Express team


Posts: 18842
Joined: 2006-05-11
Sylvain,

QueryInterface. Please see http://www.add-in-express.com/forum/read.php?FID=1&TID=637.


Andrei Smolin
Add-in Express Team Leader
Posted 30 Jun, 2014 07:32:44 Top
iubito




Posts: 16
Joined: 2014-06-19
OK. Got it!
Thanks :)
Sylvain

DEV: Delphi 7, Win XP, Office 2007
TARGET: Win XP-7-2012 x64, Office 2007-2010
Posted 30 Jun, 2014 07:40:58 Top
iubito




Posts: 16
Joined: 2014-06-19
Hello!
Everything is OK now with Outlook, but I can't manage, with Excel / Word / PowerPoint
to get
- file name
- file full path
- file format (XLS, XLSX, CSV...)

Example for Excel :
if (IWindow <> nil) then
begin
  ShowMessage((IWindow as Excel2000.Window).Name);
  //.FileName
end;

--> Method 'Name' not supported by Automation object
the same for 'FileName'


How can I :
- get the name of the file ?
- know if the file is just created (not yet saved) or was opened ?
- save as a temporary file as the same format ?
- open a file from an URL ?

Thanks in advance
Sylvain

DEV: Delphi 7, Win XP, Office 2007
TARGET: Win XP-7-2012 x64, Office 2007-2010
Posted 01 Aug, 2014 03:49:44 Top
Andrei Smolin


Add-in Express team


Posts: 18842
Joined: 2006-05-11
Hello Sylvain,

> - get the name of the file ?

Get the Parent property of the Excel2000.Window, Word2000.Window and MSPpt2000.DocumentWindow interfaces, cast the returned value to Excel2000.ExcelWorkbook, Word2000.WordDocument and MSPpt2000.PowerPointPresentation and check the FullName property of the object.

> - know if the file is just created (not yet saved) or was opened ?

See the Saved.Property of the object above; true means "modified, not saved". A newly created file has Saved=True. A modified file which was never saved doesn't have an extension in the FullName property.

> - save as a temporary file as the same format ?

See SaveAs() on the object above.

> - open a file from an URL ?

ExcelApp.Workbooks.Open, WordApp.Document.Open, PowerPointApp.Presentations.Open

===
Getting Help on COM Objects, Properties and Methods

To get assistance with host applications?Â?Ð?é objects, their properties, and methods as well as help info, use the Object Browser. Go to the VBA environment (in the host application, choose menu Tools | Macro | Visual Basic Editor or just press {Alt+F11}), press {F2}, select the host application in the topmost combo and/or specify a search string in the search combo. Select a class /property /method and press {F1} to get the help topic that relates to the object.
===


Andrei Smolin
Add-in Express Team Leader
Posted 01 Aug, 2014 04:46:59 Top
iubito




Posts: 16
Joined: 2014-06-19
As usual, you are very fast ! :)
Thanks for the tips, I'll check all that next week.

Regards,
Sylvain

DEV: Delphi 7, Win XP, Office 2007
TARGET: Win XP-7-2012 x64, Office 2007-2010
Posted 01 Aug, 2014 05:10:02 Top