Help me please aboutVS 2003 and Office 2003
Why does it cannot work?Could you tell me the mistake?
Thanks you very much :)
using System;
using System.Windows.Forms;
using Office = Microsoft.Office.Core;
using Excel = Microsoft.Office.Interop.Excel;
using System.Data;
using System.Diagnostics;
using System.Xml.Serialization;
using System.Web.Services.Protocols;
using System.Web.Services;
using MSForms = Microsoft.Vbe.Interop.Forms;
// Office integration attribute. Identifies the startup class for the workbook. Do not modify.
[assembly:System.ComponentModel.DescriptionAttribut e("OfficeStartupClass, Version=1.0, Class=Excel_ja.BondCalculation")]
namespace Excel_ja
{
/// <summary>
/// Contains managed code extensions for the workbook.
/// </summary>
///
[System.Web.Services.WebServiceBindingAttribute(
Name="BondCalculationSoap",
Namespace="urn:BondCalculation")]
public class BondCalculation : System.Web.Services.Protocols.SoapHttpClientProtoc ol
{
/// <summary>
/// Application object.
/// </summary>
///
internal Excel.Application ThisApplication
{
get { return thisApplication;}
}
/// <summary>
/// Workbook object.
/// </summary>
internal Excel.Workbook ThisWorkbook
{
get { return thisWorkbook;}
}
private Excel.Application thisApplication = null;
private Excel.Workbook thisWorkbook = null;
private MSForms.CommandButton button;
private Excel.Worksheet ws;
private Excel.WorkbookEvents_OpenEventHandler openEvent;
private Excel.WorkbookEvents_BeforeCloseEventHandler beforeCloseEvent;
#region Generated initialization code
/// <summary>
/// Default constructor.
/// </summary>
/// <summary>
/// Required procedure. Do not modify.
/// </summary>
/// <param name="application">Application object.</param>
/// <param name="workbook">Workbook object.</param>
public void _Startup(object application, object workbook)
{
this.thisApplication = application as Excel.Application;
this.thisWorkbook = workbook as Excel.Workbook;
openEvent= new Excel.WorkbookEvents_OpenEventHandler (ThisWorkbook_Open);
thisWorkbook.Open += openEvent;
beforeCloseEvent = new Excel.WorkbookEvents_BeforeCloseEventHandler(ThisW orkbook_BeforeClose);
thisWorkbook.BeforeClose += beforeCloseEvent;
}
/// <summary>
/// Required procedure. Do not modify.
/// </summary>
public void _Shutdown()
{
thisApplication = null;
thisWorkbook = null;
}
/// <summary>
/// Finds the control with the specified name in the active worksheet.
/// </summary>
/// <param name='name'>Name of the control to find.</param>
/// <returns>
/// Returns the specified control, or null if it is not found.
/// </returns>
object FindControl(string name )
{
return FindControl(name, (Excel.Worksheet) ThisWorkbook.ActiveSheet);
}
/// <summary>
/// Returns the control with the specified name in the specified worksheet.
/// </summary>
/// <param name='name'>Name of the control to find.</param>
/// <param name='sheet'>Worksheet object that contains the control.</param>
/// <returns>
/// Returns the specified control, or null if it is not found.
/// </returns>
object FindControl(string name, Excel.Worksheet sheet )
{
Excel.OLEObject theObject;
try
{
theObject = (Excel.OLEObject) sheet.OLEObjects(name);
return theObject.Object;
}
catch
{
// Returns null if the control is not found.
}
return null;
}
#endregion
/// <summary>
/// Called when the workbook is opened.
/// </summary>
///
public BondCalculation()
{
this.Url = "http://192.168.10.74/bondserv_test/pricingservice.asmx";
}
[System.Web.Services.Protocols.SoapDocumentMethodAt tribute(
"urn:BondCalculation/calcPriceByYTM",
RequestNamespace="urn:BondCalculation",
ResponseNamespace="urn:BondCalculation",
Use=System.Web.Services.Description.SoapBindingUse .Literal,
ParameterStyle=System.Web.Services.Protocols.SoapP arameterStyle.Wrapped)]
public string calcPriceByYTM(string name)
{
object[] results = this.Invoke("calcPriceByYTM",
new object[] {"LB22NA",3,DateTime.Now.Date,DateTime.Now.Date });
return ((string)(results[0]));
}
protected void ThisWorkbook_Open()
{
this.button = (MSForms.CommandButton) this.FindControl("button");
this.button.Click +=new MSForms.CommandButtonEvents_ClickEventHandler(butt on_Click);
if (this.button != null)
{
this.button.Click += new
MSForms.CommandButtonEvents_ClickEventHandler(
button_Click);
}
Excel.Worksheet ws = (Excel.Worksheet) ThisApplication.Sheets.get_Item(1);
Excel.Range rng = ws.get_Range ("D1" , "H1");
rng.Value2 = "Hello, ****ing Excel";
ws.Cells.AutoFit();
ws.Cells.AddComment("Nash");
ThisApplication.get_Range("A1","G2").Calculate();
show();
//InitSheet ( );
Ex();
MSForms.CommandButton button1 = (MSForms.CommandButton) FindControl("button1");
button1.Click += new Microsoft.Vbe.Interop.Forms.CommandButtonEvents_Cl ickEventHandler
(button1_Click);
}
/// <summary>
/// Called before the workbook is closed. Note that this method
/// might be called multiple times and the value assigned to Cancel
/// might be ignored if other code or the user intervenes.
/// </summary>
/// <param name="Cancel">False when the event occurs. If the event procedure
/// sets this to true, the document does not close when the procedure is finished.
/// </param>
protected void ThisWorkbook_BeforeClose(ref bool Cancel)
{
Cancel = false;
}
private void show()
{
Console.WriteLine("Calling the SOAP Server to say hello");
// BondCalculation BondCalculation = new BondCalculation();
// Console.WriteLine("The SOAP Server says: "+BondCalculation.calcPriceByYTM("siwaporn"));
}
private void button_Click()
{
show();
}
private void button1_Click()
{
Excel.Worksheet sheet = (Excel.Worksheet) ThisApplication.Sheets.get_Item(1);
Excel.Range rng = sheet.get_Range ( "A1", "D1" );
rng.Value2 = "Hi What the hell is going on";
}
protected void Ex()
{
Excel.Worksheet ws=new Excel.WorksheetClass();
ws = (Excel.Worksheet)ThisApplication.ActiveWorkbook.Ac tiveSheet;
for(int i=1;i<11;i++)
{
for(int j=1;j<11;j++)
{
ws.Cells[i,j]=i;
}
}
}
private void InitSheet ( )
{
Excel.Range rng;
for ( int i = 1 ; i < 30; i ++ )
{
rng = (Excel.Range)ws.Rows[i,"nash"];
rng.RowHeight = 10;
}
for ( int j = 1 ; j < 30; j ++ )
{
rng = (Excel.Range)ws.Columns[j,"Nash"];
rng.ColumnWidth = 1;
}
}
}
}
|