MsoAutomationSecurity is ambiguous in the namespace of Microsoft.Office.Core - vb.net

I am trying to use Microsoft.Office.Core.MsoAutomationSecurity in vb.net to set the security mode of an application when opening an excel file. But when I declare this variable on my code in vb.net:
Dim secAutomation As New Microsoft.Office.Core.MsoAutomationSecurity
it has a blue lining below Microsoft.Office.Core.MsoAutomationSecurity and on the Error window, this is listed:
'MsoAutomationSecurity' is ambiguous in the namespace 'Microsoft.Office.Core'
Type 'Microsoft.Office.Core.MsoAutomationSecurity' is not defined.
I hope anyone could help me fix this.
EDITED:
Imports OfficeCore = Microsoft.Office.Core
Imports excelApp = Microsoft.Office.Interop.Excel
Sub ExcelSecurity()
Dim secAutomation As OfficeCore.MsoAutomationSecurity
secAutomation = Microsoft.Office.Core.MsoAutomationSecurity.msoAutomationSecurityLow
End Sub
thank you!

The main problem with this question is that on one (1) solution there are 4 projects. The reference on the other projects has a duplicate Reference when adding the said Reference which is the Microsoft Office 14.0 Object Library. When I see the References on the other projects, the Microsoft Office 14.0 Object Library has a duplicate with different versions, to fix this problem I just removed the outdated version of the Reference.

This error throws due to not add reference for MS Office which installed on your machine. Under project solution > References > Add references > COM > Microsoft office 16.06 Object library marked as checked and saved.
After showing your code minor modification needed as below:
Imports OfficeCore = Microsoft.Office.Core
Imports excelApp = Microsoft.Office.Interop.Excel
Sub ExcelSecurity()
Dim secAutomation As OfficeCore.MsoAutomationSecurity
secAutomation = Microsoft.Office.Core.MsoAutomationSecurity.msoAutomationSecurityLow
End Sub
I hope it will resolve your issue without any other changes. Let me know if any further issues.

Related

LinqPad 7 - C# - Microsoft.VisualBasic.Interaction.GetObject

