Receiving an error from trying to make a startup program shortcut - vb.net

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.

Related

Cannot find the error in code for identifying if email account exist

In the RPA software Blue Prism, I am trying to write some VB.Net code for identifying if an account (i.e. Account_Display_Name) is configured to the desktop Outlook application (including shared mailboxes). The code utilizes Microsoft.Office.Interop.Outlook.dll, and the internal compiler does not identify any errors. However, when I try to run the code, I get the error "Object variable or With block variable not set".
I cannot find what is causing the issue, and would really appreciate your support. Perhaps there is a better way altogether for achieving what I want...
Dim olApp As Object
Dim olNamespace As Object
Dim Account_Exist As Boolean
Dim Account_Display_Name As String
olApp = CreateObject("Outlook.Application")
olNamespace = olApp.GetNameSpace("MAPI")
Account_Exist=False
If Not String.IsNullOrEmpty(Account_Display_Name) Then
Dim aa As List(Of Account) = olNamespace.Accounts.Cast(Of Account).ToList()
For Each x As Account In aa
If x.DisplayName = Account_Display_Name Then
Account_Exist=True
Exit For
End If
Next
End If
I have tried to put the code into online compilers to see if I can get some leads, but to no prevail.
The logic in your loop doesn't appear that it will ever execute. Is that intentional? Dim Account_Display_Name as String will initialize to string.empty and when it hits the if statement, String.IsNullOrEmpty(Account_Display_Name) will return true and then be negated by your not and your inner block will be skipped. (assuming this is a complete excerpt and nothing was ommitted for brevity. If someone was removed I would encourage you to edit your OP and update it with the complete code sample.)
With the above in mind, we know that the for loop logic is not getting hit so we know the issue is likely occurring at olApp.getNameSpace("MAPI"). The most likely culprit is olApp is nothing while you are trying to reference one of its functions.
You can confirm which with a break point and stepping through the debugger.
I have tried to put the code into online compilers to see if I can get some leads, but to no prevail
This is indeed the trouble with logic bugs as syntactically, your code appears to be correct.

SolidWorks VBA - Translating API Help into useable code

