Announcements
Attention for Customers without Multi-Factor Authentication or Single Sign-On - OTP Verification rolls out April 2025. Read all about it here.
Rossco44
576 Views, 9 Replies

Trying to add text to an idw, unable to cast COM object of type System._ComObject

Hey all,

I'm trying to add a text box to an idw file, but I'm getting the following error.

Rossco44_0-1669944236593.png

The weird part is that the code will open the file, and add the text box with the correct input, but THEN throws this error? It is happening within another subroutine that is mass-exporting PDFs from all passed idw files within the assembly that runs the rule.

 

Sub MakePDFFromDoc(oDocument As String, UserSelectedAction As Integer, folderName As String, jobNum As String, Qty As String)
 	oPDFAddIn = ThisApplication.ApplicationAddIns.ItemById("{0AC6FD96-2F4D-42CE-8BE0-8AEA580399E4}")
	
		
'	oMsg = MessageBox.Show(oDocument, "FolderName")
  
	oContext = ThisApplication.TransientObjects.CreateTranslationContext
	oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
	oOptions = ThisApplication.TransientObjects.CreateNameValueMap
	oDataMedium = ThisApplication.TransientObjects.CreateDataMedium
	
	oFile = ThisApplication.Documents.Open(oDocument, True)
	
	'Create Job Number/Qty Text Box on the idw
	Dim oDrawDoc As DrawingDocument = ThisApplication.ActiveDocument
	Dim oSketch As DrawingSketch = oDrawDoc.ActiveSheet.Sketches.Add
	oSketch.Edit
	
	Dim oTG As TransientGeometry = ThisApplication.TransientGeometry
	
	Dim JBText As String = "<" & jobNum & ">"
	Dim oTextBox As System.Windows.Forms.TextBox 
	oTextBox = oSketch.TextBoxes.AddFitted(oTG.CreatePoint2d(2, 2), JBText)
	
	oSketch.ExitEdit

	oFullFileName = oFile.File.FullFileName
'	oMsg = MessageBox.Show(folderName, "oFullFileName")
	oPath = Left(oFullFileName, InStrRev(oFullFileName, "\") -1)
'	oMsg = MessageBox.Show(folderName, "oPath")
	oFileName = Right(oFullFileName, Len(oFullFileName) -InStrRev(oFullFileName, "\"))
'	oMsg = MessageBox.Show(folderName, "oFileName")
	oFilePart = Left(oFileName, InStrRev(oFileName, ".") -1)
'	oMsg = MessageBox.Show(folderName, "oFilePart")
 
	oOptions.Value("All_Color_AS_Black") = 0
	oOptions.Value("Remove_Line_Weights") = 0
	oOptions.Value("Vector_Resolution") = 400
	oOptions.Value("Sheet_Range") = Inventor.PrintRangeEnum.kPrintAllSheets
 
	'get PDF target folder path
	oFolder = folderName
'	oMsg = MessageBox.Show(oFolder, "oFolder")
	oDirectoryName = System.IO.Path.GetDirectoryName(oFullFileName)
'	oMsg = MessageBox.Show(oDirectoryName & oFolder, "oDirectoryName")
 
	'Check for the PDF folder and create it if it does not exist
	If Not System.IO.Directory.Exists(oFolder) Then
		System.IO.Directory.CreateDirectory(oFolder)
	End If
 
	'Set the PDF target file name
	oDataMedium.FileName = oFolder & "\" & oFilePart & ".pdf"
	 
	'Publish document
	If (UserSelectedAction = 1) Or (UserSelectedAction = 3) Then
		oPDFAddIn.SaveCopyAs(oFile, oContext, oOptions, oDataMedium)'For PDF's
	End If
 
	If (UserSelectedAction = 2) Or (UserSelectedAction = 3) Then
		oFile.SaveAs(oFolder & "\" & oFilePart & ".dwg", True) 'For DWG's
	End If

	oFile.Close
 
'------end of iLogic-------
End Sub

The idw file's full address is passed in. oFile was originally running as an invisible open, but I had to change it as it was trying to add the textbox to the assembly file haha.