Solved by Comment from Joe Albahari
In LinQPad 7 I try to connect to an open Excel Application.
In C# .net this is easy done by eg:
public Microsoft.Office.Interop.Excel.Application ExcelApplication; ExcelApplication = (Application)Microsoft.VisualBasic.Interaction.GetObject(Class: "Excel.Application");
With references to Microsoft.Office.Interop.Excel and Microsoft.VisualBasic
In LinQPad 7 I tried the same but ExcelApplication is null (Yes Excel is open!)
References are set and Namespace imported.
It shows no error, just does not connect.
What am I doing wrong?
(The example from LinqPad for Excel creates a new excel application.
thanks for any help

Why doesn't code work in VB.net, but works in VBA; GetObject

VBA code works great:
Sub testVBA()
Dim wb As Object ' Lotus123.Document
Set wb = GetObject("S:\Temp\T\0375D.WK3", "Lotus123.Workbook")
End Sub
VB.net code fails:
Sub TestVBNet()
Dim wb As Object ' Lotus123.Document
wb = GetObject("S:\Temp\T\0375D.WK3", "Lotus123.Workbook")
End Sub
In VB.net I get a FileNotFoundException: "File name or class name not found during Automation operation."
As I can run it from VBA that means the file exists and that the class name exists. So why doesn't it work and how can I fix it in VB.net.
EDIT: I guess I'm not sure how to start diagnosing this: Obviously the class exists on my computer but somehow VB.net doesn't manage to find it. Maybe VB.net uses a different method to activate the class. Maybe a registry entry is missing. I am glad for any suggestions.
Edit 2: I also tried using CreateObject and got this error: "Cannot create ActiveX component." Not unexpected.
For some reason VB.net cannot find the class name "Lotus123.Workbook" so I tried getting the file without the class name and it works fine in XP.
Dim wb As Object ' Lotus123.Document
wb = GetObject("S:\Temp\T\0375D.WK3")
EDIT: In Win8 64bit the above doesn't work; just hangs.
The code below works in XP 32 bit as well as in Win8 64 bit. I checked with process monitor what is happening under the hood. CreateObject checks for the CLSID in the registry using the given object. Then it looks up the necessary info using the CLSID.
Public Shared Function GetLotusWB(ByVal sFile As String) As Object
'HKCU takes precedence if exists
'HKCU\Software\Classes\Lotus123.Workbook\CLSID
'HKCU\Software\Classes\CLSID\{29130007-2EED-1069-BF5D-00DD011186B7}
'normally this is used because Lotus123 doesn't create HKCU entries
'HKCR\Lotus123.Workbook\CLSID = {29130007-2EED-1069-BF5D-00DD011186B7}
'HKCR\CLSID\{29130007-2EED-1069-BF5D-00DD011186B7}\InprocHandler32 = ole32.dll
'HKCR\CLSID\{29130007-2EED-1069-BF5D-00DD011186B7}\LocalServer32 = C:\Lotus\123\123w.exe
'using object as that sometimes works better
Dim LotusObj As Object = CreateObject("Lotus123.Workbook")
'get application
'need a reference to Lotus 123 else declare as Object
Dim LotusApp As Lotus123.Application = LotusObj.Application
'FAILS: LotusApp.Visible = True
'open file; also works fine As Lotus123.Document
Dim ldoc As Object = LotusApp.OpenDocument(sFile)
'visible and activate (must declare as Object else gives exception)
Dim appObject As Object = ldoc.Application
appObject.Visible = True
ldoc.Activate()
Return ldoc
End Function
This works great because it creates the "Lotus123.Workbook" which is used to get the application object.
Load the file into an Excel workbook. It should be able to convert the lotus123 workbook on the fly.
First of all, check to make sure your inclusions (I think under Tools menu, includes or references or something like that) include the library that references Lotus123.Document. Chances are it's in the "Microsoft Excel 14.0 Object Library" or similar.
I've heard it said that VB is not VBA!

Cannot create new Excel.Application

I have this class in which I want to create a new Excel application to export some data to an Excel workbook, but I can't seem to create the application itself.
I already added the Microsoft.Office.Interop.Excel.dll reference to the Bin folder:
And tried to add the code like this:
Imports Microsoft.VisualBasic
Imports Excel = Microsoft.Office.Interop.Excel
Public Class ExcelExport
Public Sub Export()
Dim excel As New Excel.Application
End Sub
End Class
But when I do I can't find the Application:
Any ideas why it wont work?
VS 2008, VB.NET, .NET 3.5, Office 2007, Windows 7 Ultimate x64

Visual Basic 2010 Express - How to use excel function in Visual Basic 2010 Express

I want to use excel function in Visual Basic 2010 Express. I tried to search in the internet to see how to do that.
I come across with this solution:
Module Module1
Sub Main()
Imports Excel = Microsoft.Office.Interop.Excel
Dim oXLApp As New Excel.Application
Dim ExcelMath As Excel.WorksheetFunction
ExcelMath = New Excel.WorksheetFunction
Dim I As Double
Dim s As Double
I = ExcelMath.Average(1, 2, 3, 4, 5)
s = ExcelMath.StDev(1, 2, 3, 4, 5)
ExcelMath = Nothing
oXLApp.Quit()
oXLApp = Nothing
End Sub
End Module
However, there is error of:
1. Syntax error.
2. Type 'Excel.Applciation" is not defined.
3. Type 'Excel.WorksheetFunction' is not defined.
How should it be resolved?
Or is there other simple way to do that?
Thanks a lot!
Not sure if it has to do with using the Express edition or not, but I had the same problem.
Whenever I attempted to use Imports Microsoft.Office.Interop.Excel it would not work.
I went to Project > Add References > COM > Microsoft Office14.0 Object Library and it works now.
My guess is that you need to add a reference to the Microsoft Excel Object Library. For a brief tutorial, see http://support.microsoft.com/kb/301982 (more specifically, step #3).
If this doesn't resolve the problem, let us know.
Include an import statement at the top : Imports Microsoft.Office.Interop.Excel

How can I edit a registry key with VB.NET or VB6?

I need to edit a registry key and set the data value to "4"
I know how to do it through the command prompt but am trying to find some Visual Basic code to do it.
If it helps, this is the key:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\USBSTOR\Start
Here's how'd you do it in Visual Basic .NET
Dim key As RegistryKey = Registry.LocalMachine
Dim subkey As RegistryKey
subkey = key.OpenSubKey("SYSTEM\CurrentControlSet\Services\USBSTOR", True)
subkey.SetValue("Start", 4)
You'll need to make sure to add
Imports System
Imports Microsoft.Win32
at the top of your code.
Here's how you can do it in Visual Basic 6 (or VBA)
Download this registry editing code and put it into a class.
Then you can have code like the following to actually modify the value:
Dim reg As New RegistryClass
With reg
.SetKeyValue .HKEY_LOCAL_MACHINE, "SYSTEM\CurrentControlSet\Services\USBSTOR", .RegDWORD, "Start", 4
End With
You need to use the Registry class in the Microsoft.Win32 namespace. Check the docs, it is pretty easy to use.