I'm relative beginner in programming, in school i studied C, at work i started to learn VBA by myself.
I have a lot of tasks, which are not that hard, but takes a lot of time, like creating reports, find some data in excel sheets, sometimes write a code which can control measuring devices through GPIB, etc.
Altough, i'm not allowed to install any program in PCs i use (it's not banned, but it takes too many time and effort, to get permission for every single software), i decided to write programs in VBA, because it's built in MS Word and Excel.
Recently, i started think that it was more elegant to create exe, than do every task with Excel sheets with macros in them.
I looked it up, if there is any way to convert my VBA code to exe, but it's not the best solution, there are 3rd party converters with advertisements, or softwares i should buy... And i'm still in trouble, because i can't install them at my workplace.
So i decided to change to VB.Net, because it's seems similar to VBA, and i thought there is an easy way to create an exe from VB.Net code via Visual Studio.
Yep, i find the exe, but it need the other files, which are created to the same directory (and maybe even Visual Studio installed?), but my purpose to create a single exe, which i can transport via e-mail or by usb flash.
Is there a solution to create an exe with Visual Studio, which doesn't need anything else?
If there is possible, in Visual Studio, how can i create it?
If it's not possible, or too complicated, what else language should i try with?
Thanks a lot!
I created a simple form, with some very basic code, and tried to run the exe out of the original directory. In the original directory it can run, but if i copy it to somewhere else, it can't.
CodePudding user response:
If you simply want to create a simple Windows GUI app using Visual Basic and Windows Forms (which is somewhat similar to UserForms in VBA which you might have used before) and get it up and running as fast as possible, here is what you will want to do:
- Open Visual Studio. Go to New Project, and select "Windows Forms App (.NET Framework)". (Make sure it says ".NET Framework", as the .NET Framework is included by default since Windows 7, since you want it to run on another computer.) Type a name for your project and create it.
- You will see a blank form. Go to View>Toolbox, and make sure the Toolbox is shown. The toolbox contains a variety of controls which you can place on the form.
- For example, try dragging a button onto the form. Once you have placed a button, double click on it, and type in some code in its Sub, such as
MsgBox("Hello world!")
. - Press the green play button at the top to compile your program and run it.
- If you want the Release
.exe
file to send to your friends and run, change "Debug" to "Release" in the drop-down menu, and go to Build>Build Solution. Go to the folder where your project is, and find the folder called "Release". You will find your.exe
in that folder. That.exe
will run on any computer with the .NET Framework version you selected, and .NET Framework 4.8 should be installed automatically on Windows 7 and up via Windows Update, so you should have no problem running your program on another computer.
You will want to search the Internet for further Visual Basic .NET and Windows Forms (WinForms) tutorials if you are interested.
Happy coding!
CodePudding user response:
Thanks for all of the helps, comments, and sorry for making offtopic with my question, and being unclear.
I find a solution here: https://docs.microsoft.com/en-us/dotnet/core/deploying/single-file/overview
The key movements are the follow steps:
In the projectname.vbproj file at the PropertyGroup section i had to add the following lines:
<IncludeAllContentForSelfExtract>true</IncludeAllContentForSelfExtract>
<PublishSingleFile>true</PublishSingleFile>
<SelfContained>true</SelfContained>
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
<PublishReadyToRun>true</PublishReadyToRun>
The first one is for being framework independent, the other 4 is for including the other files, to create a single file.
At Publishing... I chose "Self contained" deployment mode, chose the proper target runtime and put a tick in "Procedure single file".
The result is a huge exe file. Its not optimal, in most cases it is not lifelike that anyone would like to create a monster like this, but it does what it was created for. (Tested in another pc, which has no .Net installed, it worked properly.)
I hope, it will be helpful to anyone who has a similar problem.
CodePudding user response:
HotkeyToWindow.exe Assigns a hotkey to a window. When pressed will activate that window.
This uses the inbuilt compilers in Windows 10 - there are three VB.NET compilers and three C# compilers - just copy each text file into the same folder and double click the batch file to make the program.
There are two types of hotkeys in Windows. RegisterHotkey requires a program to expect it. WM_SetHotkey is supported by the default behaviour of a window so the program doesn't need to be aware of it.
Hotkeys are system keys so work all the time. To take the example below, using sendkeys to send Ctrl N to any application will activate Notepad. This is one way around restrictions on setting the active window.
REM HotkeyToWindow.bat
REM This file compiles TopMost.vb to TopMost.exe
REM HotkeyToWindow.exe sets a hotkey to activate the window
REM To use
REM HotkeyToWindow <Modifier> <virtualkey> <Windowtitle>
REM E.G.
REM To assign Ctrl (2) and N key (78) to notepad
REM HotkeyToWindow 2 78 Untitled - Notepad
REM To remove the hotkey send 0
"C:\Windows\Microsoft.NET\Framework\v4.0.30319\vbc.exe" /target:winexe /out:"%~dp0\HotkeyToWindow.exe" "%~dp0\HotkeyToWindow.vb"
pause
'HotkeyToWindow.vb
Imports System
Imports System.IO
Imports System.Runtime.InteropServices
Imports Microsoft.Win32
Public Module TopMost
Public Declare UNICODE Function FindWindowW Lib "user32" (ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
Public Declare UNICODE Function SendMessageInt Lib "user32" alias "SendMessageW" (ByVal hwnd As IntPtr, ByVal wMsg As Integer, ByVal wParam As Integer, lParam as Integer) As Integer
Public Const WM_SETHOTKEY = &h32
Public const HOTKEYF_SHIFT = 1
Public const HOTKEYF_CONTROL = 2
Public const HOTKEYF_ALT = 4
Public const HOTKEYF_EXT = 8
Sub Main()
On Error Resume Next
Dim hWindows as IntPtr
Dim CmdLine as String
Dim Ret as Integer
Dim vKey as UInteger
CmdLine = Command()
Dim A as String()
A = Split(CmdLine, Chr(32), 3, 1)
hwindows = FindWindowW(vbNullString, A(2))
If hwindows = 0 then
Msgbox(A(2) & " cannot be found.")
Else
vKey = (cByte(A(0)) * &h100)
vKey = vKey cByte(A(1))
Ret = SendMessageInt(hWindows, WM_SETHOTKEY, vkey, 0)
If Ret = -1 then MsgBox("Invalid Hotkey")
If Ret = 0 then MsgBox("Invalid Window")
End If
End Sub
End Module
From https://winsourcecode.blogspot.com/2021/03/hotkeytowindowexe-assigns-hotkey-to.html