I wrote a script to save a word or excel document as a PDF with the same file card data, then check in the new PDF. It works, but I keep getting this error for each file:
All the files still check in just fine with the variables populated after click ok. I can put a breakpoint on the last line of the script (batchUnlocker.UnlockFiles(0, Nothing)) and that's where this happens with no additional debug information. I have no idea how to try to fix this.
Imports System.Windows.Forms
Imports System.IO
Imports System.Runtime.InteropServices
Imports Microsoft.Office.Interop.Word
Imports Microsoft.Office.Interop.Excel
Imports EdmLib
Public Class Class1
Implements IEdmAddIn5
Dim batchAdder As IEdmBatchAdd2
Dim batchUnlocker As IEdmBatchUnlock2
Dim fileList As IEdmSelectionList6
Dim Files(0) As EdmAddFileInfo
Public Sub GetAddInInfo(ByRef poInfo As EdmAddInInfo, ByVal poVault As IEdmVault5, ByVal poCmdMgr As IEdmCmdMgr5) Implements IEdmAddIn5.GetAddInInfo
' Fill in the add-in's description
poInfo.mbsAddInName = "Convert to PDF Add-in"
poInfo.mbsCompany = "RadiaBeam Technologies"
poInfo.mbsDescription = "Makes PDF copy of Office files"
poInfo.mlAddInVersion = 1
' Minimum SOLIDWORKS PDM Professional version needed for VB.NET add-ins is 6.4
poInfo.mlRequiredVersionMajor = 6
poInfo.mlRequiredVersionMinor = 4
' Register a menu command
'poCmdMgr.AddCmd(1, "VB.NET Add-in", EdmMenuFlags.EdmMenu_Nothing)
poCmdMgr.AddHook(EdmCmdType.EdmCmd_PostState)
poCmdMgr.AddCmd(1, "Convert Office Doc to PDF", EdmMenuFlags.EdmMenu_OnlyFiles)
End Sub
Public Sub OnCmd(ByRef poCmd As EdmCmd, ByRef ppoData As System.Array) Implements IEdmAddIn5.OnCmd
Try
Dim v8 As IEdmVault8 = poCmd.mpoVault
Dim AffectedFile As EdmCmdData
Dim AffectedFileNames As String = ""
Select Case poCmd.meCmdType
Case EdmCmdType.EdmCmd_PostState, EdmCmdType.EdmCmd_Menu
Dim afileinfo As New srcFileInfo
For Each AffectedFile In ppoData
If poCmd.mlCmdID = 1 Then
If AffectedFile.mlObjectID1 <> 0 Then
For Each x As srcFileInfo In srcFullFileInfo
Dim sourceFile As IEdmFile5
Dim filepath As String
sourceFile = poCmd.mpoVault.getobject(EdmObjectType.EdmObject_File, AffectedFile.mlObjectID1)
afileinfo.srcFile = sourceFile
afileinfo.fileName = AffectedFile.mbsStrData1 'File Name with extension
filepath = sourceFile.GetLocalPath(AffectedFile.mlObjectID3)
afileinfo.filePath = filepath
afileinfo.fileName = Path.GetFileNameWithoutExtension(filepath) 'Remove extension
srcFullFileInfo.Add(afileinfo)
Next x
End If
ElseIf AffectedFile.mlObjectID3 = 168 Then
Dim filepath As String = AffectedFile.mbsStrData1
afileinfo.filePath = filepath
afileinfo.srcFile = v8.GetFileFromPath(filepath)
afileinfo.fileName = Path.GetFileNameWithoutExtension(filepath)
srcFullFileInfo.Add(afileinfo)
End If
Next
If srcFullFileInfo.Count <> 0 Then
'Initialize Batch Add Utility
batchAdder = v8.CreateUtility(EdmUtility.EdmUtil_BatchAdd)
Dim sFileInfo As New FileInfo
For Each x As srcFileInfo In srcFullFileInfo 'FAILS ON LAST ONE
Dim srcFile As IEdmFile5 = x.srcFile
Dim folderPath As String = Path.GetDirectoryName(x.filePath)
Dim ext As String = Path.GetExtension(x.filePath)
Dim vars As IEdmEnumeratorVariable5 = srcFile.GetEnumeratorVariable
Dim filePath As String = x.filePath
Dim Folder As IEdmFolder5 = v8.GetFolderFromPath(folderPath)
vars.GetVar("Revision", "", sFileInfo.Revision)
Dim Revision As String = sFileInfo.Revision
If Strings.InStrRev(Revision, "/") > 0 Then
Revision = Strings.Replace(Revision, "/", "")
End If
Dim NewFileName As String = x.fileName & "-" & Revision
Dim NewFilePath As String = folderPath & "\" & NewFileName
Select Case ext
Case ".doc", ".docx", ".docm", ".dotx"
Dim WordApp As Microsoft.Office.Interop.Word.Application = CreateObject("Word.Application")
Dim WordDoc As Document = WordApp.Documents.Open(filePath)
WordApp.Visible = False 'perform silently
WordDoc.ExportAsFixedFormat(NewFilePath, WdExportFormat.wdExportFormatPDF)
WordDoc.Close(Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges)
WordApp.Quit()
WordDoc = Nothing
WordApp = Nothing
Case ".xls", ".xlsx", ".xlsm", ".xlst"
Dim xlApp As Microsoft.Office.Interop.Excel.Application = CreateObject("Excel.Application")
Dim xlBook As Workbook = xlApp.Workbooks.Open(filePath)
xlApp.Visible = False 'performs silently
xlBook.ExportAsFixedFormat(XlFixedFormatType.xlTypePDF, NewFilePath, XlFixedFormatQuality.xlQualityStandard, True, False, 1, 10, False)
xlBook.Close(Microsoft.Office.Interop.Excel.XlSaveAction.xlDoNotSaveChanges)
xlApp.Quit()
xlBook = Nothing
xlApp = Nothing
Case Else
NewFileName = ""
NewFilePath = ""
End Select
If NewFileName <> "" Then
batchAdder.AddFileFromPath(NewFilePath & ".pdf", Folder.ID)
'vars.GetVar("Revision", "", sFileInfo.Revision)
vars.GetVar("Author", "", sFileInfo.Author)
vars.GetVar("Approval Date", "", sFileInfo.RevDate)
vars.GetVar("Document Number", "", sFileInfo.DocNumber)
vars.GetVar("FileName", "", sFileInfo.DocName)
FullFileInfo.Add(sFileInfo)
End If
Next
Dim ppoAddedFiles(FullFileInfo.Count - 1) As EdmFileInfo
If batchAdder IsNot Nothing Then
Dim results As Integer
results = batchAdder.CommitAdd(0, ppoAddedFiles)
batchUnlocker = v8.CreateUtility(EdmUtility.EdmUtil_BatchUnlock)
Dim ppoSelection() As EdmSelItem
ReDim ppoSelection(ppoAddedFiles.Count - 1)
Dim j As Integer = 0
For Each x As FileInfo In FullFileInfo
Dim NewPath As String = ppoAddedFiles(j).mbsPath
If NewPath <> "" Then
Dim currFile As IEdmFile5 = v8.GetFileFromPath(NewPath)
If currFile IsNot Nothing Then
Dim currFolderPath As String = Path.GetDirectoryName(NewPath)
Dim currFolder As IEdmFolder5 = v8.GetFolderFromPath(currFolderPath)
Dim PDFvars As IEdmEnumeratorVariable8 = currFile.GetEnumeratorVariable
PDFvars.SetVar("Revision", "", x.Revision)
PDFvars.SetVar("Author", "", x.Author)
PDFvars.SetVar("Approval Date", "", x.RevDate)
PDFvars.SetVar("Document Number", "", x.DocNumber)
PDFvars.SetVar("FileName", "", x.DocName)
PDFvars.Flush()
PDFvars.CloseFile(True)
PDFvars = Nothing
ppoSelection(j).mlDocID = currFile.ID
ppoSelection(j).mlProjID = currFolder.ID
End If
End If
j = j + 1
Next x
batchUnlocker.AddSelection(v8, ppoSelection)
If batchUnlocker IsNot Nothing Then
batchUnlocker.CreateTree(0, EdmUnlockBuildTreeFlags.Eubtf_ShowCloseAfterCheckinOption + EdmUnlockBuildTreeFlags.Eubtf_MayUnlock)
batchUnlocker.Comment = "This file was automatically added by Convert to PDF Add-in"
batchUnlocker.UnlockFiles(0, Nothing)
End If
End If
End If
Case Else
End Select
Catch ex As Runtime.InteropServices.COMException
MessageBox.Show("HRESULT = 0x" + ex.ErrorCode.ToString("X") + vbCrLf + ex.Message)
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
End Class