I have a code, which task would be changing .url or .lnk (shortcuts) properties, but it does not seem to do anything.
Imports System.IO
Imports Shell32
Module Module1
Sub Main()
'References Microsoft Shell Controls and Automation.
'http://msdn.microsoft.com/en-us/library/bb776890%28v=VS.85%29.aspx
End Sub
Public Sub Change_Shortcut()
Dim shell As Shell32.Shell
Dim folder As Shell32.Folder
Dim folderItem As Shell32.FolderItem
Dim shortcut As Shell32.ShellLinkObject
shell = New Shell32.Shell
folder = shell.NameSpace("C:\Users\GrzegoP\Desktop\xxx") 'Shortcut path
If Not folder Is Nothing Then
folderItem = folder.ParseName("o2.url") 'Shortcut name
If Not folderItem Is Nothing Then
shortcut = folderItem.GetLink
If Not shortcut Is Nothing Then
shortcut.Path = "www.o2.ie" 'new shortcut address
shortcut.Save()
MsgBox("Shortcut changed")
Else
MsgBox("Shortcut link within file not found")
End If
Else
MsgBox("Shortcut file not found")
End If
Else
MsgBox("Desktop folder not found")
End If
End Sub
End Module
Can anyone give me some advice where am I going wrong?
Thanks.
It's noted here that you cannot create a URL shortcut that way:
http://msdn.microsoft.com/en-us/library/windows/desktop/bb776891%28v=vs.85%29.aspx
You should instead simply create a xxx.url file on the desktop, writing the following lines of text inside of it:
[InternetShortcut]
URL=http://www.o2.ie
Windows will turn that into a web-site shortcut.
From my understanding, it sounds like nothing is getting executed. Try putting a breakpoint at the beginning of the sub to see if it even gets called. If it does not, then make sure you're able to properly connect the Change_Shortcut() so it gets called appropriately.
Once you set up your breakpoint, you should be able to walk through the code and see where you end up to ensure it all works as expected.
A simple solution to my problem...
in the line: shortcut.Path = "www.o2.ie"
I forgot to put http:// in front of the address.
Related
Im using WPF and VB.net and in order to use a picture (source) for an image you need the file to be in the folder of the application, so the source of stack.png would be stack.png.
But I want to use C:/APictureLocation/stack.png. I think Releative file name maybe? I honestly do not know how to ask a question without explaining. Sorry.
So I can do:
image.source = "C:\APictureLocation\stack.png"
instead of
image.source = "stack.png"
Thanks for you help!
Do not vote down without giving me pointers. Doesn't help the community.
My answer!
Private Sub BrowseButton_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs)
Dim dialog As New OpenFileDialog()
dialog.InitialDirectory = "C:\"
dialog.ShowDialog()
picbox.Source = New System.Windows.Media.Imaging.BitmapImage(New Uri(dialog.FileName, UriKind.Absolute))
End Sub
I believe I have the solution to your problem (or what I think your asking)...
Let's say your image source folder is 'C:\Images\ProgramImages'
(notice how I have used a '\' not a '/' in my path...)
Firstly, create a new form called 'ImageBrowser'. Then, add a ListBox control and name it 'ImageBrowserLB'. After this, double click on the ImageBrowser form so it takes you to the code editor. In the auto-generated sub, type the following:
For Each F As File In System.IO.Directory.GetFiles("C:\Images\ProgramImages\")
If Path.Extension(F) = ".png" Then
ImageBrowserLB.Items.Add(Path.GetFileName(F))
End If
Next
When that form loads, it will add all the files with a PNG extension in the directory I specified. I'm sure you can take this code and manipulate it to do what you would like (which isn't very clear in your question!).
Hope this helped,
Rodit
Has anyone using VB.NET 2010 been able to create a single instance application?
I've followed the MSDN sample but it does not have an Application.xaml file.
Converting any C# samples to VB doesn't work as I cannot override the Main sub in Application.xaml (C# calls it App.xaml).
You can try using a Mutex. In the projects properties, disable the application framework and set Sub Main as the startup object. Then add a Module to your project:
Imports System.Threading
Module EntryPoint
Sub Main()
Dim noPreviousInstance As Boolean
Using m As New Mutex(True, "Some Unique Identifier String", noPreviousInstance)
If Not noPreviousInstance Then
MessageBox.Show("Application is already started!")
Else
Dim mainWindow As New MainWindow()
Dim app As New Application()
app.Run(mainWindow)
End If
End Using
End Sub
End Module
With this method, you will have to take care of your app's shutdown by calling the Shutdown method of the application.
Here's a few possible solutions. I'd look through the whole thread here before deciding on one. Make sure you backup your code before trying any of these, so if one doesn't work, you can try another.
http://social.msdn.microsoft.com/forums/en-US/wpf/thread/6c15b837-9149-4b07-8a25-3266949621a7/
I'm trying something out. I've got this system where a bunch of scripts are triggered at certain event handlers. This is used during the creation of Users in Active Directory.
So, what I think I need is a function that basically collects the properties of the user being processed into - hopefully, an object. And then returning that object and using it in other functions and subs. So that I can perhaps reach the properties I want using only "myObject.firstname" and that's it.
How can I create an object, create and set values in it and returning the entire object? Is this possible in VBS?
Edit: What would be best if I could create an Object that holds these properties and just being able to access them whenever I need to.
I'm basically after something like this:
Public Sub Main()
Set userStream = New objUser
msgbox objUser.firstname
msgbox objUser.lastname
msgbox objUser.username
End Sub
Edit: To explain furter, I have this library script which is called 'libGetUserProperties'
It's in this I want the function and Class. Then I just import this library script in the actual script that is run at runtime. Looks like this:
libGetUserProperties:
Public Function getobjUser(ByRef Request, ByVal From)
Dim thisUserObject
Set thisUserObject = New objUser
'If type = 0, data comes from AD
If From = 0 Then
thisUserObject.firstname = "some string"
thisUserObject.lastname = "some string"
End If
'If type = 1, data comes from Request stream
If From = 1 Then
thisUserObject.firstname = "some string"
thisUserObject.lastname = "some string"
End If
Set getobjUser = thisUserObject
End Function
Class objUser
Public firstname
Public lastname
Public username
End Class'
This is what the actual runtime Script looks like:
Dim libGetUserProperties
Set libGetUserProperties = ScriptLib.Load("Script Modules/Library Scripts/libGetUserProperties")
Sub onPreCreate(Request)
Dim userStream
Set userStream = New objUser
'Do whatever with objUser which contains all of the properties I'm after
End Sub
That’s not possible using VBScript, unless you want to dynamically generate the code for a class, including the properties, save that to file, and then execute that file as a script.
This is so much easier to do using JScript. Since Windows Scripting can handle JScript as well as VBScript, I’d recommend going that way.
If you definitely need VBScript, the best you can go for is a Scripting.Dictionary that would contain the properties as keys:
Dim dicUser
dicUser = CreateObject("Scripting.Dictionary")
dicUser("FirstName") = objUser.FirstName
which would allow you to access the first name as follows:
dicUser("FirstName")
Update
If you want to write a function which creates a dictionary and returns that, this is how to do it in VBScript:
Function RememberTheUser(Request)
Dim dicResult
Set dicResult = CreateObject("Scripting.Dictionary")
dicResult("FirstName") = Request.GetTheFirstName
Set RememberTheUser = dicResult
End Function
Which can then be assigned to some other variable:
Dim dicUser ' in the main section of the script, this means it's a global variable
Sub onPreCreate(Request)
' ...
Set dicUser = RememberTheUser(Request)
' ...
End Sub
Sub onSomethingLater()
WScript.Echo dicUser("FirstName") ' It's still accessible here
End Sub
In JScript, it’s a lot easier: just create a new object, and tack on new properties.
function readObject(dataSource) {
var result = {};
result.source = dataSource;
result.firstName = dataSource.firstName;
result['lastName'] = dataSource.FunctionThatGetsLastName();
}
Note that in JScript, object properties can be accessed using the object.property notation, but also through the object['property'] notation, which is very useful when you have dynamically-named properties. ('property' in the second notation is a string, and can be replaced by any expression that returns a string).
Keeping with VBScript, a disconnected recordset would work as well and is possibly faster than a dictionary; see here for an example.
Visual Studio 2008 - framework 3.5 - Visual Basic
Hi!
I have a problem with a static method invoked by reflection.
On the loading of my win-wpf I create a copy of "A4Library.dll" with the name "_temp.dll", in the same directory of the original.
Then, on a button-click event, I invoke a static method on the _temp.dll in this way:
Dim AssemblyFileName As String = Directory.GetCurrentDirectory() & "\_temp.dll"
Dim oAssembly As Assembly = Assembly.LoadFrom(AssemblyFileName)
Dim TypeName As String = "MyLibrary.MyService"
Dim t As Type = oAssembly.GetType(TypeName)
Dim mi As MethodInfo = t.GetMethod("MyMethod", BindingFlags.Static AndAlso BindingFlags.Public)
Dim bResponse As Boolean = mi.Invoke(Nothing, New Object() {MyPar1, MyPar2})
But this works well only if I the .exe file is not in the same directory of the .dll files, otherwise I obtain this error (translated):
InnerException {"Cast impossible of [A]MyType on [B]MyType. The type A is originated from ... in the context 'Default' in the position 'F:\MyPath\A4Library.dll'. The type B is originated from ... in the context 'LoadFrom' in the position 'F:\MyPath_temp.dll'."}
It's strange: it seems to be a conflict with the same method in the original .dll, but I can't undertend why it looks at the original and not at the copy. If the .exe file relative to the principal assembly is placed in another directory, all runs well.
I neet to have the .exe in the same folder of the .dll, how can I solve the problem?
Thank you!
Pileggi
Why create a copy of assembly before executing static method? If creating a copy is needed, load that assembly in another AppDomain and execute the method in there.
I have several commandbuttons in different sheet. Now I want the user to be able to run all at once if wanted. Therefore I want to use a new CommandButton that runs all CommandButtons in all Worksheets. However I allways get a error message..I have to approaches and have not worked out for me:
running the subs:
Private Sub CommandButtonX_Click()
run CommandButtonY_Click
run CommandButtonZ_Click
etc.
end sub
..Nothing happens
My other Approach is to select all worksheets and "activate the commmand button", which is always called CommandButton1_Click however I still don´t know how to do it
This was my approach:
Sheets(Array("AA", "BB", "CC",etc.).Select
Sheets("AA").ActivateCall
Call CommandButton1_Click
End Sub
I am new at this, so I would really apreciate your Help!
Thanks!
Jens
This is purely a design opinion, but I would try and tackle this problem a different way.
My preference would be to have my subroutines in a separate module and simply call them from a button. That is, the _Click event simply looks something like this:
Private Sub Command1_Click()
Call DoSomeStuff
End Sub
While in a separate module all of my subs are grouped accordingly:
Sub DoSomeStuff()
Sheet1.Range("A1").Value = "Hooray for VBA"
End Sub
Using this method you avoid having to work with VBA's sometimes wonky (In my opinion) event handler and you can re-use subs with the appropriate arguments if they're called by several different buttons.
Then your "Master Button" just calls all the subs:
Private Sub Command1_Click()
Call DoSomeStuff
Call DoSomeMoreStuff
Call DoTheFinalStuff
End Sub
What error message do you get?
What version of Excel are you running?
This code runs fine in Excel 2007 on Windows 7:
Sub Button1_Click()
MsgBox ("Hey there, I'm button 1")
End Sub
Sub Button2_Click()
MsgBox ("Hey there, I'm button 2")
End Sub
Sub Button3_Click()
MsgBox ("Hey there, I'm button 3")
End Sub
Sub Button4_Click()
Button1_Click
Button2_Click
Button3_Click
End Sub
I have 3 buttons, each on a different worksheet, and they all display a message that identifies them. The 4th button is also on a separate worksheet and it displays the other 3 messages one after the other as would be expected.
Can you attach the error message to your post?