Answer this and you're a true genius: Error removing then re-adding missing reference - vba

Dim brokenRef As VBIDE.Reference
Dim refrnc As VBIDE.Reference
For Each refrnc In vbProj.References
If refrnc.GUID = "{C94A4194-E621-404A-8E20-447E4D415ABD}" Then
found = True
If refrnc.IsBroken Then
found = False
Set brokenRef = refrnc
End If
End If
Next
If Not found Then
If Not brokenRef Is Nothing Then vbProj.References.Remove brokenRef 'THISLINE'
vbProj.References.AddFromFile ThisWorkbook.Path & "NuancePDF.tlb"
End If
I am looking for a missing reference and then attempting to add it. I got some error when adding it, so I figured it must be because the reference is there, it is just broken, so I try to remove the reference and I get the error on that line (this line). The error description is given in the image. As background, I want to automate the process of adding references since some machines cannot find them. This runs in the workbook_open sub so it seems to run this sub before compiling the rest, which relies on the references and so for some reason I am not getting a compile error because of a missing reference, but if it works idc.
I read online about running regedit in the cmd, and then deleting some stuff. However I do not fully understand what they were doing and I am working on company computers and so I do not want to risk messing anything up.

Sort of solved, see the comments. The link posted by Tim Williams gives a good idea which is to start without the reference and then add it, which I guess is maybe similar to late binding in a way. As for getting what I wanted to explicitly work it seems that you have to register it via regsvr32 in the cmd, which is not working for me.

Related

Receiving an error from trying to make a startup program shortcut

I am trying to make a shortcut for my program so that it starts automatically with windows. The following code is the sub that creates the shortcut and how it gets it's variables but it fails.
Dim ShortCutPath As String = Environment.GetFolderPath(Environment.SpecialFolder.Startup) & "/ScreenRot.lnk"
Shared Sub CreateShortCut(File As String, ShortCutPath As String)
Dim oShell As Object
Dim oLink As Object
'you don’t need to import anything in the project reference to create the Shell Object
Try
oShell = CreateObject("WScript.Shell")
oLink = oShell.CreateShortcut(ShortCutPath)
oLink.IconLocation = File
oLink.TargetPath = File
oLink.Arguments = ""
oLink.WindowStyle = 1
oLink.Save()
Catch ex As Exception
End Try
End Sub
The line it seems to fail on is.
oLink = oShell.CreateShortcut(ShortCutPath)
The error I am getting is
DirectCast(ex, System.MissingMemberException).Message
Public member 'CreateShortcut' on type 'IWshShell3' not found.
I am using this in my program.
Imports IWshRuntimeLibrary
I have tried a couple different ways to make the shortcut but this seems to be the one that should work for what I need. I've read a bit about using this code and watched a video but nothing talks about the error. I've googled the error but nothing resembles a solution. I've tried to adjust the code slightly by using other examples but it still fails with more or less the same error. I don't really understand what the error is saying, so I can try and figure it out. thanks for your time and any help you guys can give.
After reviewing many posts and trying a lot of different things I found a solution on Stackoverflow which I can confirm actually works.
Stackoverflow Post
This post has the solution, hopefully it helps other people with this problem.

MS Access Run Time Error 2465 Can't find the field 'employeeNum' referred to in your expression

Just finished tweaking a query, then started testing the change and then this issue popped up.
In a Module called GLOBALS I declare a number of public variables. One of which is this:
Public employeeNum As Long ' PO entering/modifying data
In form frmSearch I assign this variable. For some reason I am getting a run time that I catch in my Form_Load() procedure.
Here is a pic of when I stop at the breakpoint and hoover over the global variable employeeNum.
Any thoughts on this strange error?
Attempts to fix include the following:
Ran Compact and Repair Database a couple of times and no luck.
Ran /decompile to clean things up. No luck.
Compiled Database to validate no issues and no errors detected.
Changed Public to Global when declaring employeeNum. No luck.
Tried to debug.print employeeNum early in the procedure but I still would get the runtime error on the debug.print line.
Thanks
This problem is caused by two variables being named the same. A global variable and a local variable (employeeNum). The various replies gave me the clue I needed and I used the Object Browser to validate the problem. When I tried to find the object, the Object Browser stated it was hidden which is annoying but I’ll go through all the embedded code, etc. and figure out where it is just so I know.
I renamed the global variable employeeNum to employeeNumber throughout the application and everything works fine.

Explicit "WorkSheet.Range" Fails With “Method or Data Member Not Found”

