SimpleCom.bi (lean and mean LateBound COM-access)
Re: SimpleCom.bi (lean and mean LateBound COM-access)
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.
-
- Posts: 564
- Joined: Sep 27, 2016 18:20
- Location: Valencia, Spain
Re: SimpleCom.bi (lean and mean LateBound COM-access)
> We need to add this inside the AfxGetCom function.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 ;I think that i am missing something. this is only an any ptr. Please guide me.Code: Select all
Var xlApp = AfxGetCom("Excel.Application") ? xlApp.ActiveWindow.Caption
Edit note:
We need to add this inside the AfxGetCom function.Code: Select all
CoInitialize(Null)
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.
-
- Posts: 564
- Joined: Sep 27, 2016 18:20
- Location: Valencia, Spain
Re: SimpleCom.bi (lean and mean LateBound COM-access)
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 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.
Re: SimpleCom.bi (lean and mean LateBound COM-access)
@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.
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;
}