longvh211 / Chromium-Automation-with-CDP-for-VBA

A method to directly automate Chromium-based web browsers, such as Chrome, Edge, and Firefox, using VBA for Office applications by following the Chrome DevTools Protocol framework.
MIT License
45 stars 6 forks source link

Slight typo in CDPElement class and some Access adaptions. #12

Closed MitsOmitsaras closed 1 year ago

MitsOmitsaras commented 1 year ago

Since you encourage help in pointing bugs, I would like to report some little issues. 1) There is a slight typo in Property Get varResult of the CDPelement class. Instead of varResut = varType the correct should be varResult = varType 2) When importing the 4 classes in Access instead of Excel, some issues occur on compiling. i) Sometimes there seems to be some kind of "class overlapping" when the "Microsoft Word 16.0 Object Library" is also added before "Microsoft Scripting Runtime" library, but not always. The result is that the Dictionary class raises an error pointing at the New keyword of the Dictionary instantiations. This is solved by turning every Dim x As Dictionary declaration into Dim x As Scripting.Dictionary, and every Set x = New Dictionary instantiation into Set x = New Scripting.Dictionary. ii) In the cleanUpSessions function of the CDPBrowser class, using the word "proc" as a variable, raises an error. I just turned proc to proc1 and everything worked fine. iii) Thisworkbook object that is mentioned in a couple of places, has to be turrned into CurrentProject object and everything works fine.

longvh211 commented 1 year ago

@MitsOmitsaras thank you for the report. I have made some tweaks based on your valuable findings in v2.7.3. Would you be able to help check if the new framework version is compatible with Word & Access at your end out-of-the box?

MitsOmitsaras commented 1 year ago

Once again I would like to thank you for sharing this work with us. This is endless and very useful for people like me, that need browser automation but cannot install any software. Also very handy. I will try it the soonest and be back with the results.

MitsOmitsaras commented 1 year ago

First test using MS Word 2016-9/Windows 10 latest updates.

After importing the 4 CDP classes in a new word document, and adding a reference to the Microsoft Scripting Runtime library.

The Dictionary class of the Microsoft Scripting Runtime library fails in the following points: A) CDPBrowser class Module declarations section: Private varIDs As Dictionary Function invokeMethod(...) As Dictionary Function sendMessage(strMessage As String, Optional objAllMessages As Dictionary ...) Function invokeError(result As Dictionary)

B) CDPJconv class Function json_ParseObject(...) As Dictionary

C) CDPElement class Module declarations section: Private varIDs As Dictionary

After changing at these points every Dictionary to Scripting.Dictionary, the code compiles successfully.

However the remaining "simply mentioned" Dictionaries fail at runtime due to type mismatch. Function sendMessage needs to be turned into: sendMessage(strMessage As String, Optional objAllMessages As Dictionary ...) As Scripting.Disctionary. Also the variable params of the Function invokeMethod. Also the variable targetObjIdCol of the Sub init in the CDPElement class.

I must say that this failing of the Dictionary class feels like some kind of built in error of the Microsoft Scripting Runtime library when used with MS Word, rather than an error raised by your particular code. May I suggest this. What about making this change (Dictionary to Scripting.Dictionary) to every point of this code just to be sure. Anyway you know better.

Second test using MS Excel 2016-9/Windows 10 latest updates.

Everything works fine.

Third test using MS Access 2016-9/Windows 10 latest updates.

After importing the 4 CDP classes in an existing access project (accdb), already having a reference to the Microsoft Scripting Runtime library, the MS Word library and the MS Excel library.

The ThisWorkbook variable in the Sub Class_Initialize of the CDPBrowser class raises a run-time error '1004': Method 'ThisWorkbook' of object '_Global' failed. I wasn't able to understand why. Declaring the variable has stopped this error raising though. Just a Dim ThisWorkbook.

longvh211 commented 1 year ago

2.7.4 now applied with your recommendations above Mits. These are great recommendations and well done on finding these solutions too.

MitsOmitsaras commented 1 year ago

I 've tested the new files. Unfortunately my suggestion for the last compat issue for Access, was a bad approach. Dim ThisWorkbook does the trick in my case, but creates another problem if used in Excel.

This is what I 've found out: Your initial code without Dim ThisWorkbook works fine with all 3 apps. The run-time error '1004': Method 'ThisWorkbook' of object '_Global' failed that I reported to you, only occurs in Access (or Word) if the Microsoft Excel 16.0 Object Library is added.

In this case, if Dim ThisWorkbook is added, stops this error. But this line creates the following error if used in Excel. Just declaring ThisWorkbook while in Excel, causes it to be empty in Sub Class_Initialize's scope, so the first case in the Select Case is never executed and the thisApp variable remains empty too. This raises a run-time error '91': Object varable or With block variable not set on thisApp when starting a CDPBrowser object.

Everything else works perfect in any case. Sorry for the poor suggestion.

longvh211 commented 1 year ago

No prob Mits. Certain things can only be found out when it happens. I changed the lines to this and they are all ok. 2.7.4 has been re-uploaded.

Select Case True
    Case InStr(Application.name, "Excel"): Set thisApp = ThisWorkbook
    Case InStr(Application.name, "Word"): Set thisApp = ThisDocument
    Case InStr(Application.name, "Access"): Set thisApp = CurrentProject
End Select
MitsOmitsaras commented 1 year ago

Now that is a really clean and simple solution! All tested. Compiles and runs perfect in any app and any combination of these libraries. Once again thanks for the life saving work you shared with us. Thanks for the reference too. This was very kind.

MitsOmitsaras commented 1 year ago

Let me know if I can help with anything else.