TITLE!

How to convert an office 2007 document to 2003.

by on Mar.30, 2009, under .NET/C#, Coding

So, backwards converting seemed to be impossible, and is give or take but if you can interact with the desktop here’s a way to do it that’s beyond ugly, cludgy, and I wish someone had a better solution… Oh, this is to interact with OFFICE12\Moc.exe, best of luck!

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Threading;
using System.IO;

namespace t
{
   class Program
   {

      static void Main(string[] args)
      {

         Program.ProcessChecker.Proc();
         Console.ReadLine();
      }

      static class ProcessChecker
      {

         internal static class NativeMethods
         {
            [DllImport("user32.dll")]
            public static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);

            [DllImport("user32.dll")]
            public static extern bool SetForegroundWindow(IntPtr hWnd);

            [DllImport("user32.dll")]
            public static extern bool EnumWindows(EnumWindowsProcDel lpEnumFunc,
                Int32 lParam);

            [DllImport("user32.dll")]
            public static extern uint GetWindowThreadProcessId(IntPtr hWnd,
                ref Int32 lpdwProcessId);

            [DllImport("user32.dll")]
            public static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString,
                Int32 nMaxCount);

            [DllImport("user32.dll")]
            public static extern bool SetWindowText(IntPtr hWnd, string lpString);

            [DllImport("user32.dll")]
            public static extern int GetFocus();

            [DllImport("user32.dll")]
            public static extern bool AttachThreadInput(uint idAttach, uint idAttachTo, bool fAttach);

            [DllImport("kernel32.dll")]
            public static extern uint GetCurrentThreadId();

            [DllImport("user32.dll")]
            public static extern uint GetWindowThreadProcessId(int hWnd, int ProcessId);

            [DllImport("user32.dll")]
            public static extern int GetForegroundWindow();

            [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
            public static extern int SendMessage(int hWnd, int Msg, int wParam, StringBuilder lParam);

            [DllImport("user32.dll", CharSet = CharSet.Auto)]
            public static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);

            [DllImport("user32.dll", SetLastError = true)]
            public static extern IntPtr GetWindow(IntPtr hWnd, uint uCmd);

            public enum GetWindow_Cmd : uint
            {
               GW_HWNDFIRST = 0,
               GW_HWNDLAST = 1,
               GW_HWNDNEXT = 2,
               GW_HWNDPREV = 3,
               GW_OWNER = 4,
               GW_CHILD = 5,
               GW_ENABLEDPOPUP = 6
            }

            public const int SW_SHOWNORMAL = 1;
         }
         const int WM_SETTEXT = 12;
         const int WM_GETTEXT = 13;
         public delegate bool EnumWindowsProcDel(IntPtr hWnd, Int32 lParam);

         static private bool EnumWindowsProc(IntPtr hWnd, Int32 lParam)
         {
            int processId = 0;
            uint remoteThreadId = NativeMethods.GetWindowThreadProcessId(hWnd, ref processId);
            StringBuilder WindowText = new StringBuilder(5000);

            NativeMethods.GetWindowText(hWnd, WindowText, 5000);
            StringBuilder v_szbTest2 = new StringBuilder(99);
            NativeMethods.GetClassName(hWnd, v_szbTest2, 99);
            if (WindowText.ToString().Equals("Save As") && processId.Equals(lParam))
            {
               uint currentThreadId = NativeMethods.GetCurrentThreadId();
               NativeMethods.AttachThreadInput(remoteThreadId, currentThreadId, true);

               IntPtr i = IntPtr.Zero;
               i = NativeMethods.GetWindow(hWnd, (uint)NativeMethods.GetWindow_Cmd.GW_CHILD);
               for (; i.ToInt32() > 0; )
               {

                  i = NativeMethods.GetWindow(i, (uint)NativeMethods.GetWindow_Cmd.GW_HWNDNEXT);
                  StringBuilder v_szbTest3 = new StringBuilder(99);
                  NativeMethods.GetClassName(i, v_szbTest3, 99);

                  StringBuilder builder = new StringBuilder(500);
                  builder.Append("c:\\Hey look a text.xls");
                  if (v_szbTest3.ToString().Equals("ComboBoxEx32"))
                  {
                     NativeMethods.SendMessage(i.ToInt32(), WM_SETTEXT, builder.Capacity, builder);
                  }
                  StringBuilder v_szbText = new StringBuilder(256);
                  NativeMethods.GetWindowText(i, v_szbText, 256);
                  if (v_szbTest3.ToString().Equals("Button") && v_szbText.ToString().Equals("&Save"))
                  {
                     //Save!
                     var BM_CLICK = 0xf5;
                     NativeMethods.SendMessage(i.ToInt32(), BM_CLICK, 0, builder);
                     NativeMethods.AttachThreadInput(remoteThreadId, currentThreadId, false);
                     //Lets wait till we can access the file.

                     while (true)
                     {
                        try
                        {
                           FileStream fso = File.Open(builder.ToString() + ".xls", FileMode.Open, FileAccess.Write);
                           fso.Close();
                           //kill our proc
                           Process moc = Process.GetProcessById(lParam);
                           moc.Kill();
                           return true;
                        }
                        catch (IOException x)
                        {
                           Console.WriteLine(x);
                           Thread.Sleep(10);
                        }
                     }

                  }

               }

            }
            return true;
         }

