Sharepoint and Excel VBA integration failure - excel

I have encountered a problem when trying to retrieve file properties from SharePoint through VBA in Excel. (I can't post the workbook, but the below code should suffice).
The code in question:
Private Sub CheckCheckOutStatus()
Debug.Print Application.Workbooks.CanCheckOut("http://sp.mySharepointDomain.co.uk/myFolderPath/myFile.xlsb")
End Sub
The issue is that on my clients PC this statement always returns false regardless of whether the file is checked out or not (They are able to check out the file manually, so it isn't a file permissions issue).
Upon further investigation, it seems to be that my specific computer is able to get the correct value from this code and none other can. It's also worth mentioning that my client and all the PC's/Users I have tested this with are all on the same shared network and so we should all have the same packages installed.
Through process of elimination we have deduced that it is related to my specific computer (and it doesn't matter who logs into it, its the PC itself) that is able to use this method correctly.
My question is to all the experts out there:
Are there any client side or local packages/installations/permissions that could enable or disable programmatic access to the properties in SharePoint?
Thank you for taking the time to read this, and thanks in advance to any suggestions you might have!

I can't test this because I don't have SharePoint installed, but this looks about right...
Sub test()
Dim docCheckOut As String
docCheckOut = "Filepath&name"
Call UseCheckOut(docCheckOut)
End Sub
Sub UseCheckOut(docCheckOut As String)
' Determine if workbook can be checked out.
If Workbooks.CanCheckOut(docCheckOut) = True Then
Workbooks.CheckOut docCheckOut
Else
MsgBox "Unable to check out this document at this time."
End If
End Sub

Related

How to run application automatically using scanner "honeywell"

This is my code that I use. Please correct me if I'm wrong. I just really want to learn as freshman student
My project is using barcode I need to use scanner to read the code at the same time automatically open the application set on that code
Private Sub Button1_Click(sender As Object, e As keyPressEventArgs)
Handle textbox1.keypress
If e.keypress=ChrW(15)Then
Textbox1.text=""
end if
End sub
Also I use
Process.Start("d:\windows\notepad.exe")
It works, but my target is automatically open without clicking any button
I'm still searching for next code. Any idea is very much appreciated 🙏
Thank you
Documentation: https://social.msdn.microsoft.com/Forums/vstudio/en-US/685e9790-2f16-4141-9c01-9d8030035414/using-vbnet-to-run-external-applications?forum=vbgeneral
But generally it's the following command
proc = Process.Start("d:\windows\notepad.exe", "")

Is there a way to check if a connection in Excel is valid before refreshing it?

Quick Background:
I have almost no experience with VBA. I started learning this two weeks ago and have made fast progress, but I am lacking a lot of knowledge. I do not know enough to know what I do not know, for lack of a better word. My apologies up front if this is a question that has a very simple solution. I have been scouring Microsoft's VBA documentation with no luck for several days now.
The Problem:
I have a single connection in a main spreadsheet to a spreadsheet on SharePoint that pulls data from inputs by operators on their cell phones. The current way my code is set up, it will refresh that connection before running calculations to ensure that any new data is accounted for.
However, since the connected document's location (or even name) may change in the future due to it being a shared document, I wanted to find a way to test that the connection was valid; i.e. that the correctly named file existed where it was supposed to be.
Currently, if the file name or location do not match, Excel throws an error and my code will stop running. Ideally I would like to have something similar to this:
If connectionIsValid Then
Refresh the connection
Else
MsgBox "Could not refresh connection", vbInformation
*Continue with code instead of throwing an error*
End If
I have tried things like:
If ActiveWorkbook.Connections(1).OLEDBConnection.IsConnected Then
Run Code
End If
But after reading the documentation I realized the above is related to the MaintainConnection Property being true.
and:
On Error GoTo ErrorHandler
*Refresh the sheet*
*Continue code*
Error Handler:
*Don't refresh the sheet*
*Continue code*
Any help or even a pointer in the right direction is greatly appreciated. Thank you for your time.
I'm not sure what is wrong with your second suggestion - using an error handler.
Function checkConnection()
On Error GoTo errorHandler
ActiveWorkbook.Connections.Item(1).Refresh
Exit Function
errorHandler:
MsgBox "Could not refresh connection. " & Err.Description
End Function
I've just added the Exit command there, otherwise it will run the code in the errorHandler label every time, even if there isn't an error.

VBA EXCEL VBA How to enable (check) specific Type Library in Tools/References programmatically?

That's basically my question. Doing googling didn't return anything that I am looking for, but basically I am running SOLIDWORKS from Excel and for that I need "sldworks 2016 Type Library" and "SOLDIWORKS 2016 Constant Type Library" to be enabled. Of course you'd say to do it manually, BUT my program is being run both, by people with and without Solidworks installed and if a user doesn't have SOLDIWORKS on their PC - the entire thing won't even run. So I am looking to enable and disable those two type libraries upon necessity in the code.
Could, someone, please help me?
P.S. I am not looking for any workarounds etc.
BUT my program is being run both, by people with and without Solidworks installed and if a user doesn't have SOLDIWORKS on their PC - the entire thing won't even run.
Is this what you are trying? The below code will first try to bind with an open instance of SOLIDWORKS. If it is not open, then it will try to create a new instance. Obviously if SOLIDWORKS is not installed then the CreateObject will fail but the code will not crash because of On Error Resume Next. Finally check if objSolid is not nothing. This is late binding and you do not have to set any references.
Dim objSolid As Object
'~~> Establish an SOLIDWORKS application object
On Error Resume Next
Set objSolid = GetObject(, "SldWorks.Application")
'~~> If not found then create new instance
If Err.Number <> 0 Then Set objSolid = CreateObject("SldWorks.Application")
Err.Clear
On Error GoTo 0
If objSolid Is Nothing Then
MsgBox "SOLIDWORKS not installed"
Exit Sub
End If
'
'~~> Rest of your code
'
EDIT
You cannot say Solidworks is not properly documented without putting in the right efforts to search. It took me less than 30 seconds to find this SOLIDWORKS Example of Late Binding. Of course their code will fail if the user doesn't have SOLIDWORKS and that is because they have not done proper error handling. My answer above does that for you.
Their website has all the information that you need. You just need to put in the right efforts to search. As I mentioned in the chat below, when you convert the code into late binding, you will have to search for the value of those constants. No one will give them to you in a platter. :) You can either search Google with swDocPART Constant value or as #FunThomas pointed out, type ?swDocPART in Immediate Window to get the value when the reference to SOLIDWORKS has been established.

Constantly getting Err 1004 when trying to using Application.AddIns.Add

I'm trying to implement a bootstrap installer for my add-ins workbook, such that I can easily install the add-in for new users and send out updates. It works fine on my machine, but when having others test it, I get a runtime error when I try to call Set AI = Application.AddIns.Add(fileName:=fullPath, copyfile:=True). Specifically, the error is "1004: Unable to get the Add property of the AddIns class". I thought this was because the user needed to have "Trust access to the VBA project object model" enabled, but the error seems to occur even after they've toggled that box.
Other things I've checked:
The fullPath to the add-in is valid and the user can access the directory and the file
The user has the folder located at Application.UserLibraryPath
Any ideas?
Figured it out. It appears that the issue isn't one of permissions, but rather of whether a workbook is already open. Opening any workbook before running the Addins.Add prevented the error from occurring so I've simply added that into the program:
If Application.Workbooks.Count = 0 then Set wb = Application.Workbooks.Add()
Set AI = Application.AddIns.Add(fileName:=fullPath, copyfile:=True)
If not wb is nothing then wb.Close
Duke, perhaps it's the Trust Center settings on the recipients' machines. I have found this and may be helpful.
Best,
Danny
Check out VBA videos on ExcelVBADude on YouTube.

msoFileDialogFilePicker resulting in error

Please don't comment with anything about naming conventions, approaches, asking what the code is supposed to DO, or anything that isn't directly related to my issue:
This runs perfectly for me, everytime--A window pops up, and I select multiple Excel files and their data is uploaded into my sheet (Code not pictured). My client says he gets an error when he runs it, and naturally I assumed it was because he ran it on a Mac...but he says he gets the error on both PC and Mac. I can't recreate the error...and here we are.
Here's the code in question, the erring line highlighted in yellow:
Code for your copying:
Sub Import_Employee_Sheet()
With Application.FileDialog(msoFileDialogFilePicker)
.AllowMultiSelect = True
If .Show = True Then
End If
End With
End Sub
This is probably because he hasn't set the Microsoft Object [Version number] Library reference under Tools/References in the IDE, or because it's broken. Also see this post on how to fix the problem WITHOUT setting the object reference in order to avoid similar problems in the future.
Edit
It should read "...without setting the library reference" above.

Resources