I recently had cause to check whether I already had an instance of an MDIChild form open in VB.NET. Using Array.Exists on the MDIChildren property of the MDI Parent form seemed like an elegant way to go. However, after further investigation it looked like i’d need code some supporting functions for the predicate. What I went with in the end was this:
Dim formName As String = "" ' Enter form name here
For Each f As Form In Application.OpenForms
If f.IsMdiChild = True AndAlso f.Name = formName Then
' Yes, we have a MDI Child form open with the required name
End If
Next
Here’s the complete code to open a new form, or activate the existing form (if there is one) :
Dim formName As String = "" Enter form name here
Dim openForm As Form = Nothing
' Iterate and check for instance
For Each f As Form In Application.OpenForms
If f.IsMdiChild = True AndAlso f.Name = formName Then
openForm = f
End If
Next
openForm = CType(IIf(openForm Is Nothing, New OrderForm(New Guid(formName)), openForm), Form)
openForm.MdiParent = Me
openForm.Show()
openForm.Activate()
I kept getting the above error when attempting to generate documents on a thread other than the main thread. Here’s the error message in full:
Unable to cast COM object of type ‘System.__ComObject’ to interface type ‘SAPbobsCOM.ICompany’. This operation failed because the QueryInterface call on the COM component for the interface with IID ‘{3BA8DAED-5B33-4CE4-A4B8-B4308D86E524}’ failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).
[quads id=1]
Turns out that running the following command on the client machine did the trick:
C:\SAP\SAP Business One DI API\DI API 88>regsvr32 SAPbobsCOM88.dll
What was weird here was that previous calls to the COM object on the main thread work fine. Any subsequent calls on a different thread threw the exception.
Incidentally, I have also spotted this error when using some 3rd party (and SAP – Certified) add-ons. In all cases, the above DLL registration command resolved the issue. Your mileage may vary, however.
For more information on the SAP Business One DI API, check out the following link over at the SBO website:
I recently had reason to upload a lot of files to Sharepoint 2010. Unfortunately the Upload item in the menu would only allow me to do this in batches of 100. Fortunately however, you can do this from code.
Public Function UploadToSharepoint(ByVal localFile As String, ByVal remoteFilename As String, spUrl As String, username As String, password As String, domain As String) As Byte()
' Ensure that we've got a valid URL.
If Not spUrl.EndsWith("/") Then spUrl &= "/"
' Build the remote url, escape spaces and replace backslashes.
Dim rfURL As String = String.Format("{0}{1}", spUrl, remoteFilename).Replace(" ", "%20").Replace("\", "/")
' Create a webclient.
Dim wc As New WebClient
wc.Credentials = New System.Net.NetworkCredential(username, password, domain)
' Attempt the upload.
Try
Using s As New System.IO.FileStream(localFile, IO.FileMode.Open, IO.FileAccess.Read)
Using reader As New System.IO.BinaryReader(s)
UploadToSharepoint = wc.UploadData(rfURL, "PUT", reader.ReadBytes(CInt(s.Length)))
End Using
End Using
Catch ex As Exception
UploadToSharepoint = Nothing
End Try
End Function