         static public bool Proc()
         {

            Process[] v_oProcs = Process.GetProcesses();
            foreach (Process proc in v_oProcs)
            {
               if (proc.ProcessName.Equals("Moc"))
               {
                  NativeMethods.EnumWindows(new EnumWindowsProcDel(EnumWindowsProc),
                      proc.Id);
                  return false;
               }
            }
            return true;
         }
      }

      #region EnumWindows
      /// 
      /// EnumWindows wrapper for .NET
      /// 
      public class EnumWindows
      {
         #region Delegates
         private delegate int EnumWindowsProc(IntPtr hwnd, int lParam);
         #endregion

         #region UnManagedMethods
         private class UnManagedMethods
         {
            [DllImport("user32")]
            public extern static int EnumWindows(
                EnumWindowsProc lpEnumFunc,
                int lParam);
            [DllImport("user32")]
            public extern static int EnumChildWindows(
                IntPtr hWndParent,
                EnumWindowsProc lpEnumFunc,
                int lParam);
         }
         #endregion

         #region Member Variables
         private List<IntPtr> items = null;
         #endregion

         /// 
         /// Returns the collection of windows returned by
         /// GetWindows
         /// 
         public List<IntPtr> Items
         {
            get
            {
               return this.items;
            }
         }

         /// 
         /// Gets all top level windows on the system.
         /// 
         public void GetWindows()
         {
            this.items = new List<IntPtr>();
            UnManagedMethods.EnumWindows(
                new EnumWindowsProc(this.WindowEnum),
                0);
         }

         /// 
         /// Gets all child windows of the specified window
         /// 
         /// Window Handle to get children for
         public void GetWindows(
             IntPtr hWndParent)
         {
            this.items = new List<IntPtr>();
            UnManagedMethods.EnumChildWindows(
                hWndParent,
                new EnumWindowsProc(this.WindowEnum),
                0);
         }

         #region EnumWindows callback
         /// 
         /// The enum Windows callback.
         /// 
         /// Window Handle
         /// Application defined value
         /// 1 to continue enumeration, 0 to stop
         private int WindowEnum(
             IntPtr hWnd,
             int lParam)
         {
            if (this.OnWindowEnum(hWnd))
            {
               return 1;
            }
            else
            {
               return 0;
            }
         }
         #endregion

         /// 
         /// Called whenever a new window is about to be added
         /// by the Window enumeration called from GetWindows.
         /// If overriding this function, return true to continue
         /// enumeration or false to stop.  If you do not call
         /// the base implementation the Items collection will
         /// be empty.
         /// 
         /// Window handle to add
         /// True to continue enumeration, False to stop
         protected virtual bool OnWindowEnum(
             IntPtr hWnd)
         {
            items.Add(hWnd);
            return true;
         }

         #region Constructor, Dispose
         public EnumWindows()
         {
            // nothing to do
         }
         #endregion
      }
      #endregion EnumWindows

   }

}
:, ,
No comments for this entry yet...

Leave a Reply

*

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!