Why do equal strings sometimes behave differently? (VB.NET COM interface) - vb.net

Example:
Dim Sh32 As Object = CreateObject("Shell.Application")
Dim path As String = "C:\temp\catalog.zip"
Dim sf As Object = Sh32.NameSpace(path)
-> does not work, sf = Nothing
Dim Sh32 As Object = CreateObject("Shell.Application")
Dim path As String = "C:\temp\catalog.zip"
Dim sf As Object = Sh32.NameSpace(path.ToString)
-> works
Any idea?
Clearly path = path.ToString, but they behave differently when used as COM parameters.

Hmm, I don't know the details of VB and/or COM well enough, but maybe there is a difference between a string object and a string representation?
Should be interesting to see someone "in the know" resolving the puzzle ;)

Related

copy file to directory in visual basic vb.net

I am tringing to copy files settings.copy from sourceDir to backupDir but getting error
Dim sourceDir As String = "c:\in\settings.copy"
Dim backupDir As String = "c:\out\"
File.Copy(sourceDir, backupDir)
while executing above script getting below error
System.IO.DirectoryNotFoundException: 'Could not find a part of the path 'c:\out\'.'
I already created c:\out\ folder
Have you read the documentation for File.Copy, or even just paid attention to Intellisense? Both arguments must be file paths. Neither can be folder paths.
On a related note, why do you have a variable named 'sourceDir' when it's clearly a file path and not a directory path? If you name things clearly - and particularly not misleadingly - then it's more likely that you'll avoid such mistakes. Of course, using the Help menu or F1 key to confirm that you're using a type of method correctly would help too.
Dim userprofile As String = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)
Dim SystemDir As String = Environment.GetEnvironmentVariable("SystemDrive")
Dim sourceDir As String = "y\inbound\settings.exe"
Dim backupDir As String = "AppData\Local\user\default_user\"
Dim root As String = Path.GetPathRoot(userprofile)
Dim useDrpath As String = Path.Combine(userprofile, backupDir)
Dim SysDrpath As String = Path.Combine(SystemDir, root, sourceDir)
Dim file = New FileInfo("settings.cps")
file.CopyTo(Path.Combine(SysDrpath, useDrpath, file.Name), True)
My gole is to copy file from system installed driver to user profile driver
with above code i am able to copy file
c:\y\inbound\settings.exe C:\Users\pavan\AppData\Local\user\default_user\
please suggested any other better way to do above

VBA Groupwise Email

I'm using Excel 2013, Windows 7, Groupwise 12.
The code should be pretty straight forward, but I keep getting an error
"Active X component can't create object"
I've made sure the references are there according to the Groupwise's development documents which is simply their "gwcma1.dll" and I just can't figure out what is going on.
Theory right now is that Groupwise was installed some way that didn't populate the registry information needed for the code to create the object even though it is in the object browser.
Code:
Dim gwAccount As GroupwareTypeLibrary.Account2
Set gwApp = CreateObject("NovellGroupwareSession")
I've used this code for years on older versions of excel and windows and groupwise, and I can't figure out what has changed.
---EDIT---
Here's the beginning part of the code. After this is just standard loops and logic that all works.
Here's the first part of it. After this point, it's just standard loops and and logic.
Sub EmailPDFs()
Dim i, k As Long
Dim PhysicianCount As Long
Dim StartPoint As Long
Dim CurrentNPI As Long
Dim CurrentEmail As String
Dim CurrentFileName As String
Dim CurrentFilePath As String
Dim CurrentDate As String
Dim EmailBody As String
'Declaration for Email
Dim gwMessage As GroupwareTypeLibrary.Message2
Dim gwAccount As GroupwareTypeLibrary.Account2
Dim gwApp As GroupwareTypeLibrary.Application
Set gwApp = CreateObject("NovellGroupwareSession")
Set gwAccount = gwApp.Login()
---EDIT 2---
I tried Application2 and still the same error. I think I was trying the older one and the properties were the same on the object, but Application2 let you do proxy email access which I wasn't worried about.
For creating a new application, I may need a refresh on the syntax but I'm assuming you're suggesting something like:
Dim xlApp as Excel.Application
Set xlApp = New Excel.Application 'Early Binding
Set xlApp = CreateObject(“Excel.Application”) 'Late Binding

VB.net use string in Directory Path?

