2

Possible Duplicate:
How do I call WCF client from Excel 2003 VBA?

I want to call a web service developed using WCF from the Excel using VBA code. How can we do this ? I have tried the GetObject() method but I'm getting syntax error while using this. I want to display the data that I'm getting from the web service in Excel. Please help. The DataContract is as follows: `

[DataContract] 
public class Data
{
    [DataMember]
    public int Id;
    [DataMember]
    public DateTime LockTime;
    [DataMember]
    public DateTime LoginTime;
    [DataMember]
    public DateTime LastDefenitionDate;
    [DataMember]
    public string NTLogin;
    [DataMember]
    public string SystemName;
}

ServiceContract is as follows:

`

[ServiceContract]

interface IDataService
{
    [OperationContract]
    List<Data> GetData();
    [OperationContract]
    void SubmitData(Data data);
}

`

DataService for accessing database is as follows:

`

[ServiceBehavior(InstanceContextMode=InstanceContextMode.Single)]

public class DataService : IDataService { public static SQLConnection SQLDBConnection = new SQLConnection();

    #region IDataService Members

    public List<Data> GetData()
    {
        List<Data> datas = new List<Data>();
        try
        {
            if (SQLDBConnection.con.State == ConnectionState.Closed) SQLDBConnection.con.Open();
            datas.Clear();
            SqlCommand sqlcommand = new SqlCommand();
            sqlcommand.Connection = SQLDBConnection.con;
            sqlcommand.CommandText = "select * from tblData";
            sqlcommand.CommandType = CommandType.Text;
            SqlDataAdapter sqladapter = new SqlDataAdapter(sqlcommand);
            DataTable dt = new DataTable();
            sqladapter.Fill(dt);

            Data data = null;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                data = new Data();
                data.Id = Convert.ToInt32(dt.Rows[i]["id"]);
                data.NTLogin = dt.Rows[i]["NTLogin"].ToString();
                data.SystemName = dt.Rows[i]["SystemName"].ToString();
                data.LockTime = Convert.ToDateTime(dt.Rows[i]["LockTime"]);
                data.LoginTime = Convert.ToDateTime(dt.Rows[i]["LoginTime"]);
                data.LastDefenitionDate = Convert.ToDateTime(dt.Rows[i]["LastDefenitionDate"]);
                datas.Add(data);
            }
        }
        catch (Exception ex)
        {  } 
        return datas;
    }

    public void SubmitData(Data data)
    {
        if (SQLDBConnection.con.State == ConnectionState.Closed) SQLDBConnection.con.Open();
        SqlCommand sqlcommand = new SqlCommand();
        sqlcommand.Connection = SQLDBConnection.con;
        sqlcommand.CommandText = "Insert into dbo.tblData(NTLogin, SystemName, LockTime, LoginTime, LastDefenitionDate) values ('" + data.NTLogin + "','" + data.SystemName + "','" + data.LockTime + "' , '" + data.LoginTime + "', '" + data.LastDefenitionDate + "')";
        sqlcommand.CommandType = CommandType.Text;
        int RowsAffected = sqlcommand.ExecuteNonQuery();
    }

    #endregion
}

