Detect the installed Office version Tag(s): Environment


One way is to call the Windows ASSOC and FTYPE commands, capture the output and parse it to determine the Office version installed.

From the DOS shell :

C:\Users\me>assoc .xls
.xls=Excel.Sheet.8

C:\Users\me>ftype Excel.sheet.8
Excel.sheet.8="C:\Program Files (x86)\Microsoft Office\Office12\EXCEL.EXE" /e
From Java code :
import java.io.*;
public class ShowOfficeInstalled {
    public static void main(String argv[]) {
      try {
        Process p = Runtime.getRuntime().exec
          (new String [] { "cmd.exe", "/c", "assoc", ".xls"});
        BufferedReader input =
          new BufferedReader
            (new InputStreamReader(p.getInputStream()));
        String extensionType = input.readLine();
        input.close();
        // extract type
        if (extensionType == null) {
          System.out.println("no office installed ?");
          System.exit(1);
        }
        String fileType[] = extensionType.split("=");

        p = Runtime.getRuntime().exec
          (new String [] { "cmd.exe", "/c", "ftype", fileType[1]});
        input =
          new BufferedReader
            (new InputStreamReader(p.getInputStream()));
        String fileAssociation = input.readLine();
        // extract path
        String officePath = fileAssociation.split("=")[1];
        System.out.println(officePath);
        //
        // output if office is installed :
        //  "C:\Program Files (x86)\Microsoft Office\Office12\EXCEL.EXE" /e
        // the next step is to parse the pathname but this is left as an exercise :-)
        //
      }
      catch (Exception err) {
        err.printStackTrace();
      }
    }
  }

blog comments powered by Disqus