I apologise for my lack of basic VB.net knowledge but I'm looking to use the equivalent of %systemdrive% to find the drive containing Windows to check for an existing directory in VB.net - so I have the below.
Dim systemPath As String = Mid(Environment.GetFolderPath(Environment.SpecialFolder.System), 1, 3)
If Not Directory.Exists("'systemPath'\MyFolder") Then
Directory.CreateDirectory("'systemPath'\MyFolder")
End If
Can someone help with using the systemPath string in the directory query? Thank you.
Well you should write
Dim systemPath As String = Mid(Environment.GetFolderPath(Environment.SpecialFolder.System), 1, 3)
If Not Directory.Exists(Path.Combine(systemPath, "MyFolder")) Then
Directory.CreateDirectory(Path.Combine(systemPath, "MyFolder"))
End If
You could get the environment variable called %SYSTEMDRIVE% with Environment.GetEnvironmentVariable, but then the results obtained should be manually combined with current directory separator char ("\") because I have not found any way to convince Path.Combine to build a valid path with only the system drive (I.E. C: )
Dim sysDrive = Environment.GetEnvironmentVariable("SystemDrive")
Dim myPath = sysDrive & Path.DirectorySeparatorChar & "MyFolder"
IO.Path has methods that should be used IMHO
Dim sysDrive As String = Environment.GetEnvironmentVariable("SystemDrive") & IO.Path.DirectorySeparatorChar
Dim myPath As String = IO.Path.Combine(sysDrive, "FolderName")

Compiling and running code in runtime

I am trying to compile and run code at runtime. I am using the below code to achieve this. However, when i trying to invoke the method, simply a "Find Source" file browser dialog opens and the code is not run. Can anyone please help me here.
Dim VBP As New VBCodeProvider
Dim CVB As System.CodeDom.Compiler.ICodeCompiler
CVB = VBP.CreateCompiler
Dim PM As New System.CodeDom.Compiler.CompilerParameters
PM.GenerateInMemory = True
PM.GenerateExecutable = True
PM.OutputAssembly = "RunCode.dll"
PM.MainClass = "MainClass"
PM.IncludeDebugInformation = True
Dim ASM As System.Reflection.Assembly
For Each ASM In AppDomain.CurrentDomain.GetAssemblies
PM.ReferencedAssemblies.Add(ASM.Location)
Next
Dim CompileResults As System.CodeDom.Compiler.CompilerResults
CompileResults = CVB.CompileAssemblyFromSource(PM, sCode)
Dim CompileErrors As System.CodeDom.Compiler.CompilerError
For Each CompileErrors In CompileResults.Errors
RTMainScript.AppendText(vbCrLf & CompileErrors.ErrorNumber & ": " & CompileErrors.ErrorText & ", " & CompileErrors.Line)
Next
Dim objRun As New Object
Dim vArgs() As Object
objRun = CompileResults.CompiledAssembly.CreateInstance("RunCode.MainClass", False, BindingFlags.CreateInstance, Nothing, vArgs, Nothing, Nothing)
If Not objRun Is Nothing Then
Dim oMethodInfo As MethodInfo = objRun.GetType().GetMethod("Main")
Dim oRetObj As Object = oMethodInfo.Invoke(objRun, BindingFlags.Static Or BindingFlags.Instance Or BindingFlags.Public Or BindingFlags.NonPublic, Nothing, Nothing, Nothing) 'Find source dialog appears here
Else
MsgBox("Compile Error")
End If
The code you provided is incomplete. You are using this method to compile the code:
CompileResults = CVB.CompileAssemblyFromSource(PM, sCode)
But you actually never specified what sCode is. If you are getting an open file browser dialog, then I am quite sure that your sCode is the cause of it. It must have been set somewhere while calculating the variable value to open a file.
If you are trying to change a piece of code that was used to compile from a file then changing the method from CompileAssemblyFromFile() to CompileAssemblyFromSource() is not enough. You need to dig more into the code and change all related methods.
Ensure your threading model is STA.
OpenFileDialog and similar objects will not operate correctly if the threading model is set to MTA.
If you must use MTA for some other reason then you can create your own custom OpenFileDialog class; sort of sucks.

Variable not declared before it is used with DAO

I'm using DAO (been asked not to use ADO.NET) to update an Access database. I'm currently thus far, however, VB2008 is telling me that the variable "daoengine" is not declared before it is used. What am I doing wrong in the following code?
Function update_db()
Dim daoengine As DAO.DBEngine
Dim dbs As DAO.Database
Dim rst As DAO.Recordset
dbs = daoengine.OpenDatabase("Project.mdb")
rst = dbs.OpenRecordset("Log", dao.RecordsetTypeEnum.dbOpenDynaset)
End Function
You should have
Dim daoengine As New DAO.DBEngine
instead of
Dim daoengine As DAO.DBEngine
When you say
Dim daoengine As DAO.DBEngine
you're just creating a variable, daoengine, but it doesn't actually point to anything. You need to do this afterwards:
Set daoengine = New DAO.DBEngine
You can also type Dim daoengine As New DAO.DBEngine, but it's better to do it in the two lines above. If you put the New in the Dim line, you create what's called an "auto-instancing" variable which can magically come to life again after you thought you'd disposed of it.
For more details see the Don't Use Auto-Instancing Object Variables here: Declaring Variables (in VBA)