`

EDIT:

The possible duplicate suggestion's answer didn't worked out for me. My entire code is given here. I have even checked that post, before posting my question. Please check my code.

Community
  • 1
  • 1
Anish V
  • 673
  • 3
  • 17
  • 38
  • See this http://stackoverflow.com/questions/1013428/how-do-i-call-wcf-client-from-excel-2003-vba – Siddharth Rout Oct 05 '12 at 11:20
  • Its not working for me. I'm getting an error like this while I tried to run the vba code. `Run-time error '-2147221020 (800401e4)': Automation error Invalid syntax.` – Anish V Oct 08 '12 at 04:50
  • Any idea about this error? Please help.. – Anish V Oct 08 '12 at 12:41
  • The link which you have provided me is not working for me. Please help me to find my error by checking my code. This question may be duplicate, but the answer is NOT. – Anish V Oct 10 '12 at 04:23
  • I didn't get a proper help yet regarding this problem. I have posted my entire set of code, but still my question is closed!!! – Anish V Oct 11 '12 at 08:19

1 Answers1

2

Read following article, it describes in details how to call WCF service from VBA code using GetObject:

Calling WCF Services from Excel VBA clients using the WCF Service Moniker
http://damianblog.com/2009/07/05/excel-wcf/

But it works only for simple WCF service contracts. For more complex things you need to use VSTO

UPD: In such case, when using Moniker with MEX contracts, you should use only primitive types, and arrays of primitive types. When you need to use complex type, either e.g. try to pack them into string or use more advanced techniques like WCF Moniker with COM Clients ( http://msdn.microsoft.com/en-us/library/ms752245.aspx) or VSTO.

Regfor
  • 8,515
  • 1
  • 38
  • 51
  • Did you check the link that I gave above? :) – Siddharth Rout Oct 05 '12 at 13:38
  • @SiddharthRout No, my link came from Google. But now checked that question and yes, they suggest there the same link – Regfor Oct 06 '12 at 11:09
  • Its not working for me. I'm getting an error like this while I tried to run the vba code. `Run-time error '-2147221020 (800401e4)': Automation error Invalid syntax`. – Anish V Oct 08 '12 at 04:50
  • @AnishV Please read article with attention. Your situation is described there. As a suggestion - attach your VS debugger to Excel to get more information about error, it's described in article – Regfor Oct 08 '12 at 06:33
  • @Regfor I have checked that too. Tried to add the excel, but it gives only one error message as `Interface not found.` But this seems to an internal exception occurred inside the Framework and not the exact error message. – Anish V Oct 08 '12 at 06:52
  • @AnishV Seems to me that addr argument from GetObject(addr) is incorrect. Try to check it or even post here to check it together. – Regfor Oct 08 '12 at 07:01
  • @Regfor Please check the code. `Dim addr As String addr = "service:mexAddress=""http://localhost:2436/EPCompServiceSite/EPCService.svc/mex""," addr = addr + "address=""http://localhost:2436/EPCompServiceSite/EPCService.svc""," addr = addr + "contract=""EPCompService.IDataService"", contractNamespace=""http://tempuri.org/""," addr = addr + "binding=""wsHttpBinding"", bindingNamespace=""http://tempuri.org/""" Dim service1 As Object Set service1 = GetObject(addr)` – Anish V Oct 08 '12 at 07:08
  • @AnishV Seems to be some "hacks" in this addr string. Look at following question and answers, it could help you http://stackoverflow.com/questions/2968963/interface-not-found-in-wcf-moniker-without-registration-for-excel – Regfor Oct 08 '12 at 07:29
  • @Regfor I have tried the ways described there , but still getting the same error. I'm exposing two methods in the service named `GetData()` and `SubmitData()`. The first method returns a generic list of type Data ie. `List`, of which Data is the class containing the defenitions of all the variables. Also, the method `SubmitData()` takes the parameter of type Data. Is that could be the problem for this ? Any idea? Does it support this type of non primitive datatypes ? – Anish V Oct 08 '12 at 08:49
  • @AnishV Could you post Data code here? Non primitive types should be serializable and correspond to VBA types, so could e problems with them. To identify that problem is with this type try to return from service something like "string" or "int" – Regfor Oct 08 '12 at 09:07
  • @Regfor I'll edit my question with the DataContract, ServiceContract and DataService. – Anish V Oct 08 '12 at 09:49
  • @Regfor Any idea of my error? Entire is posted in my question. Please check. – Anish V Oct 10 '12 at 04:26
  • @AnishV I've made update in answer for you. In this case use primitive types or arrays of primitive types or use other advanced ways. – Regfor Oct 10 '12 at 11:13