SimpleCom.bi (lean and mean LateBound COM-access)

Windows specific questions.
kcvinu
Posts: 173
Joined: Oct 07, 2015 16:44
Location: Keralam, India

Re: SimpleCom.bi (lean and mean LateBound COM-access)

Postby kcvinu » Sep 09, 2018 11:23

I think it is better to avoid using freebasic when you need to read data from already opened excel file. It seems that using another language is the best option.
Josep Roca
Posts: 392
Joined: Sep 27, 2016 18:20
Location: Valencia, Spain

Re: SimpleCom.bi (lean and mean LateBound COM-access)

Postby Josep Roca » Sep 09, 2018 12:21

kcvinu wrote:@Josep Roca

Thanks a lot for the code. Infact, I was frustrated using disphelper and SimpleCom, since these libs dont have a function for getting active com object. So far so good. Your code is running and it gives me an any ptr. But using this gave an error message ;

Code: Select all

Var xlApp  = AfxGetCom("Excel.Application")
? xlApp.ActiveWindow.Caption

I think that i am missing something. this is only an any ptr. Please guide me.

Edit note:
We need to add this inside the AfxGetCom function.

Code: Select all

CoInitialize(Null)


> We need to add this inside the AfxGetCom function.

Not really. You can initialize the COM libary in your application before calling it.

> Your code is running and it gives me an any ptr. But using this gave an error message ; Var xlApp = AfxGetCom("Excel.Application")

Of course. You have to cast the returned pointer to an IDispatch PTR and then use it with disphelper or another library. Yu can also change the return value of the function to IDispatch PTR instead of ANY PTR.
Josep Roca
Posts: 392
Joined: Sep 27, 2016 18:20
Location: Valencia, Spain

Re: SimpleCom.bi (lean and mean LateBound COM-access)

Postby Josep Roca » Sep 09, 2018 13:46

kcvinu wrote:I think it is better to avoid using freebasic when you need to read data from already opened excel file. It seems that using another language is the best option.


Automation languages like VBScript are tailored to work with Automation servers such Office. You can use them with Free Basic, but not as easily, and certainly you can't take VBScript code and expect that it will work with Free Basic.
kcvinu
Posts: 173
Joined: Oct 07, 2015 16:44
Location: Keralam, India

Re: SimpleCom.bi (lean and mean LateBound COM-access)

Postby kcvinu » Sep 09, 2018 16:59

@Josep Roca ,
Thank you for the reply. I can managed to find some code which deals with opened excel file but those are in C & C++. But i got a fair idea about what to do with the IDispatch Ptr which returns from your AfxGetCom function. This is my next aim - "IDispatch::GetIDsOfNames"
And this is some code which i should need to study before doing anything in fb.

Code: Select all

#include <assert.h>
#include <ole2.h>
#include <tchar.h>

int main() {
    HRESULT hr;
    IDispatch *objExcel, *objWorkbooks;
    CLSID clsid;
    DISPID id, id2;
    DISPPARAMS p;
    VARIANT v;
    TCHAR *name;

    CoInitialize(NULL);

    hr = CLSIDFromProgID(_T("Excel.Application"), &clsid);
    assert(SUCCEEDED(hr));
    hr = CoCreateInstance(clsid, NULL, CLSCTX_LOCAL_SERVER,
            IID_IDispatch, (LPVOID *)&objExcel);
    assert(SUCCEEDED(hr));

    id2 = DISPID_PROPERTYPUT;
    name = _T("Visible");
    hr = objExcel->GetIDsOfNames(IID_NULL, &name, 1, LOCALE_USER_DEFAULT, &id);
    assert(SUCCEEDED(hr));
    VariantInit(&v);
    v.vt = VT_I4;
    v.lVal = 1;
    p.cArgs = 1;
    p.rgvarg = &v;
    p.cNamedArgs = 1;
    p.rgdispidNamedArgs = &id2;
    hr = objExcel->Invoke(id, IID_NULL, LOCALE_SYSTEM_DEFAULT,
            DISPATCH_PROPERTYPUT, &p, NULL, NULL, NULL);
    assert(SUCCEEDED(hr));

    name = _T("Workbooks");
    hr = objExcel->GetIDsOfNames(IID_NULL, &name, 1, LOCALE_USER_DEFAULT, &id);
    assert(SUCCEEDED(hr));
    p.cArgs = 0;
    p.rgvarg = NULL;
    p.cNamedArgs = 0;
    p.rgdispidNamedArgs = NULL;
    hr = objExcel->Invoke(id, IID_NULL, LOCALE_SYSTEM_DEFAULT,
            DISPATCH_PROPERTYGET, &p, &v, NULL, NULL);
    assert(SUCCEEDED(hr));
    objWorkbooks = v.pdispVal;

    name = _T("Add");
    hr = objWorkbooks->GetIDsOfNames(IID_NULL, &name, 1, LOCALE_USER_DEFAULT, &id);
    assert(SUCCEEDED(hr));
    p.cArgs = 0;
    p.rgvarg = NULL;
    p.cNamedArgs = 0;
    p.rgdispidNamedArgs = NULL;
    hr = objWorkbooks->Invoke(id, IID_NULL, LOCALE_SYSTEM_DEFAULT,
            DISPATCH_PROPERTYGET, &p, NULL, NULL, NULL);
    assert(SUCCEEDED(hr));

    return 0;
}

Return to “Windows”

Who is online

Users browsing this forum: No registered users and 2 guests