none
[Interop] open a file, quick and easy RRS feed

  • Question

  • Here's my problem:

    I need to read a specific CustomDocumentProperty from a lot of Office documents. I must support Word, Excel, and Powerpoint. I know how to read the property from a document, for example:

    Dim objWord As New Word.Application Dim document As Word.Document = objWord.Documents.Open(FileName:=filename, [ReadOnly]:=True, AddToRecentFiles:=False, Visible:=False) Dim honey As String = document.CustomDocumentProperties("HONEY").Value.ToString().ToUpper() document.Close(SaveChanges:=Word.WdSaveOptions.wdDoNotSaveChanges)

    objWord.Quit(SaveChanges:=Word.WdSaveOptions.wdDoNotSaveChanges)

    Most of those documents have macros that automatically execute when a document is opened. 

    Sometimes, those macros have errors that Office shows in a message box. Under some conditions, a warning dialog explaining that macros are disabled is shown to the user.

    I'm looking for a way to read a CustomDocumentProperty from a Word, Excel, and Powerpoint document that will not, under any circumstances, ask for user interaction. If there's an error, I'd rather get an exception, that I can silently swallow. I tried a few things, but each would show a message to the user under some condition.

    I'm developping with Visual Studio 2010, referencing the Office Interop libraries, targeting .NET framework 4.0 and I must support Office 2007 and 2010.


    if a problem looks too big, break it into smaller objects


    • Edited by M0nkeyMaster Wednesday, March 12, 2014 8:55 AM typo
    Tuesday, March 11, 2014 5:46 PM

Answers

  • Hello "DonkeyMaster",

    For example, the Word object model provides the following properties that you may find useful:

     Visible - it allows you to hide Word.

    ScreenUpdating - The ScreenUpdating property controls most display changes on the monitor while a procedure is running. You can increase the speed of some procedures by keeping screen updating turned off. You must set the ScreenUpdating property to True when the procedure finishes or when it stops after an error.

    DisplayAlerts - allows to set the way certain alerts and messages are handled.

    The similar properties can be found in each Office application.

    Also, if you work only with Open XML files, you can use the Open XML SDK 2.5 for Office for reading documents without automating host applications.

    Tuesday, March 11, 2014 10:59 PM