Uploading an Excel spreadsheet to Google Sheets - excel

I've been hitting my head on a brick wall with the following lovely conundrum:
The point is to get data from Excel to Google Sheets without uploading files, but going through the URLs. The following handily explain how all that business transpires:
https://www.youtube.com/watch?v=mX2_XNYPGiI
http://ramblings.mcpher.com/Home/excelquirks/exceldocsintegration/excelsheetsv4
I've tried to adapt the following code for the VBA side of things, common to both examples:
Option Explicit
Sub GetDataFromGoogle()
'link to tutorial: https://www.youtube.com/watch?v=mX2_XNYPGiI
Dim link As String
link = "https://docs.google.com/spreadsheets/d/e/2PACX-1vSes7Jb06JRy8KSFyNlpUSzNQxSB_HZay4S8AB2IqzpZP0QdwGO5PFSS-6uzd8v_GsjlkXM31pby2jE/pubhtml"
Sheet4.QueryTables(1).Connection = "URL;" & link
Sheet4.QueryTables(1).Refresh False
Sheet4.Columns(1).ColumnWidth = 10
End Sub
Sub PushDataToGoogle()
Dim link As String
link = "https://docs.google.com/forms/d/e/1FAIpQLSeDHEKJmRDynwOAS4g53T9AVMtpXQkWsRGbAzLpLI7rdsbiFA/formResponse?entry.1155739640=4&fvv=1&draftResponse=%5B%5B%5Bnull%2C1155739640%2C%5B%224%22%5D%0D%0A%2C0%5D%0D%0A%5D%0D%0A%2Cnull%2C%2276341394568976993%22%5D%0D"
'go to that link, refresh it and ensure that the first column isn't too narrow
sheets("Sheet4").QueryTables(1).Connection = "URL;" & link
sheets("Sheet4").QueryTables(1).Refresh True
sheets("Sheet4").Columns(1).ColumnWidth = 10
End Sub
On the Google Sheets side, I used a Form to create a spreadsheet that is both the data source for an Excel import and the destination for the export of data. Replcaing POST and GET in the HTML of the form page is the way to get the destination URL. Subsequently, that URL can be either hardcoded or edited with variables to get various inputs for the "entry.######" part of the URL, and then they are passed on to the sheet in the next available line, which would suit my purposes.
I hit a snag(s) with the oAuth2 part of the operation. From the example on the Desktop Liberation site (second link above), credentials are created from Google and then inserted into a code that authenticates the connection before transmitting the data. As such:
Private Function sheetsOnceOff()
getGoogled "sheets", , _
"1023445954023-hq8gkdcmo9sue822d23gy9ak5hmun27.apps.googleusercontent.com", _
"dX7ABCDEGFBETFWtvX5ShmDfrgrQ"
End Function
If that isn't done, the prior lines of code will just return the main Google login page. I've made the credentials, determined the destination key for the spreadsheet, but I get an error in the getGoogled routine that says:
Runtime-error: '-2147024809 (80070057)': The parameter is incorrect."
parameter values are as follows:
- scope = 'sheets', as it should be
- replacement package = ""
- clientID is OK
- clientSecret is OK
- complain = true
- cloneFromScope = ""
- apikey =""
I'm thinking the replacement package should not be empty if complain is true, but I feel that I'm out of my depth here. At least the logic of what needs to happen makes sense to me, and I've managed to get the transfer from Google Sheets to work fine, but I'm just not sure how to handle the oAuth2 authentication matter.
Thank you in advance.

