I have got a sample add in for excel:
I create a object InDesign.Application
Dim myInDesign As InDesign.Application
Dim myDoc As InDesign.Document
Dim myPage As InDesign.Page
myInDesign = CType(Activator.CreateInstance(Type.GetTypeFromProgID("InDesign.Application"), True), InDesign.Application)
myDoc = myInDesign.Documents.Add
myDoc = myInDesign.ActiveDocument
InDesign opens, But the add-in shows error at:
`myInDesign = CType(Activator.CreateInstance(Type.GetTypeFromProgID("InDesign.Application"), True), InDesign.Application)`
Content of Error:
Unable to cast COM object of type 'System.__ComObject' to interface type 'InDesign.Application'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{ABD4CBB2-0CFE-11D1-801D-0060B03C02E4}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).
Why? Can you help me?
Have you added the reference to the InDesign Type Library using vb.net's com interface?
Open the references panel in Visual Studio and choose the "COM" tab, and look for InDesign in your list. It will default to Copy Local = False.
Now you can use the COM functions just like you were writing vbs.
Related
I am making com object in php. But when I write die($object), it shows empty object. I have also registered my dll file. How to resolve this issue?
$obj = new com("dllfilename.classname") or die("Unable to include the dll");
die(json_encode($obj));
Hi i got form that reads eID card, smart cards
text data reads correctly
picturebox name is picLK
but last statement is a picture
in VB6 and VBA I used
me.pctLK.Picture = ReaderEngine.portrait
ReaderEngine is procedure that reads data from card
when I use command in vb.net I get an error
me.pctLK = ReaderEngine.portrait
reader is reading card, but I got this message
An unhandled exception of type 'System.InvalidCastException' occurred in Project1.exe
Additional information: Unable to cast COM object of type 'stdole.StdPictureClass' to class type 'System.Windows.Forms.PictureBox'. Instances of types that represent COM components cannot be cast to types that do not represent COM components; however they can be cast to interfaces as long as the underlying COM component supports QueryInterface calls for the IID of the interface.
I am new to VB .net
Is there any suggestions?
VB.NET has a helper function in the Microsoft.VisualBasic.Compatibility.VB6.Support module, named IPictureDispToImage, which accepts an StdPictureClass object (which implements IPictureDisp) and returns a .NET System.Drawing.Image object which you can assign to the PictureBox.Image property. Be sure you appropriately dispose of the Image when you're finished with it:
Dim comImage As StdOle.StdPictureClass = ReaderEngine.portrait
If Me.picLK.Image Is Not Nothing Then
Me.picLK.Image.Dispose()
Me.picLK.Image = Nothing
End If
Dim newImage As System.Drawing.Image = Support.IPictureDispToImage( comImage )
Me.picLK.Image = newImage
I am trying to make a WYSIWYG HTML-editor by embedding GeckoFX in a Windows Forms application in VB.NET.
The code goes like this:
Dim Gbrowser As New GeckoWebBrowser
Gbrowser.Navigate("about:blank")
...
Gbrowser.Navigate("javascript:void(document.body.contentEditable='true')")
How can I activate and access the nsIHTMLEditor interface from within my application?
Thank you.
UPDATE
This code does not work:
Dim hEditor As nsIHTMLEditor
hEditor = Xpcom.GetService(Of nsIHTMLEditor)("#mozilla.org/editor/htmleditor;1")
hEditor = Xpcom.QueryInterface(Of nsIHTMLEditor)(hEditor)
hEditor.DecreaseFontSize()
Error in the last line: HRESULT E_FAIL has been returned from a call to a COM component.
nsIHTMLEditor is likely a per browser instance rather than a global instance (like things returned by Xpcom.GetService)
One can get a nsIEditor like this by (by supplying a Window instance)
var editingSession = Xpcom.CreateInstance<nsIEditingSession>("#mozilla.org/editor/editingsession;1");
nsIEditor editor = editingSession.GetEditorForWindow((nsIDOMWindow)Window.DomWindow);
Marshal.ReleaseComObject(editingSession);
(or you can just call the nsIEditor GeckoWebBrowser.Editor property.)
You may be able to cast this nsIEditor to a nsIHtmlEditor (although I have yet to try it)
GeckoWebBrowser browser = .....;
// Untested code
nsIHTMLEditor htmlEditor = (nsIHTMLEditor)browser.Editor;
Update:
The VB code from #GreenBear
Dim gEditor As nsIHTMLEditor:
gEditor = Gbrowser.Editor:
gEditor.DecreaseFontSize()
I am using below code to use autocad object.
Dim acadapp As AcadApplication
acadapp = GetObject(, "AutoCAD.Application")
'''and using below code to create object -------------
acadapp = CreateObject("AutoCAD.Application")
Getting error "Cannot create ActiveX component".
I tried using 18,19 and various combination as below :
acadapp = GetObject(, "AutoCAD.Application.18")
But nothing work.
Please help.
#Locke : Thanks for reply.I tried your soltion as below :
Dim acadType As Type
Try
acadapp =
System.Runtime.InteropServices.Marshal.GetActiveObject("AutoCAD.Application.18.1")
''Above code din't worked so tried below code also
' acadapp = DirectCast(Marshal.GetActiveObject("AutoCAD.Application.18.1"),
'AcadApplication)
Catch ex As Exception
acadType = Type.GetTypeFromProgID("AutoCAD.Application")
acadapp = DirectCast(Activator.CreateInstance(acadType, True), AcadApplication)
End Try
Showing Exception :
Unable to cast COM object of type 'System.__ComObject' to interface type 'AutoCAD.AcadApplication'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{8E75D910-3D21-11D2-85C4-080009A0C626}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).
Here's what I typically use when dealing with AutoCAD interop. It checks for a running instance, and creates a new one if necessary:
private static AcadApplication GetAcadApp(string progId)
{
// Create the return application
AcadApplication returnApp = null;
try
{
// Try getting a running instance
returnApp = (AcadApplication)Marshal.GetActiveObject(progId);
}
catch (COMException)
{
try
{
// Try creating a new instance
Type acadType = Type.GetTypeFromProgID(progId);
returnApp = (AcadApplication)Activator.CreateInstance(acadType, true);
}
catch (COMException)
{
// Report failure
MessageBox.Show(string.Format("Cannot create object of type \"{0}\"", progId));
}
}
// Return the application
return returnApp;
}
An AcadApplication COM object can be set like this:
// Get/create an AutoCAD instance
var acadApp = getAcadApp("AutoCAD.Application.18");
Regardless of C# or VB.NET, using Marshal.GetActiveObject and Activator.CreateInstance are probably the better ways to approach this.
According to the exception, the problem is not the GetActiveObject() call, but that the returned object doesn't support the interface you're looking for. Most likely reason is that your code references a different version of AcadApplication than the one you're getting back from GetActiveObject(). Change your project to reference the COM library version for the returned AutoCAD instance, and it should work fine.
I have created a COM add-in for Excel 2003 using Visual Studio 2005 Tools for Office. The add-in code looks like this:
[Guid("EAC0992E-AC39-4126-B851-A57BA3FA80B8")]
[ComVisible(true)]
[ProgId("NLog4VBA.Logger")]
[ClassInterface(ClassInterfaceType.AutoDual)]
public class Logger
{
public double Debug(string context, string message)
{
Trace.WriteLine(message);
return message.Length;
}
[ComRegisterFunctionAttribute]
public static void RegisterFunction(Type type)
{
Registry.ClassesRoot.CreateSubKey(GetSubKeyName(type, "Programmable"));
RegistryKey key = Registry.ClassesRoot.OpenSubKey(GetSubKeyName(type, "InprocServer32"), true);
key.SetValue("", System.Environment.SystemDirectory + #"\mscoree.dll", RegistryValueKind.String);
}
[ComUnregisterFunctionAttribute]
public static void UnregisterFunction(Type type)
{
Registry.ClassesRoot.DeleteSubKey(GetSubKeyName(type, "Programmable"), false);
}
private static string GetSubKeyName(Type type, string subKeyName)
{
System.Text.StringBuilder s = new System.Text.StringBuilder();
s.Append(#"CLSID\{");
s.Append(type.GUID.ToString().ToUpper());
s.Append(#"}\");
s.Append(subKeyName);
return s.ToString();
}
}
I've set the project to register for COM interop, and I've registered the DLL with:
regasm.exe /tlb NLog4VBA.dll
When I open Excel, I go to Tools -> Add-Ins, click Automation, and add NLog4VBA.Logger. I can then go to Insert -> Function, pick NLogVBA.Logger from the list of categories, and choose Debug.
The end result is a cell with contents like:
=Debug("My Context","My Message")
... and a displayed value of:
10
This is all as it should be. In my VBA code, I can go to Tools -> References and add NLog4VBA. I then add the following code to a button on my sheet:
Private Sub CommandButton1_Click()
Application.COMAddIns("NLog4VBA.Logger").Object.Debug "My Context", "My Message"
End Sub
This fails, because COMAddIns("NLog4VBA.Logger") fails with:
Run-time error '9': Subscript out of range
Could someone please tell me what I need to do to make the Debug() method accessible to my VBA code (which is more useful to me than being able to call the method from within a cell)?
I'm sure I'm missing something simple here.
Edited 2010/09/07: I've updated the code snippet to include the [ProgId] attribute as suggested below by Jim; the problem persists. I can see the object in registry:
[HKEY_CLASSES_ROOT\CLSID\{EAC0992E-AC39-4126-B851-A57BA3FA80B8}]
#="NLog4VBA.Logger"
[HKEY_CLASSES_ROOT\CLSID\{EAC0992E-AC39-4126-B851-A57BA3FA80B8}\Implemented Categories]
[HKEY_CLASSES_ROOT\CLSID\{EAC0992E-AC39-4126-B851-A57BA3FA80B8}\Implemented Categories\{62C8FE65-4EBB-45e7-B440-6E39B2CDBF29}]
[HKEY_CLASSES_ROOT\CLSID\{EAC0992E-AC39-4126-B851-A57BA3FA80B8}\InprocServer32]
#="C:\\WINDOWS\\system32\\mscoree.dll"
"ThreadingModel"="Both"
"Class"="NLog4VBA.Logger"
"Assembly"="NLog4VBA, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
"RuntimeVersion"="v2.0.50727"
"CodeBase"="file:///C:/projects/nlog4vba/NLog4VBA/bin/Debug/NLog4VBA.dll"
[HKEY_CLASSES_ROOT\CLSID\{EAC0992E-AC39-4126-B851-A57BA3FA80B8}\InprocServer32\1.0.0.0]
"Class"="NLog4VBA.Logger"
"Assembly"="NLog4VBA, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
"RuntimeVersion"="v2.0.50727"
"CodeBase"="file:///C:/projects/nlog4vba/NLog4VBA/bin/Debug/NLog4VBA.dll"
[HKEY_CLASSES_ROOT\CLSID\{EAC0992E-AC39-4126-B851-A57BA3FA80B8}\ProgId]
#="NLog4VBA.Logger"
[HKEY_CLASSES_ROOT\CLSID\{EAC0992E-AC39-4126-B851-A57BA3FA80B8}\Programmable]
Also, the ProgID is visible in the Add-Ins dialog:
I still have no idea why this isn't working :-(
The COMAddIns collection is either indexed via a numerical index, or via a string that is the ProgId of the desired component. Make sure that your ProgId is actually "NLog4VBA.Logger" (via the ProgId attribute in .NET) and verify that the object is registered with this id (which you can easily check in the registry, searching for your assigned GUID).
It turns out that my VBA code was quite wrong; here is the answer courtesy Jan Karel Pieterse:
I think you would need to do something
like this:
Private Sub CommandButton1_Click()
'Declare an object variable using the referenced lib.
'if all is well, intellisense will tell you what the proper object name is:
Dim objLogger as NLog4VBA
'Create an instance of the object
Set objLogger = New NLog4VBA
'Now use the object
objLogger.Object.Debug "My Context", "My Message"
End Sub