what wrong with this code?. Converting VBA to VB.Net - vba

I would like to ask if someone can help me with this.
I have this code that Unload a CorelDRAW Macro project a get the new one from the network in order to became easier to update several machines.
VBA code:
Sub CDRMacroUpdate()
' Unloads the project from CorelDRAW
GMSManager.Projects("Project1").Unload
Dim nMacro As String
' The project have the same name, but it's a new file, here is in the C drive, but it's located in the network folder
nMacro = "C:\Project1.gms"
' Loads the new file and copy to the User GMS folder
GMSManager.Projects.Load nMacro, True, False
End Sub
Now I'm trying to convert this VB.Net but I'm getting a lot of errors and I can't see what I'm doing wrong.
In a form with a Button I have this code:
I'm using SharpDevelop
Imports Corel.Interop.VGCore
Public Partial Class MainForm
Public Sub New()
' The Me.InitializeComponent call is required for Windows Forms designer support.
Me.InitializeComponent()
End Sub
Sub BtnUpdateMacroClick(sender As Object, e As EventArgs)
Dim GMSManager As Corel.Interop.VGCore.GMSManager
GMSManager.Projects("Project1").Unload
Dim nMacro As String
nMacro = "C:\Project1.gms"
GMSManager.Projects.Load(nMacro, True, False)
End Sub
End Class
Now, can someone please explain what I'm doing wrong in the VB.Net code?
Thank very much for the help.

Related

Missing object reference in Outlook add-in (VB)

