Opening a document with Office Mobile from your own application

You want to write your own mobile application and need to open Office documents from it ?

Since Microsoft has released Office Mobile on iOS and Android platforms, you probably asked how to open Office documents with these applications from your own application.

After few hours of searches on Google, I didn’t find any information on this subject. So I decided to investigate by myself and I found the solution by using Office Web Apps and by analysing how it works when you click on the option “Edit in Word“.

Office Mobile applications expose some special schemes (like http or https) which allow you to open documents and passing information to the application.

Here is the list of schemes I found through my analysis of Office Web Apps :

  • ms-word
  • ms-excel
  • ms-powerpoint
  • onenote

My need is to open in Office Mobile, some documents which are stored in Office 365, but are presented through a dedicated mobile interface (running on iOS). Opening these files with Office Mobile allow users to read and edit them, and save modifications directly to Office 365.

By using previous schemes, it’s easy to build an URL and to open it using the following code :

NSURL *documentURL = [NSURL URLWithString:@"ms-word:https://tenant.sharepoint.com/Shared%20Documents/Document.docx"];
if ([[UIApplication sharedApplication] canOpenURL:documentURL]) {
    [[UIApplication sharedApplication] openURL:documentURL];
}

As you can see, you just have to concatenate the desired scheme (ms-word: in my case) with the URL of the document.

To ensure that Office Mobile is installed on the device, you can check if the application is able to open it with the call to canOpenURL.

When your application execute the openURL method, the right application is executed and the document is opened. If you’re not authenticated inside the application, you’re invited to do it.

Office Mobile - View Mode

When the document was opened, you probably noticed that it was opened in read-only mode.

It’s the default mode, when you just concatenate an URL to the scheme. But what to do if you want to open the document in edit mode ?

I also found the solution by analysing Office Web Apps  😉

You can specify some additional information between the scheme and the URL of the document.

NSURL *documentURL = [NSURL URLWithString:@"ms-word:ofe%7Cu%7Chttps://tenant.sharepoint.com/Shared%20Documents/Document.docx"]

As you can see above, “ofe%7Cu%7C” was inserted to tell Office Mobile that you want to edit the file (I guess “ofe” means “open for edit”). It’s also possible to use “ofv” (I guess it means “open for view”) in the same manner.

When you run the same code as previously, but with the new URL, the yellow bar below the ribbon has disappeared and you can edit the document without any further action.

Office Mobile - Edit mode

Note that after some tests, this also works with Office for Mac (and probably with Office for Windows).

Enjoy  😉

Advertisements

20 thoughts on “Opening a document with Office Mobile from your own application

  1. Pingback: Office 365 Developer Podcast: Episode 035 on OneDrive consumer APIs with Ryan Gregg | POKORNY

  2. Pingback: Office 365 Developer Podcast: Episode 035 on OneDrive consumer APIs with Ryan Gregg | Nokipedia

  3. Pingback: Office 365 Developer Podcast: Episode 035 on OneDrive consumer APIs with Ryan Gregg | Office 365 Deployment Autoblog

  4. Pingback: Office 365 Developer Podcast: Episode 035 on OneDrive consumer APIs with Ryan Gregg » PC Portal of Wausau

  5. Pingback: Office 365—monthly Dev Digest for March | POKORNY

  6. Pingback: Office 365—monthly Dev Digest for March | Nokipedia

  7. Pingback: Office 365—monthly Dev Digest for March | Office 365 Deployment Autoblog

  8. Pingback: Office 365—monthly Dev Digest for March » PC Portal of Wausau

  9. Victor

    I’m trying to open a local file. The path it is not a http:// it is a file:///. There is any way to open it? Or convert the local file path into a http:// path?

    Like

    Reply
    1. Stéphane Cordonnier (@s_cordonnier) Post author

      Hi Victor,

      Due to the security model of iOS applications, it’s not possible to directly open a local file from an application to another application.

      If you want to perform this kind of operation, you have to use the UIDocumentInteractionController class available in UIKit.

      Like

      Reply
      1. tranhieutt

        HI Stéphane Cordonnier!
        I have trouble similar.
        I can’t open word app with file in local in my app.
        If I use UIDocumentInteractionController,I can open word app and can edit my doc file in Word app.
        I have question:
        1. Could I back to my app with file which I just edit in Word app? How do that?

        Like

        Reply
      2. Stéphane Cordonnier (@s_cordonnier) Post author

        Hi tranhieutt,

        When you open a file with UIDocumentInteractionController, you open a copy of the initial file on the destination application. This copy is stored in the sandbox container of that application.

        There’s no way to get back the file inside the original application after the end of editing (because the destination application don’t know who is the source).

        Liked by 1 person

        Reply
    1. Stéphane Cordonnier (@s_cordonnier) Post author

      I didn’t tested with files which are not stored in SharePoint but because I found these capabilities by doing reverse engineering on Office Web Apps (which is component of Office 365), I have some doubts that it can work with files stored on a “normal server”.

      Like

      Reply
    2. Saranya

      Hi Michael,

      I am also facing some issues while opening excel file in office 365

      My Code:

      NSString *path=@”https://www.google.ch/url?sa=t&rct=j&q=&esrc=s&source=web&cd=8&cad=rja&uact=8&ved=0CE8QFjAHahUKEwinwLekqPvHAhVILhoKHetDDRQ&url=http%3A%2F%2Fopendatakit.org%2Fwp-content%2Fuploads%2Fstatic%2Fsample.xls&usg=AFQjCNFYlLuJBYfQ8EawVQx2MFmKqL3UXw&bvm=bv.102829193,d.bGg”;

      NSURL *documentURL = [NSURL URLWithString:[[NSString stringWithFormat:@”ms-excel:ofv|u|%@”,path] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
      if ([[UIApplication sharedApplication] canOpenURL:documentURL]) {
      [[UIApplication sharedApplication] openURL:documentURL];
      }

      Issue:Excel can’t open this workbook.

      Please help me out to open, edit excel in office 365 from my app.

      Like

      Reply
  10. Pingback: Office 365—monthly Dev Digest for March |

  11. Pingback: Office 365—monthly Dev Digest for March – Office.com/setup Blog | www.office.com/setup

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s