Highlighted
Absent Member.
Absent Member.
1650 views

[archive] Calling COBOL From Outlook

[Migrated content. Thread originally posted on 05 November 2010]

We're adding a button to allow users to save a copy of an email into our program. The button calls a COBOL program using wrunnet.dll like this:

Imports acucobol
Public Class MailToCOBOL
   Implements IDisposable
   Private COBOL As CVM
   Private iFilename As String
   Private iUsername As String
   Private BasePath As String = "J:\CFDEV2"
   Public Sub New()
      If COBOL Is Nothing Then
         COBOL = New CVM
      End If
      COBOL.Debug = True
      COBOL.NoStop = False
      COBOL.ExtendedError = True
      COBOL.RunPath = IO.Path.Combine(BasePath, "BIN")
      COBOL.Cache = False
      COBOL.ConfigFile = IO.Path.Combine(BasePath, "ETC\CBLCONFI")
   End Sub
   Public Sub SendMessage()
      Try
         Environment.CurrentDirectory = COBOL.RunPath
         Dim PATH As String = Environment.GetEnvironmentVariable("PATH")
         If Not PATH.Contains(";" & COBOL.RunPath & ";") Then
            Environment.SetEnvironmentVariable("PATH", PATH & ";" & COBOL.RunPath & ";")
         End If
         Dim ErrCode As errorTypes
         Dim RtnCode As Integer
         Dim Linkage(1) As Object
         Linkage(0) = sUsername.ToString.PadRight(1024, " "c)
         Linkage(1) = sFilename.ToString.PadRight(1024, " "c)
         ErrCode = COBOL.Call("SAVEEMAIL.cob", Linkage, Nothing, "", RtnCode)
         COBOL.CancelProgram("SAVEEMAIL.cob")
         sUsername = Linkage(0).ToString
         If Not (ErrCode = errorTypes.CS_OK) Then
            MsgBox(ErrCode.ToString & vbCrLf & COBOL.LastErrorMsg.ToString)
         End If
         COBOL.CancelAllPrograms()
         COBOL.UnloadAllPrograms()
      Catch ex As Exception
         MsgBox(ex.ToString)
      End Try
   End Sub
End Class


Everything works well the first time through. After the call is completed, the calling class calls the Dispose method of MailToCOBOL, and in the Dispose method, we call "UnloadAllPrograms" and set COBOL = Nothing.
The second time this class is instantiated, the code appears to work fine, but the window is never opened (from within COBOL), and when the program gets to the ACCEPT, it gets a 97, so we exit. I have tried opening both INDEPENDENT and FLOATING windows, and either way, nothing appears on the screen.
It may be telling that the second time in, the values in our EXTERNAL data items are still there, even though we cancel the program, and set the COBOL object to nothing. I have tried calling the "Shutdown" method, but that just throws an exception. If I put a "STOP RUN" at the end of the program (in COBOL), the 2nd call results in a CS_COBOL_FATAL_ERROR message.
I'm sure there's something I'm doing incorrectly here, can anyone help?
Thanks in advance,
-Chris
0 Likes
7 Replies
Highlighted
Absent Member.
Absent Member.

RE: [archive] Calling COBOL From Outlook

You should try call the Shutdown method at the end.
0 Likes
Highlighted
Absent Member.
Absent Member.

RE: [archive] Calling COBOL From Outlook

Gisle;
When I call the "shutdown" method (I'm not sure what the "message" parameter is supposed to be, I just used a zero), the whole application shuts down, with no message or additional information (it closes Outlook).

-Chris
0 Likes
Highlighted
Absent Member.
Absent Member.

RE: [archive] Calling COBOL From Outlook

Ah, I mixed up. Sorry, you should not do that, you are right. (It is even stated in the docs, shame on me)
0 Likes
Highlighted
Absent Member.
Absent Member.

RE: [archive] Calling COBOL From Outlook

A little more information, which may or may not be helpful... When I get to the last line of my program (GOBACK or STOP RUN), the initial window doesn't close unless I have the .NoStop property set to False, the problem being that this also causes Outlook to close. If .NoStop is set to True, the window doesn't go away until control returns to Outlook, and when I try to run the program again, I never see the windows that should be displayed.
0 Likes
Highlighted
Absent Member.
Absent Member.

RE: [archive] Calling COBOL From Outlook

Another interesting piece of trivia. Due to some issues with the CVM closing Outlook, I had started calling MailToCOBOL in a thread. This seems to have been part of the problem. Once I stopped calling in a thread I was able to get windows to open properly in subsequent calls to the DLL. I'm still working out a few quirks, but it seems to be working...
0 Likes
Highlighted
Absent Member.
Absent Member.

RE: [archive] Calling COBOL From Outlook

.NoStop is imperative if you use STOP RUN. If this is FALSE (default), the entire process is shut down.
0 Likes
Highlighted
Absent Member.
Absent Member.

RE: [archive] Calling COBOL From Outlook

I found that out 😄
0 Likes
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.