First of all, I am not a professional developer, I code only for fun so there are many, many things I don't know.
I created an Outlook add-in in VS Community 2019 in VB that is working fine. In that add-in, I manipulate a text string variable.
I need to update the add-in so that this text string variable is Base64 encoded.
Below is the overall simple structure of the add-in and the code that I added to Base64 encode the variable "SearchTerm"
Public Class ThisAddIn
Private Sub ThisAddIn_Startup() Handles Me.Startup
End Sub
Private Sub ThisAddIn_Shutdown() Handles Me.Shutdown
End Sub
Public Sub SearchText()
Dim SearchTerm As String
Dim data As Byte()
Dim EncodedSearchTerm
SearchTerm = "Text to encode"
data = UTF8Encoding.GetBytes(SearchTerm)
EncodedSearchTerm = Convert.ToBase64String(data)
End Sub
End Class
I get an error on the line data = UTF8Encoding.GetBytes(SearchTerm) that says
Class System.Text.UTF8Encoding
BC30469: Reference to a non-shared member requires an object reference
I have seen that UTF8Encoding.GetBytes() belongs to System.Text Namespace. It seems that this is already referenced in my solution. I guess I need to add/create an object reference in the code (but I'm not sure what that really means), or my code is completely wrong!
I would appreciate help!
Thanks

Is there a way to input a text from textbox1.text to input box of a website? I'm using a chromium browser in Visual Basic

I'm trying to make a program in which I can input my username from my Visual basic project Textbox1 to facebook input box username. I'm using chromium from CefSharp for webbrowser. I got the id through 'view page source' which is 'email'. but what I'm not sure if my line of code is correct.
Here are my code so far:
Imports CefSharp
Imports CefSharp.Web
Imports CefSharp.JavascriptBinding
Imports CefSharp.Event
Public Class Form2
Private WithEvents browser As ChromiumWebBrowser
Sub New()
InitializeComponent()
InitializeChromium()
End Sub
Private Sub InitializeChromium()
Dim settings As New CefSettings()
CefSharp.Cef.Initialize(settings)
Dim browser As New ChromiumWebBrowser("https://www.facebook.com")
Panel1.Controls.Add(browser)
browser.Dock = DockStyle.Fill
End Sub
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
browser.ExecuteScriptAsync("document.getElementById('email').value='ansdew#gmail.com'")
End Sub
When I run this, I get the error message
Object reference not set to an instance of an object.'
The line of code which this exception
browser.ExecuteScriptAsync("document.getElementById('email').value='ansdew#gmail.com'")
I don't know if the inside of this
ExecuteScriptAsync
is correct and I need to find the correct script so I can input my username/email using my VB project.
Let me know also if I need to include some lines of code.
Thanks!

vb.net Parameters Startup Form not hiding?

I am creating a application to be used with a touch panel device. The touch panel device comes with a standard windows OSK (On screen Keyboard). Whilst testing its been concluded that the standard OSK is to large and too complex for what we need it in. So I have built my own OSK. some of the feilds though only requier numeric inputs so I though of futher simplifying the process by creating a new form which hosts a numeric pad. so far this is all working. the idea is then to have the app which ask for diffrent inputs then to trigger the OSK application, say that the user wants to enter a phonenumber in one textbox I then want to start the OSK app using a parameter that trigers the OSK to start the NumericForm form first... this too I have working but the thing I can't get right is to hide the AlphabetForm I have tried the following method but am a little stumpt on how to get this right
In short its the Me.hide which isnt working as expected?
Private Sub AlphabetForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
#Region "Recive startup parameters (if any)"
Try
Dim OSKParameters As String = Command()
If OSKParameters = "OSKNUM" Then
NumericForm.Show()
Me.Hide()
Else
ShiftSelect = 0
End If
Catch ex As Exception
'Do nothing
End Try
#End Region
End Sub
Three possible setups, as described in comments:
► Using the Application Framework, override OnStartup and set Application.MainForm to a Form object determined by a command-line argument:
To generate ApplicationEvents.vb, where Partial Friend Class MyApplication is found, open the Project properties, Application pane, click the View Application Events Button: it will add the ApplicationEvents.vb file to the Project if it's not already there.
Imports Microsoft.VisualBasic.ApplicationServices
Partial Friend Class MyApplication
'[...]
Protected Overrides Function OnStartup(e As StartupEventArgs) As Boolean
Application.MainForm = If(e.CommandLine.Any(
Function(cmd) cmd.Equals("OSKNUM")), CType(NumericForm, Form), AlphabetForm)
Return MyBase.OnStartup(e)
End Function
'[...]
End Class
► Disabling Application Framework, to start the Application from Sub Main().
In the Project->Properties->Application pane, deselect Enable application framework and select Sub Main() from the Startup form dropdown.
If Sub Main() doesn't exist yet, it can be added to a Module file. Here, the Module is named Program.vb.
Module Program
Public Sub Main(args As String())
Application.EnableVisualStyles()
Application.SetCompatibleTextRenderingDefault(True)
Dim startForm as Form = If(args.Any(
Function(arg) arg.Equals("OSKNUM")), CType(New NumericForm(), Form), New AlphabetForm())
Application.Run(startForm)
End Sub
End Module
► If the OSK can be moved to UserControls, similar to what jmcilhinney suggested, run the default container Form and select the UserControl to show using the same logic (inspecting the command-line arguments):
Public Class AlphabetForm
Public Sub New()
InitializeComponent()
Dim args = My.Application.CommandLineArgs
Dim uc = If(args.Any(
Function(arg) arg.Equals("OSKNUM")), CType(New NumericUC(), UserControl), New AlphabetUC())
Me.Controls.Add(uc)
uc.BringToFront()
uc.Dock = DockStyle.Fill
End Sub
End Class

Invoke Click Cefsharp VB

I would like to simulate a click or keypresses to a web browser element that is on my visual studio vb project.
I have found ways to do it for the webbrowser object built-in to visual studio, but I am using the cefsharp browser, so
weBrowser.Document.GetElementById('id').InvokeMember("Click") would not work, because cefsharp doesn't allow .Document. So my question, to reiterate, is, how would I use vb to simulate a click on my cefsharp webbrowser? Any help is appreciated, and have a nice day.
EDIT: I've been working on this code:
Dim elementID As String = "myBtn"
Dim click As String = "Click"
browser.ExecuteScriptAsync("Document.All(elementID).InvokeMember(click)")
but I am not sure if it will work or how to use the elementID part (I am not sure what kind of web elements can go here). Maybe this extra information will help.
Using ExecuteScriptAsync will execute JavaScript against the Chrome engine, so you would have to send valid JavaScript to this function. The following code shows how you could start a search using DuckDuckGo
Imports System.Threading
Imports CefSharp
Imports CefSharp.WinForms
Public Class Form1
Private _browser As New ChromiumWebBrowser()
Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
_browser.Top = 10
_browser.Left = 10
_browser.Width = 600
_browser.Height = 400
Controls.Add(_browser)
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
_browser.Load("https://duckduckgo.com/")
'for simplicity just wait until page is downloaded, should be handled by LoadingStateChanged
Thread.Sleep(3000)
Dim jsScript As String = <js><![CDATA[
document.all("q").value = "stack overflow";
document.all("search_button_homepage").click();
]]></js>.Value
_browser.ExecuteScriptAsync(jsScript)
End Sub
End Class

Is it possible to return a string from a .net program using vba

I have this vba code
Dim strpath As String
Dim this As String
strpath = "C:\Users\johbra\Documents\Visual Studio 2010\Projects\WindowsApplication6\WindowsApplication6\bin\Debug\WindowsApplication6.exe"
this = Shell(strpath)
MsgBox this
I also have this function in a .net program
Public Class Form1
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
dothis()
Application.Exit()
End Sub
Function dothis()
Return "HI"
End Function
End Class
Im trying to get the vba code to give me a messagebox that says "HI".. Is this possible?
The most accepted and reliable way to call .NET code from VBA is to expose the .NET class as a COM class (no extra code needed, just tick the appropriate box in project properties), then add a reference the COM class in your VBA project, the way you would for any other other COM class, and call the function.
Here's a guide on how exactly this is done.
Please let me know if you need more details on how to do this.
I think you need to try this with command line parameters.
Like:
Dim CommandLineArgs As System.Collections.ObjectModel.ReadOnlyCollection(Of String) = My.Application.CommandLineArgs
For i As Integer = 0 To CommandLineArgs.Count - 1
MessageBox.Show(CommandLineArgs(i))
Next
To pass parameters back to the VBA script;
Console.WriteLine("Message")
Can catch the console outcome in your VBA code...