Missing object reference in Outlook add-in (VB) - vb.net

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

Related

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

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.

Reading Applications and services log

I want to read a custom event log which is stored under Applications and services log section in Windows Eventlog.
Unfortunately when calling the Log according to its naming properties I receive an error message that the log cannot be found.
Ulitmately I try read event details from events with a specific ID but first I need to able to access the log.
This is the code that I have so far:
Imports System
Imports System.Diagnostics.Eventing.Reader
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim query As New EventLog("Logname as per Properties", System.Environment.MachineName)
Dim elEventEntry As System.Diagnostics.EventLogEntry
Dim nmbr As Integer = query.Entries.Count
MsgBox(nmbr)
End Sub
End Class
This is the structure in the eventlog (I want to read the blue highlighted part)
Anybody any idea how to determine the correct log name?
Thx & BR
Daniel
For many of the event logs, you need to use an EventLogQuery.
As an example, if you wanted to query the "Setup" event log to count the number of entries with an EventID of 1, you could do this:
Imports System.Diagnostics.Eventing.Reader
Module Module1
Sub Main()
Dim query As New EventLogQuery("Setup", PathType.LogName, "*[System/EventID=1]")
Dim nEvents = 0
Using logReader = New EventLogReader(query)
Dim eventInstance As EventRecord = logReader.ReadEvent()
While Not eventInstance Is Nothing
nEvents += 1
eventInstance = logReader.ReadEvent()
End While
End Using
Console.WriteLine(nEvents)
Console.ReadLine()
End Sub
End Module
You can see the names of the items to query by looking at the XML for an event in Windows Event Viewer.
The Using construct makes sure that the EventLogReader is properly disposed of after it's been used.
Further information: How to: Access and Read Event Information (from Microsoft).

Filling RichTextBox works from click-Event only

I have a RichTextBox in a Form which I try to fill from a Module; I tried already a few different ways to get it to work, but I can't figure out what the actual problem is?
In my module I have an instance::
Dim Protokoll_UI As New Form1
With this I fill the RichTextBox directly.
Protokoll_UI.RichTextBox.Text = File.ReadAllText(filename)
I already tried to call a method in class Form1 from the module, too, but it didn't have any impact on it:
Module Module1
Public Sub get_protokoll()
Protokoll_UI.Protokoll()
End sub
End Module
Class Form1
Public Sub Protokoll()
Protokoll_UI.Text = File.ReadAllText(Dateiname)
End Sub
End Class
The funny thing is that I have a ToolStripMenuItem.Click Event in the Class as well in which I update the RichTextBox, and it doesn't matter how I fill the RichTextBox, I can call a sub or fill it directly, it works perfectly:
Private Sub UpdateToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles AktualisierenToolStripMenuItem.Click
Protokoll()
End Sub
Even changing the WordWrap property of the RichTextBox did not help. At the moment I have absolutely no idea where I could look to solve this problem.
Btw. this is the result when I search for the RichTextBox in the whole project.
In your module, just do:
Form1.RichTextBox.Text = File.ReadAllText(filename)
and remove the Dim Protokoll_UI As New Form1 from it.
You can call the Protokoll() function in the module from Form1 because it's in a Module; modules are global, and usable by all forms/classes in the project.

Copying code from Private Sub to Shared Sub

I'm trying to have a bunch of code run on the hour every hour in my VB Application. The code works in it's own Sub, but when I add it to this "TopOfTheHour" Shared Sub I get the error "cannot refer to an instance member of a class from within a shared method or shared member initializer without an explicit instance of the class"
I left in the loadlbl.Visible as an example of what doesn't work which is just a label control on my main Form (Form1). Writing the refresh time to the console works, but loadlbl.Visible = True does not.
Private Shared Sub OnTimedEvent(source As Object, e As System.Timers.ElapsedEventArgs)
Dim aTimer As System.Timers.Timer = CType(source, System.Timers.Timer)
aTimer.Stop()
Console.WriteLine("Server Status Refreshed at " & DateTime.Now)
loadlbl.Visible = True
'Far more code is here, much of it with the same error.
aTimer.Interval = MillisecondsToNextTopOfTheHour()
aTimer.Start()
End Sub
All my code for Form 1 (including what I want to add to this Shared Sub) is here on PasteBin. (VB Syntax Highlighting is on so code is easier to read)
Thank You!
Do you really need this method to be 'shared'? Remove the shared and try again.
Shared means that is method is not an instance method, it has no access to any local variables.

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...