Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default Converting Excel Calculations to C# (or VB.NET) code and assem

Hi Wei Lu
Thanks very much for that - I will look into it. I've been wanting to get
into VSTO and this looks like my chance.

Best regards

Marek

"Wei Lu [MSFT]" wrote:

Hello Marek,

I did not have a sample but in VSTO you could use the hook.

And here is a sample which use the hook to sub-classes the Excel window and
handles the Paste event. You may modify it to hook the calculation and use
it in VSTO.

This is the Hook.cs class that sub-classes the main Excel window using
SetWindowLong

using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Reflection;

namespace SampleXLS
{
class Hook
{
private const int WM_KEYDOWN = 0x0100;

[DllImport("user32.dll")]
private static extern IntPtr FindWindowEx(IntPtr hParent, IntPtr hChild,
string sClass, string sWindow);

[DllImport("user32.dll")]
private static extern bool GetKeyState(int nVirtKey );

public delegate int WndProcDelegate(IntPtr hwnd, uint msg, uint wParam, int
lParam);
public static WndProcDelegate wpd;
public static IntPtr lpPrevWndProc;
public bool IsHooked;
public IntPtr gHW;

public void SetHook(int hWnd)
{
try
{
if (IsHooked)
MessageBox.Show("Don't hook it twice without unhooking, or you
will be unable to unhook it.");
else
{
gHW = new IntPtr(hWnd);
gHW = FindWindowEx(gHW, IntPtr.Zero, "XLDESK", string.Empty);

gHW = FindWindowEx(gHW, IntPtr.Zero, "EXCEL7",
"SampleXLS.xls");

wpd = new WndProcDelegate(WndProcSub);
lpPrevWndProc = Win32.SetWindowLong(gHW, -4,
Marshal.GetFunctionPointerForDelegate(wpd));
//Marshal.GetFunctionPointerForDelegate(m_delegate)) ;
IsHooked = true;

}
}
catch (Exception ex)
{
Debug.Print(ex.Message);
}

}

public void SetUnhook()
{
IntPtr temp;
temp = Win32.SetWindowLong(gHW, -4, lpPrevWndProc);
IsHooked = false;
}

private int WndProcSub(IntPtr hWnd, uint msg, uint wParam, int lParam)
{
uint vkCode = wParam;
if(GetKeyState((int)Win32.VK_CONTROL))
{
if(vkCode == Win32.VK_V)
Debug.Print("Paste Called");
}

return Win32.CallWindowProc(lpPrevWndProc, hWnd, msg, wParam,
lParam);
}
}
}

This is the code in VSTO where I call the SetHook and SetUnHook methods.

private void ThisWorkbook_Startup(object sender, System.EventArgs e)
{
WorkbookSheetActivate();
oControl = Application.CommandBars["Worksheet Menu
Bar"].Controls["Edit"];
oControls =
(Microsoft.Office.Core.CommandBarControls)oControl .GetType().InvokeMember("C
ontrols"
, System.Reflection.BindingFlags.GetProperty, null, oControl, null);
oButton = (Microsoft.Office.Core.CommandBarButton)oControls["Paste"];
oButton.Click += new
Microsoft.Office.Core._CommandBarButtonEvents_Clic kEventHandler(oButton_Clic
k);

int i = Application.Hwnd;
objHook.SetHook(i);

}

private void ThisWorkbook_Shutdown(object sender, System.EventArgs e)
{
objHook.SetUnhook();
}

Hope this helps.

Sincerely,

Wei Lu
Microsoft Online Community Support

==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Converting EasyLanguage code to Excel holiday Excel Discussion (Misc queries) 0 January 6th 08 06:31 PM
Preserving VB code when converting from Excel [email protected] Excel Programming 1 February 10th 07 01:28 AM
Deploying a application which uses Excel primary interop assemblie Ajey Ayachit Excel Programming 2 November 23rd 05 11:05 AM
Converting date to numerical and doing calculations Teddy Excel Worksheet Functions 3 October 5th 05 09:39 PM
Converting VB Code for Excel 2000 to Excel 2003 MarkPirks Excel Programming 2 January 5th 05 12:37 AM


All times are GMT +1. The time now is 02:22 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"