Problem with .net script - ApplicationSwitchImmediate

Hi All

I made a little script to create two Printscreens, for this I have to switch application. ( Because I don't know how to select / show the MCS for our Front and Backside programm)

I start the script always from manufacturing.

If I change inside the script to an another application than manufacturing I get a error message:

NXOpen.NXException: NX error status: 896084

If I go back manually to manufacturing and rerun the script it works fine. Does anybody have an Idea?

With best regards
Bizu

'
' V2.0 19.12.2017 / SBe
'	Creates a jpg from top and bottom view of the actual partfile
'
'
Option Strict Off  
Imports System  
Imports System.IO  
Imports NXOpen  
Imports NXOpen.UF  
 
Module Printscreen   
 
    Sub Main(ByVal args() As String)  
 
        Dim theSession As Session = Session.GetSession()  
        Dim ufs As UFSession = UFSession.GetUFSession()  
        Dim workPart As Part = theSession.Parts.Work  
        Dim partName As String = Path.GetFileNameWithoutExtension(workPart.FullPath)  
		Dim nullNXOpen_Features_Feature As NXOpen.Features.Feature = Nothing
 
		Dim displayPart As NXOpen.Part = theSession.Parts.Display
 
	'directory to output jpegs, change as needed
        Dim outputDirectory As String = "L:\USERS\cam\"  
        Dim strPartJpg As String = ""  
 
        If Not Directory.Exists(outputDirectory) Then  
            MsgBox("The specified directory does not exist, journal will now exit", MsgBoxStyle.Exclamation, outputDirectory & " not found")  
            Exit Sub  
        End If  
	Try	
		theSession.ApplicationSwitchImmediate("UG_APP_GATEWAY")
		Catch
		theSession.ApplicationSwitchImmediate("UG_APP_MANUFACTURING")
	End Try
 
	Try	
		theSession.ApplicationSwitchImmediate("UG_APP_GATEWAY")
		Catch
		theSession.ApplicationSwitchImmediate("UG_APP_GATEWAY")
	End Try
 
	' Top view
		workPart.ModelingViews.WorkView.Orient(NXOpen.View.Canned.Top, NXOpen.View.ScaleAdjustment.Fit)
			Dim scaleAboutPoint1 As NXOpen.Point3d = New NXOpen.Point3d(0.0, 0.0, 0.0)
			Dim viewCenter1 As NXOpen.Point3d = New NXOpen.Point3d(0.0, 0.0, 0.0)
			workPart.ModelingViews.WorkView.ZoomAboutPoint(0.8, scaleAboutPoint1, viewCenter1)
 
 
	' Wireframe 
		workPart.ModelingViews.WorkView.RenderingStyle = NXOpen.View.RenderingStyleType.StaticWireframe
		workPart.Views.Refresh()
 
	' create Y-Axis
		Dim datumAxisBuilder1 As NXOpen.Features.DatumAxisBuilder
		datumAxisBuilder1 = workPart.Features.CreateDatumAxisBuilder(nullNXOpen_Features_Feature)
		datumAxisBuilder1.Type = NXOpen.Features.DatumAxisBuilder.Types.YcAxis
		Dim nXObject1 As NXOpen.NXObject
		nXObject1 = datumAxisBuilder1.Commit()
		Dim datumAxisFeature1 As NXOpen.Features.DatumAxisFeature = CType(nXObject1, NXOpen.Features.DatumAxisFeature)
		datumAxisFeature1.SetName("Y")
 
	' create X-Axis plus
		Dim datumAxisBuilder2 As NXOpen.Features.DatumAxisBuilder
		datumAxisBuilder2 = workPart.Features.CreateDatumAxisBuilder(nullNXOpen_Features_Feature)
		datumAxisBuilder2.Type = NXOpen.Features.DatumAxisBuilder.Types.XcAxis
		Dim nXObject2 As NXOpen.NXObject
		nXObject2 = datumAxisBuilder2.Commit()
		Dim datumAxisFeature2 As NXOpen.Features.DatumAxisFeature = CType(nXObject2, NXOpen.Features.DatumAxisFeature)
		datumAxisFeature2.SetName("+X")
		datumAxisFeature2.Highlight
 
 
	workPart.WCS.Visibility = False
 
	' Export Jpeg Front
		'workPart.ModelingViews.WorkView.Fit()
        strPartJpg = partName & "_" & "v" & ".jpg"  
        strPartJpg = Path.Combine(outputDirectory, strPartJpg)  
        ufs.Disp.CreateImage(strPartJpg, UFDisp.ImageFormat.Jpeg, UFDisp.BackgroundColor.white)  
		datumAxisFeature2.Unhighlight
		datumAxisFeature2.HideBody
 
	' create X-Axis minus
		Dim datumAxisBuilder3 As NXOpen.Features.DatumAxisBuilder
		datumAxisBuilder3 = workPart.Features.CreateDatumAxisBuilder(nullNXOpen_Features_Feature)
		datumAxisBuilder3.Type = NXOpen.Features.DatumAxisBuilder.Types.XcAxis
		datumAxisBuilder3.IsAxisReversed = True
		Dim nXObject3 As NXOpen.NXObject
		nXObject3 = datumAxisBuilder3.Commit()
		Dim datumAxisFeature3 As NXOpen.Features.DatumAxisFeature = CType(nXObject3, NXOpen.Features.DatumAxisFeature)
		datumAxisFeature3.SetName("-X")
		datumAxisFeature3.Highlight
 
	' Rotate view 180° around Y-Axis 
		Dim origin1 As NXOpen.Point3d = New NXOpen.Point3d(0.0, 0.0, 0.0)
		Dim vector1 As NXOpen.Vector3d = New NXOpen.Vector3d(0.0, 1.0, 0.0)
		workPart.ModelingViews.WorkView.Rotate(origin1, vector1, 180.0)
		'workPart.ModelingViews.WorkView.Fit()
		workPart.Views.Refresh()
 
 
	' Export Jpeg Back
        strPartJpg = partName & "_" & "r" & ".jpg"  
        strPartJpg = Path.Combine(outputDirectory, strPartJpg)  
        ufs.Disp.CreateImage(strPartJpg, UFDisp.ImageFormat.Jpeg, UFDisp.BackgroundColor.white)  			
 
	'Shaded + Home view
		workPart.ModelingViews.WorkView.Orient(NXOpen.View.Canned.Trimetric, NXOpen.View.ScaleAdjustment.Fit)
		workPart.ModelingViews.WorkView.RenderingStyle = NXOpen.View.RenderingStyleType.ShadedWithEdges
		datumAxisFeature3.Unhighlight
		datumAxisFeature3.HideBody
		datumAxisFeature1.HideBody
 
	theSession.ApplicationSwitchImmediate("UG_APP_MANUFACTURING")
 
	' delete datum axis
		Dim markId1 As Session.UndoMarkId
		markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, "Delete")
 
		Dim notifyOnDelete1 As Boolean
		notifyOnDelete1 = theSession.Preferences.Modeling.NotifyOnDelete
		theSession.UpdateManager.ClearErrorList()
		Dim objects1(2) As NXOpen.NXObject
			objects1(0) = datumAxisFeature1
			objects1(1) = datumAxisFeature2
			objects1(2) = datumAxisFeature3
 
		Dim nErrs1 As Integer
		nErrs1 = theSession.UpdateManager.AddToDeleteList(objects1)
 
		Dim nErrs2 As Integer
        nErrs2 = theSession.UpdateManager.DoUpdate(markId1)
 
    End Sub  
 
 
	Public Function GetUnloadOption(ByVal dummy As String) As Integer  
 
	'Unloads the image when the NX session terminates
        GetUnloadOption = NXOpen.Session.LibraryUnloadOption.AtTermination  
 
    End Function  
 
End Module