VFP. Trapping COM errors - com

Trapping Excel COM errors from a VFP program.
A program is writing out an extract from several large tables to Excel using Automation.
Typical instructions include :
lCell = "I1234”
.sSheet1.Range(lCell).value = cCust.Name
This generally works fine, but if cCust.name has a value such as ‘=’ (which Excel does not like), this causes an OLE COM error.
Had tried using TRY .. . CATCH . . . ENDTRY to trap this error, but the OLE COM error still reports.
I suppose that I could find out all the possible errors in the data and clean them up, but is there any way of getting Automation to ignore the error and carry on (leaving the field unpopulated)?
Thank you.

I am adding this as an answer, since it contains some code that would otherwise cause a mess. With ON error it is working perfectly fine for me. ie:
*** Constant Group: XlSaveAction
#Define xlDoNotSaveChanges 2
#Define xlSaveChanges 1
Local loExcel As Excel.Application
Local lcValue
loExcel = Createobject('Excel.Application')
loExcel.Workbooks.Add()
loExcel.ActiveWorkbook.SaveAs('c:\temp\ExcelReadTest.xlsx')
loExcel.DisplayAlerts = .F.
*loExcel.Visible = .T.
On Error lcValue = .Null.
With loExcel.ActiveWorkbook.ActiveSheet
.Range('A5').Value = '=Like me'
lcValue = .Range('A5').Value
? m.lcValue
Endwith
On Error
loExcel.ActiveWorkbook.Close(xlSaveChanges)
loExcel.Quit()

Related

Dymola Results of checkModel()

checkmodel([Some Model]) opens the GUI "Dymola Messages", tab "Translation" and displays Errors, Warnings, and Messages.
Does anyone know how to write these infos to a logfile or get them as kind of return value of checkModel(). All I've found in the documentation was, that checkModel() only returns a success-boolean. Are these infos saved temporarily somewhere?
Note, that I only want to apply checkModel() but not actually translating the code.
I finally found a solution at least for Dymola 2016 and newer, so if someone is interested - here it is (it is not very user-friendly, but it works):
The key-command is getLastError() which not only returns the last error (as one could think...), but all errors that are detected by checkModel() as well as the overall statistics.
All informations are sampled in one string, in which the last lines looks like:
"[...]
Local classes checked, checking <[Some Path]>
ERROR: 2 errors were found
WARNING: 13 warnings were issued
= false
"
Following operations will return the number of actual errors (for warnings it is more or less the same):
b = checkmodel([Some Model])
s = getLastError()
ind1 = Modelica.Utilities.Strings.findLast(s,"ERROR:")
ind2 = Modelica.Utilities.Strings.findLast(s," errors were found")
nErrors = Modelica.Utilities.Strings.substring(s,ind1+6,ind2) //6 = len(ERROR:)
nErrors = Modelica.Utilities.Strings.replace(nErrors," ","")
nErrors
= "2"
Note:
I used findLast as I know, that the lines of interest are at the very end of the string. So this is significantly faster than using find
This only works, if the line "ERROR: ...." actually exists. Otherwise, the substring call will throw an error.
Of course this could be done in less lines, but maybe this version is easier to read.
NOTE: This will only works with Dymola 2016 and newer. The return-string of getLastError is of a different structure in Dymola 2015 and older.
The following should handle it:
clearlog(); // To start fresh
Advanced.TranslationInCommandLog=true;
checkModel(...);
savelog(...);
This is mentioned in the Dymola User Manual Volume 1, section "Parameter studies by running Dymola a number of time in “batch mode”" on pg 630 or so.

Catastrophic failure followed by MAPI_E_INTERFACE_NOT_SUPPORTED when trying to AddPstStore

I am trying to add pst store from outlook addin using Redemption with the code:
RdoSession = Redemption.RedemptionLoader.new_RDOSession();
RdoSession.MAPIOBJECT = (AddinModule.CurrentInstance as AddinModule).OutlookApp.Session.MAPIOBJECT;
string path = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + #"\Microsoft\Outlook\mystore.pst";
RDOStores stores = RdoSession.Stores;
RDOStore store = stores.AddPSTStore(path, Outlook.OlStoreType.olStoreDefault, "test");
Line stores.AddPstStore throws System.Runtime.InteropServices.COMException (0x8000FFFF): Catastrophic failure and it creates mystore.pst in correct location with the size of 265 KBs. New store/data file is not displayed in outlook and it does not apear in RdoSession.Stores on next execution. So my program tries to create it again (since it didint found it) and then same line crashes with:
System.InvalidCastException: Error in IMsgServiceAdmin::AdminProviders: MAPI_E_INTERFACE_NOT_SUPPORTED
Using office PIAs code works without any problems:
https://msdn.microsoft.com/en-us/library/office/bb612380.aspx
I would keep redemption code since there is much more logic around it. I just moved these 5 lines to the begining of function to isolate the problem.
MAPI has a few marshaling bugs in the profile management interfaces (such as IMsgServiceAdmin).
Try to replace setting the RDOSession.MAPIOBJECT property with calling RDOSession.Logon.
Is you code running in a Outlook addin or a standalone app?