I have the following code:
Dim newWorksheet As Worksheet
Set newWorksheet = ThisWorkbook.Sheets.add()
Set pivotCache = ThisWorkbook.PivotCaches.create(SourceType:=xlExternal, SourceData:=cnSet)
pivotCache.CreatePivotTable newWorksheet.Range("A1"), newWorksheet.Name
Works great 99% of the time, however today a customer sent me a version of the file that was not working, when I open the file and attempt to compile the code I get the following issue with the last line of code:
Method or Data Member Not Found
In fact you can even see that the property is missing in the autocomplete:
It appears as though it's recognizing the "Worksheet" object as a "chart":
Strange, so I covert the "newWorksheet" over to a variant, compile, works great. Now I change it back, compile, and... it works, issue is gone, code runs great. Very strange, you can even see it in the autocomplete:
(Now it's object type "Worksheet")
I don't understand what would cause this or how to prevent it in the future, the issue only occurs with this one file. The user was not doing anything exceptional with it as far as I can tell. This issue is documented but typically is related to other problems. I don't see any explicit reference to this occurring on a per file basis.
Does anyone have any insight on this one, this is one of those "only in VBA" type of issue?

"Target Framework" compatibility

I am having a problem. The following code should output a single line describing my local instance of SQL. After lots of poking and prodding I have found that the code succeeds when it is compiled with the "Target Framework" set to v3.5, but it fails to return any instances when the "Target Framework" is set to anything higher. There is no error, exception, warning or other explanation. I know that it is not simply taking longer to find the instance because it reaches the final "Console.Readkey()" within .04 seconds when the target is v3.5.
I suppose what I really want to know is: How can I make this work without changing the Target Framework? I would rather not if I don't have to since I have written the rest of my project under the default (v4.5.2) and don't know what consequences might arise from doing so.
P.S. Bonus points if you can tell me why this doesn't work after v3.5.
Module Module1
Sub Main()
Dim datatable As DataTable = System.Data.Sql.SqlDataSourceEnumerator.Instance.GetDataSources()
For Each row As DataRow In datatable.Rows
For i As Integer = 0 To (datatable.Columns.Count - 1)
Console.Write(row.Item(i) & vbTab)
Next
Console.WriteLine()
Next
Console.ReadKey()
End Sub
End Module
Unfortunately, you appear to be experiencing this reported bug.
If it looks like the bug report matches your problem, consider voting to have it fixed.

Colleagues get error 5 in Excel-file with VBA, but same file works fine on my computer?

We have an Excel-file with a large amount of VBA behind it. The Excel-file works just fine on my computer, but so far 3 of my colleagues (non-IT'ers) have gotten this error:
Runtime error 5:
Invalid procedure call or argument
The error is located on this line, and I don't see why it is throwing an error there because it's just a simple Set (and it works perfectly fine on my computer):
Set MyButton = Application.CommandBars("Attributions").Controls.Add(Type:=msoControlButton, Before:=10)
We all have the exact same Excel-file. I even sent them my version of the file, in which everything is running fine and no errors happen, but even when they open my version of the file they still get the above error on the above line!
What exactly could cause this? We all have Office 2013 and updates are installed automatically. The problem started about 2 weeks ago with one colleague and since this week I heard from two other colleagues that they have the same problem. One even said it suddenly worked again after he moved to a different desk (which I doubt would have an influence) but shortly after, it started getting the error again.
I have absolutely no idea why they get the error, or what might cause it. Seeing as we now all have the same version and they still get the error, I am thinking it might have something to do with Excel itself but that's just my idea.
Does this sound familiar to anyone here? Or does anyone know what might cause this, and how it can be fixed?
Edit: a while ago I checked on my colleagues their computers to see if the CommandBar was present, and it was. Even then the error still happened.
I suggest you to use a function that checks if there is a CommandBar in your Application like this:
Function IsCommandBarValid(cbName As String) As Boolean
Dim i As Long
IsCommandBarValid = True
For i = 1 To Application.CommandBars.Count
If (Application.CommandBars(i).Name = cbName) Then
Exit Function
End If
Next i
IsCommandBarValid = False
End Function
Now, You can use it to see that your user have that CommandBar in his or her Application, then make it like this:
If (Not IsCommandBarValid("Attributions")) Then
Call Application.CommandBars.Add(Name:="Attributions")
End If
' And after this add your code
Set MyButton = Application.CommandBars("Attributions").Controls.Add(Type:=msoControlButton, Before:=10)
Invalid procedure call or argument (Error 5)
Some part of the call can't be completed. This error has the following causes and solutions:
An argument probably exceeds the range of permitted values. For example, the Sin function can only accept values within a certain range. Positive arguments less than 2,147,483,648 are accepted, while 2,147,483,648 generates this error.
Check the ranges permitted for arguments.
This error can also occur if an attempt is made to call a procedure that isn't valid on the current platform. For example, some procedures may only be valid for Microsoft Windows, or for the Macintosh, and so on.
Check platform-specific information about the procedure.
For additional information, select the item in question and press F1 (in Windows) or HELP (on the Macintosh).
MSDN Source Article