I'd like to do what feels like a fairly simple task, and I've found the specific API Help pages which should make it clear, but, I can't actually make things work.
The Key steps that I would like to achieve are:
Rename the active document
Update References to this document to accommodate new name
Save active document.
This help page shows the Usage for renaming the doc, and under the "Remarks" heading, includes links to the next two steps, mentioning them off hand as if implementing them would be easy.
https://help.solidworks.com/2020/English/api/sldworksapi/SolidWorks.Interop.sldworks~SolidWorks.Interop.sldworks.IModelDocExtension~RenameDocument.html?verRedirect=1
The trouble is, I'm a bit of a VBA beginner - usually I get by with the 'record' function, and then tidying things up from there - but undertaking the steps above manually doesn't result in anything being recorded at all for one reason or another.
Assuming I am able to pass in the item to be renamed (I'll define a variable at the start of the Sub for this e.g. swModel = swApp.ActiveDoc), and the new name (NewName = "NEW NAME HERE"), How would I translate the Help API into a Sub that I can actually run?
Two of them suggest declaring as a Function, and one as a Public Interface - I've never used these before - do these just run in a standard Module? Do I need to write a 'master Sub' to call the different functions sequentially, or could these be included directly in the sub, if they're only to be used once?
[Feeling a little lost - it's demoralizing when the help files aren't all that helpful]
Let me know if there's any more information missing that I can add to improve my question - as I said, I'm fairly new to this coding thing...
The "record" function is sometimes a good point to start but there are a lot of functions it can't recognize while you execute them manually.
The API Help is then useful to find out how to use a specific function.
In almost every example the use of a specific method (e.g. RenameDocument) is only shown abstract. There is always a instance variable which shows you the object-type needed to call this method. So you can use these in every sub you want, but beforehand need access to the specific instance objects.
For your example the RenameDocument method is called with an object of the type IModelDocExtension. First thing for you to do is to get this object and then you can call the method as described in the help article.
Under Remarks in the article you find additional information for what you maybe have to do before or after calling a method.
For your example it is mentioned that the renaming takes permanently place after saving the document.
And finally here is what you want to do with some VBA code:
Dim swApp As SldWorks.SldWorks
Dim swModel As ModelDoc2
Sub main()
' get the solidworks application object
Set swApp = Application.SldWorks
'get the current opened document object
Set swModel = swApp.ActiveDoc
' get the modeldocextension object
Dim swModelExtension As ModelDocExtension
Set swModelExtension = swModel.Extension
Dim lRet As Long
lRet = swModelExtension.RenameDocument("NEW NAME")
If lRet = swRenameDocumentError_e.swRenameDocumentError_None Then
MsgBox "success renaming"
Else
MsgBox "failed with error: " & lRet
End If
End Sub
Afterwars you have to process the return value to check for errors described in this article: https://help.solidworks.com/2020/English/api/swconst/SolidWorks.Interop.swconst~SolidWorks.Interop.swconst.swRenameDocumentError_e.html

IsNothing showing error VB.NET VS2017 Community

My first post here and really basic question as I have just started learning.
I am following a step by step tutorial from youtube. https://www.youtube.com/watch?v=6utWyl8agDY code is working alright in the video. below is the code:
Imports System
Imports System.IO
Module Program
Sub Main()
Dim myReader As StreamReader = New StreamReader("values.txt")
Dim line As String = ""
While Not IsNothing(line)
line = myReader.ReadLine()
If Not IsNothing(line) Then
Console.WriteLine(line)
End If
End While
myReader.Close()
Console.ReadLine()
End Sub
End Module
Problem I am facing is that I have red error (squiggly) line under IsNothing. One thing to note is the in the video tutorial version of VS under use is 2013 while I am using VS2017 community. any idea what I am doing wrong here?
I'm not sure what your tutorial says but there are issues with that code. It's fairly poorly structured and, to be honest, you shouldn't really use IsNothing anyway. The most immediate issue is where you're reading the text and that you're testing for Nothing twice. The code would be better written like this:
Dim myReader As New StreamReader("values.txt")
Dim line As String = myReader.ReadLine()
While line IsNot Nothing
Console.WriteLine(line)
line = myReader.ReadLine()
End While
myReader.Close()
Console.ReadLine()
Comparing directly to Nothing using Is or IsNot is the way to go and there's also no need for that check inside the loop.
If you want to get a bit more advanced, you can also create the StreamReader with a Using statement, so it gets closed implicitly:
Using myReader As New StreamReader("values.txt")
Dim line As String = myReader.ReadLine()
While line IsNot Nothing
Console.WriteLine(line)
line = myReader.ReadLine()
End While
End Using
Console.ReadLine()
Better still would be to not create the StreamReader yourself at all but let the Framework do that for you:
For Each line As String In File.ReadLines("values.txt")
Console.WriteLine(line)
End Using
Console.ReadLine()
Note that that code uses ReadLines rather than ReadAllLines. Both would work but the former will only read one line at a time while the latter will read the whole file first, load the lines into an array and return that. In this case it probably doesn't really matter which you use but ReadLines is generally preferable unless you specifically need all the lines first or you want to loop over them multiple times.
You have to decide whether your console application requires .net Core or .net Framework is sufficient.
As others have written in responses, your code is in need of improvement.
However, your code will run correctly if you choose the .net framework for your new project in Visual Studio 2017 Community as shown in the screenshot below.

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

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.

Excel VBA: Manipulate Shockwave Flash Property

I'm working on a little project to make a 'memeplayer' in Excel with YouTube videos - here's a screenshot of the spreadsheet
The code I'm attempting to use is below:
Option Explicit
Sub PlaySamsMemes()
Dim sChosenTitle As String
Dim rngVideoData As Range
Dim lFinalRow As Long
Dim sLookupValue As String
lFinalRow = Range("C65536").End(xlUp).Row
sChosenTitle = Range("F4").Value
Set rngVideoData = Range("B2:C" & lFinalRow)
sLookupValue = Application.VLookup(sChosenTitle, rngVideoData, 2, False)
Memeplayer.SetVariable("Movie", sLookupValue)
End Sub
I'd be really grateful if you could tell me how to get the script to play nice with the embedded Shockwave Flash that I've named 'Memeplayer' in the Excel interface. The line I'm using to try and make it run is:
Memeplayer.SetVariable("Movie", sLookupValue)
but it just returns an 'Object required' error (Runtime Error 424). I've not worked with shapes and objects much as yet and I've no idea how to fix it. Help me please!
I've since solved the problem. I just hadn't Set the Object correctly - couldn't figure out the heirachy for it. The code is below:
Set MemePlayer = Sheets(1).MemePlayer
MemePlayer.Movie = sLookupValue
VBA has many limitations and trying to do something like this in Excel is probably not the best way to go about it. However, here are 2 websites that might shed some light on your problem:
http://chandoo.org/wp/2011/01/11/embed-youtube-videos-excel/ - this should help most I think
http://www.cpearson.com/excel/DownloadFile.aspx (from the website: http://www.pcreview.co.uk/forums/do-use-vba-play-sound-clip-website-t3893819.html)
Hope this helps.