DBNull error - 'System.InvalidCastException'

Using Visual Studio 2008 and generating .net 2.0 framework with VB.net for a web application.
I encountered a very strange problem currently. After I build a solution and click a link in a web page, I got an error message as following. The same thing happened when I tried to run in debug mode.
*************************************** ERROR Message ******************************************************
"An exception of type 'System.InvalidCastException' occurred in Microsoft.VisualBasic.dll but was not handled in user code"
"Additional information: Operator '=' is not defined for type 'DBNull' and string "". "
*******************************************************************************************************************
The problem part is simply as below in a vb file. The value of dataview(0)(“name”) is NULL and my_name is a variable string. The strange is thing I didn't change any code in this vb file. This vb file was built previously and used for a long time ago without any problem. It just suddenly happened today. I have this syntax all over the program without causing any problem. I have tried to restore the whole solution back to original but still have the problem.
Can anyone please advise why all the sudden and how to fix?
If dataview(0)(“name”) = my_name then …
Try to determine NULL value first ..
If NOT IsDBNull(dataview(0)(“name”)) Then
If dataview(0)(“name”) = my_name then
'codes here
End If
End If

VBA Error - Reflection.FTP.3

I had this error pop up on exactly the 3rd line of code below. There seem to be no explanation on the Internet for this behaviour.
I'm looking at why this error came up, and fixed itself after few minutes.
Set Ftp = CreateObject("Reflection.FTP.3")
Ftp.Open "xxx.xxx.xxx.xxx", "username", "password"
Ftp.SetCurrentDirectory "DirectoryName/DirectoryName/DirectoryName"
What was the error?
Run-time error '-2147418113 (8000ffff)':
Method 'SetCurrentDirectory' of object 'IReflectionFTP' failed
More details:
Application: Excel Macro
Language : VB (VBA)
*Is this because of a coding error? *
Not likely. The macro has been long running and this came up for the first time.
*Is it because of a FTP service disruption? *
May be. But logs have a recording for every second and there seems to be no outage.
It seems to me there is a connection problem here - maybe a timeout? I assume that your three lines of code don't execute one after another (ie the SetCurrentDirectory is after some more code). This error will come up if the Ftp object doesn't have a valid connection that is logged in. Change the IP for the Open command to an invalid one and you'll see you get the same error.
Try setting the following line of code before SetCurrentDirectory command.
If FTP.Status = rcLoggedIn + rcConnected Then
Ftp.SetCurrentDirectory "DirectoryName/DirectoryName/DirectoryName"
Else
'Error handle
End If
Note, that you are late binding the object so for it to work for you, you'll need the If statement to be:
If FTP.Status = 17 Then
Also, if it is a timeout problem then I'd set the Timeout period for the session to be longer, ie FTP.TimeoutSession = 300.

VB.Net "COMException was unhandled" Error

I have a VB.Net program written with .Net 2.0
I'm trying to write a code that logs entire registry entries into a log file.
I have got it to work wit the following code.
http://pastebin.com/kmA63cUe
However whenever I try to add an additional Registry key to log
LogPrint4("<-----------------HKLM/WebCheck-------------------->")
If reg.enumvalues(&H8000002, "Software\Microsoft\Windows\CurrentVersion\WebCheck", keys2, types) = 0 Then
If IsArray(keys2) Then
For p = 0 To UBound(keys2)
reg.getstringvalue(&H8000002, "Software\Microsoft\Windows\CurrentVersion\WebCheck", keys2(p), value)
LogPrint4(keys2(p) & "=" & value)
Next
End If
End If
It comes up with the following error
COMException was unhandled Check the ErrorCode Property of the
exception to determine the HRESULT returned by the COM object.
I'm not sure why it is limiting it only to 5 checks.
Anyone have any ideas?
Consider using the built-in Registry support instead of the COM libraries. At a minimum, you'll get clearer error messages. It is supported in .NET 2.0.
http://msdn.microsoft.com/en-us/library/microsoft.win32.registry(v=vs.80).aspx
Good luck!