The following is by no means a complete solution, but it is a rudimentary start. The condition on the following is to be logged into gmail, and it will move one piece of data into Google sheets with its timestamp. It works by direclty opening the page that appears after a user clicks Submit on the form, thus imitating a submission. Link2 opens the spreadsheet where the response is passed.
The downside is that it requires the user to be logged into gmail already and will open a new tab each time a variable is passed, so avoid modifying it with arrays or loops. However, I will try putting together a prior vba gmail project with this to see if can work. More will be forthcoming, but for a hacked together band-aid solution, this will do if someone needs it.
Credits and info here:
[https://stackoverflow.com/questions/3166265/open-an-html-page-in-default-browser-with-vba
https://stackoverflow.com/questions/5915325/open-google-chrome-from-vba-excel
Sub PushDataToGoogle()
Dim chromePath As String
chromePath = """C:\Program Files\Google\Chrome\Application\chrome.exe"""
Dim link As String, link2 As String
link = "https://docs.google.com/forms/d/e/1FAIpQLSeDHEKJmRDynwOAS4gtpXQkWsRGbAzLpLI7rdsbiFA/formResponse?entry.1155739640=4&fvv=1&draftResponse=%5B%5B%5Bnull%2C11557396%224%22%5D%0D%0A%2C0%5D%0D%0A%5D%0D%0A%2Cnull%2C%227634134997568976993%22%5D%0D"
link2 = "https://docs.google.com/spreadsheets/d/1W8A3UuFQTeEwk6-hqERvuIMT_HInySZTNBOIs/edit#gid=11262360"
Shell (chromePath & link)
ThisWorkbook.FollowHyperlink link2
End Sub

Turns out, the solution is considerably simpler, thanks to the Selenium Basic library, available here, with a lot of useful info about how it works:
https://codingislove.com/browser-automation-in-excel-selenium/
A note for your attention: this download needs Selenium GoogleChrome driver version 2.33, the current latest version, to resolve an error with Chrome starting up properly - at least in my case, I need Chrome and not IE working.
In any case, Selenium is a god-send for those in a similar situation. When you download and install Selenium, go into Tools --> References in the VB Editor, and enable the Selenium Type Library.
The following code will do the job:
Option Explicit
Dim myHTML_Element As IHTMLElement
Dim Driver As New WebDriver
Sub seleniumtutorial()
Dim pword As String
Dim link As String, link2 As String
pword = ThisWorkbook.sheets("Sheet1").Range("D10")
Driver.Start "chrome", "https://gmail.com"
Driver.Get ("https://gmail.com")
Driver.FindElementById("identifierId").SendKeys "user#company.com"
Driver.FindElementById("identifierNext").Click
'Driver.Get ("https://sso.diversey.com/nidp/saml2/sso?id=DIVAuthContract30&sid=0&option=credential&sid=0")
Driver.FindElementById("uname").SendKeys "Username"
Driver.FindElementById("pass").SendKeys pword
Driver.FindElementByName("loginButton2").Click
link = "https://docs.google.com/forms/d/e/1FAIpQLSeDHEKJmRDyMt7rdsbiFA/formResponse?entry.1155739640=7&fvv=1&draftR=%5B%5B%5Bnull%2C115B%224%22%5D%0D%0A%2C0%5D%%0A%5D%0D%0A%2Cnull%2C%227634134997568976993%22%5D%0D"
link2 = "https://docs.google.com/spreadsheets/d/1W8A3UuyeEwk6-hqERvuIMT_HInySBOIs/edit#gid=1126962360"
Driver.Get (link)
Driver.Get (link2)
End Sub
link and link2 are, respectively, the page that confirms a response has been submitted and link2 is the link to the spreadsheet that has the list of responses. You can divide this routine into two subs - one logs in the user, and the other creates an array of the items to be fed to google, at which points it just refreshes the same page with the new values, as it cycles through the loop or array.
One more link:
Selenium VBA - exit sub without close browser window
Making the variables public ensures that the browser window doesn't close at the end of the session, or you can declare them inside the first sub if you want it to close. As for credentials, you can either hardcode them or feed them from the spreadsheet, but that's a technicality. Beyond that, it seems like a pretty straightforward routine and it does the job really well.
The syntax of the Selenium library is decidedly more modern than VBA and I'd say quite a bit more powerful

Related

Uploading Excel file using VBA to SharePoint

Im struggling to resolve an upload problem direct from excel to sharepoint. I have the right permissions and can upload no problem when hardcoding the URL. Issue I have is all the SharePoint URL's are by Project Number and Description for example:
https://teamzzz.sharepoint.com/sites/Projects/NL001%20%20TEST/10.%20Dispatch
The project is called NL001 in this case. Another example of a URL could be:
https://teamzzz.sharepoint.com/sites/Projects/ZNP001%20%20ANOTHERTEST/10.%20Dispatch
Basically at the point of attempting to upload using my Add in I know the first part of the PATH:
https://teamzzz.sharepoint.com/sites/Projects/
The project id I can find from a cell reference:
NL001
The last part of the URL will always be the same as in:
10.%20Dispatch
My problem is the description after NL001 i.e. "%20%20TEST" (see below):
https://teamzzz.sharepoint.com/sites/Projects/NL001%20%20TEST/10.%20Dispatch
Is there anyway I can use some form of wildcard in the URL for example:
https://teamcde.sharepoint.com/sites/Projects/NL001*/10.%20Dispatch
OR
https://teamcde.sharepoint.com/sites/Projects/NL001%/10.%20Dispatch
The above 2 example dont work but was demonstrating the example I require. Here is my code thus far:
Public Sub CommandButton39_Click()
ProjectFileName = Range("B2").Value
SharePointBasePath = "https://teamcde.sharepoint.com/sites/Projects/"
SharePointEndPath = "10.%20Dispatch/"
ActiveWorkbook.SaveAs Filename:=SharePointBasePath & ThisFile & ".xlsm" & SharePointEndPath
End Sub
Is is possible to utilise a wildcard as the description is not known at time of upload.
Thankyou

How to detect if user clicks on File >> Print as option or presses CTRL+P from within MS word or Excel using VBA code?

I want to detect when user clicks File >> Print or CTRL+P inside ms word or excel and use this detection to run a batch file using vba code, is this possible?
This code should self start along with the program.
I tried to find similar code but was unable to find anything useful to my need.
Any help would greatly appreciated.
Thanks
The way your question is written seems ambiguous to me. At first read, it seems like you are trying to distinguish between these two methods of telling a file to print. I know of no way to do this in vba.
However, you can intercept the print event or command.
Another possible meaning is that you want your procedure to run whenever the user attempts to print. See Intercepting Events Like Save or Print by Word MVPs Dave Rado and Jonathon West. See also Application.WorkbookBeforePrint Event.
Note, this does not block screenshots or saving to another file. Do you mind sharing why you are trying to do this? What you hope to accomplish?
You can use the DocumentBeforePrint and WorkbookBeforePrint Events. Below quoted from linked pages on Intercepting Events and WorkBookBeforePrint documentation.
A DocumentBeforePrint event procedure looks like this:
Private Sub oApp_DocumentBeforePrint(ByVal Doc As Document, _
Cancel As Boolean)
'Your code here
End Sub
If you want to prevent printing from occurring in certain
circumstances, you can set the Cancel variable to True, e.g.:
Private Sub oApp_DocumentBeforePrint(ByVal Doc As Document, _
Cancel As Boolean)
Dim Result As Long
Result = MsgBox("Have you checked the " & "printer for letterhead paper?", vbYesNo)
If Result = vbNo Then Cancel = True
End Sub
From Excel documentation
This example recalculates all worksheets in the workbook before
printing anything.
Private Sub App_WorkbookBeforePrint(ByVal Wb As Workbook, _
Cancel As Boolean)
For Each wk in Wb.Worksheets
wk.Calculate
Next
End Sub
End Quoted Material
Intercepting the Command instead of the Event
Another, less effective, method is to Intercept the actual commands. You could name your procedure PrintPreviewAndPrint and have another called FilePrintQuick that calls your procedure PrintPreviewAndPrint. Earlier versions use FilePrint and FilePrintDefault. Thank #Timothy Rylatt for the command names. He adds: Note that neither of these will intercept the backstage command accessed via File | Print. For that you need to use an event.
Sub PrintPreviewAndPrint()
' Your code here
End Sub
Sub FileQuick()
FilePreviewAndPrint
End Sub
In Word, these would go in your template or in a Global Template.
In Word, you make a template a Global Template by placing it in your Word Startup Folder.
Dealing with making this Global in Excel
My understanding of how Excel handles global macros is far poorer than that for Word. To assist with this, I asked my own question in the Microsoft Answers Excel Programming forum. Here is a link to that question and the answers I received. Andreas Klinger, who is engaged in that thread, is an experienced and knowledgeable Excel programmer, which I am not.

Excel VBA hangs when expanding [+] workbooks object variable in locals window

I'm having a problem trying to debug some code I've written that opens a shared Excel file from SharePoint (in desktop Excel app), scrapes schedule data, makes a few changes, saves and closes that file, then populates the scraped data into the current workbook formatted as a calendar.
As is typical, the code executes fine for me, but only works about 50% of the time when another user runs it. I'm still searching for fixes to those bugs - they are not the reason for my question today. I'll try as best I can to describe my problem and hopefully someone can tell what I'm doing wrong.
First step I do is to check whether the SharePoint file exists and if so, open it (no problems/errors typically encountered here for myself or anyone else):
Sub getcalendardata()
...define variables...
Application.EnableEvents = False
Application.ScreenUpdating = False
schedule = "https://mycompany.sharepoint.com/sites/Schedule/Shared Documents/Schedule.xlsx"
If URLExists(schedule) = 0 Then
MsgBox "Schedule not found. Check internet connection, login to Office 365, and try again."
Exit Sub
End If
Set schedulewb = Workbooks.Open(schedule, False, False)
schedulewb.AutoSaveOn = False
Application.WindowState = xlMinimized
Set schedulews = schedulewb.Worksheets("Design")
...more code here...
Application.ScreenUpdating = True
Application.EnableEvents = True
End Sub
Function URLExists(url As String) As Boolean
Dim Request As Object
Dim ff As Integer
Dim rc As Variant
On Error GoTo EndNow
Set Request = CreateObject("WinHttp.WinHttpRequest.5.1")
With Request
.Open "GET", url, False
.Send
rc = .StatusText
End With
Set Request = Nothing
If rc = "OK" Then URLExists = True
Exit Function
EndNow:
On Error Goto 0
End Function
*** Occasionally, I run into issues with the cached file in MS Office's Upload Center becoming out-dated and asking to save a copy or discard changes, but that is a problem for another day.
The main problem I've come here for is that after stepping through the Workbooks.Open line, if I open the "Locals Window" to view the stored variables and try and expand the Workbook object "schedulewb" (Here:
Locals Window during code execution), code execution seems to stop or go into an infinite loop. It's like VBA is trying to expand the schedulewb object to display the properties, but isn't getting a response, so it just waits. I've not been patient enough to see if it ever self recovers.
The only thing I see is that the Locals Window schedulewb line goes blank (like it's trying to expand) and I can no longer step through, continue, or reset/end the code execution. I can however, break/pause the execution, at which point, schedulewb returns to the Locals Window list and I'm able to expand it with a very brief delay. Restarting or resetting code execution after this just puts the program into the previous stall pattern and the only recovery option (besides waiting potentially forever) is to close and restart excel via Task Manager. Interestingly, the actual Schedule Workbook I'm trying to peak inside now shows up in the Office Upload Center as a pending upload or having just been synced.
I've tried bypassing SharePoint by opening a local version of the Schedule Workbook saved to my computer, and as expected, I can interact with the Locals Window variables without consequence.
Is there something wrong with setting a SharePoint Workbook to a variable like I've done?
Has an upload/refresh been triggered by me trying to inspect the object properties, causing some other uncontrollable VBA/SharePoint event to infinitely loop in the background?
Could this be connected somehow to the Upload Center cache headache that I think is unrelated?
Thank you in advance for any insight...

Move to Element Syntax in Seleniumbasic - Excel VBA

I have looked across everywhere to find answers but could not get it, so would like your help in guiding me.
Selenium Basic (Excel VBA ) is giving me syntax error at the time of compilation.
My Objective is to move the mouse to a known element.
Here are the codes
Dim driver As New WebDriver
Dim actions As Selenium.actions
driver.Start "chrome", "http://anywebsite.com"
driver.get "/"
WebElement we = driver.FindElementByClass("MyClassName")
actions.MoveToElement (we)
It gives me an error at the compilation level as and FindElementByClass is highlighted. However in the watch window I can see values against it.
Compile Error: Sub or Function Not Defined
Would request if you can guide me since in the watch
Change
driver.actions.MoveToElement(we)
to
driver.actions.MoveToElement(we).perform

Unable to find the right excel file library reference error

Every time I open this Excel file I get this error which is in screenshot 1 and screenshot 2.
When I click OK it takes me to code windows and asks me to select library.
I tried everything to make it work but no luck.
Screenshot one and screenshot two:
Make sure that these references are in this Priority order.
HTML and Internet Controls need to be above OLE Automation.
If you are still having this issue after this, run a Repair on the Office install and it refreshes the .DLL files that may have been corrupted.
Update: 7/6/2017
The pervious answer above will re-associate the references and allow the script to compile.
However, there is a better way.
This issue occurs when the VBA script is shared to other computers that are not identical platforms(OS and MS Office versions) and use early binding. Early binding reduces latency and is the correct way when only intended for one computer.
The answer is to use late binding format in the script and not select any references. Change any data type objects other than object back to object and use the following format:
Sub Late_Binding()
Dim IE_App_obj As Object
Dim MyShell_obj As Object
Dim IE_Window_obj As Object
Dim Windows_cnt As Long
Dim x_cnt As Variant
Dim HTML_Element_obj As Object
Set IE_App_obj = CreateObject("InternetExplorer.Application")
'Use IE_App_obj to Navigate to webpage and control it.'
Set MyShell_obj = CreateObject("Shell.Application")
'Use MyShell_obj to find an existing webpage and control it.'
Let Windows_cnt = MyShell.Windows.Count
For x_cnt = Windows_cnt - 1 To 0 Step -1
On Error Resume Next
If Instr(MyShell_obj.Windows(x_cnt).Document.Title,"WebPage_Title") > 0 Then
Set IE_Window_obj = MyShell_obj.Windows(x_cnt)
Exit For
End If
Next
Set HTML_Element_obj = IE_Window_obj.Document.getElementByID("ID_text")
End Sub
Latency will increase but so will stability.

Resources