Change Drawing Reference - DocumentManager

Hi there, I have been running up against a consistent issue while trying to replace the referenced file for a drawing with swDM. SwDmDocumentSaveError always returns a zero indicating that it saved happily, but when i check the reference through solidworks open dialog, no change has taken place.

Also i have made absolutely sure that I am using getdocuemnt with the open as read only value set to false. I have additionally tried calling the replace both before, and after using CloseDoc. Either way there are no open errors, no save errors, but no results.

I am aware that this has been touched on in other threads, including this one where i left a very similar comment. Not a lot of traffic there though since it has been marked answered and i decided it would be best to repost this as a new disscussion since the problem obviously has a different solution.

The code I'm using does a lot of other things, so i will just post the relevant (failing) portion here for your picking pleasure.

(and yes, the licKey is present elsewhere in the code, all other swDm methods are working fine)


<p>    'Move drawing files to appropriate folders and re-referenceto new parent files</p><p>    For i = LBound(drwFiles) To UBound(drwFiles)</p><p></p><p>    If Not drwFiles(i) = Empty Then</p><p>        </p><p>        Set swDoc = swDm.GetDocument(drwFiles(i), SwDmDocumentType.swDmDocumentPart, False, mOpenErrors)</p><p>          Debug.Assert (SwDmDocumentOpenError.swDmDocumentOpenErrorNone = nRetVal)</p><p>          swDoc.CloseDoc</p><p>        </p><p>        '/Set an array of existing references</p><p>        DrwRefArr = GetDrawingRefs(drwFiles(i))</p><p>        '/Find appropriate b or d folder based on partNo</p><p>        destFolder = FindDrwFolder(drwFiles(i))</p><p>        </p><p>            '/use existing reference array to set new references</p><p>            Debug.Print "References Replaced for drawing: " & drwFiles(i)</p><p>            For j = LBound(DrwRefArr) To UBound(DrwRefArr)</p><p>              oldRef = DrwRefArr(j)</p><p>              </p><p>              If Not oldRef = Empty Then</p><p>                'Debug.Print oldRef & " Is not empty"</p><p>                </p><p>                If InStr(oldRef, "BOX") Or InStr(oldRef, "PUSHER") Then</p><p>                    'Debug.Print oldRef & " Is not empty but contains box or pusher"</p><p>                    oldRef = ""</p><p>                Else</p><p>                    newRef = Replace(drwFiles(i), extFind(drwFiles(i)), extFind(oldRef))</p><p>                    'newRef = strOutputPath & GetFullPartNo(oldRef) & extFind(oldRef)</p><p>                    swDoc.ReplaceReference oldRef, newRef</p><p>                    swDoc.Save</p><p>                    swDoc.CloseDoc</p><p>                        Debug.Print "   OLD REFERENCE: " & oldRef & " has been replaced with NEW REFERENCE: " & newRef</p><p>                        Debug.Print ""</p><p>                    '/Empty ref containers</p><p>                    oldRef = ""</p><p>                    newRef = ""</p><p>                        </p><p>                End If</p><p>              Else</p><p>              End If</p><p>            Next j</p><p>        </p><p>        newName = destFolder & "\\" & GetFullPartNo(drwFiles(i)) & extFind(drwFiles(i))</p><p>        newName = UCase(newName)</p><p>        </p><p>        '/Move Drawing to appropriate b or d folder</p><p>        moveError = swDm.MoveDocument(swDoc.FullName, newName, fromChildren, toChildren, vOption, pSrcOption)</p><p>            If Not moveError = 0 Then</p><p>            Debug.Print "ERROR CODE -" & moveError & "- " & drwFiles(i) & " failed to move to " & FindDrwFolder(newName)</p><p>            Debug.Print ""</p><p>            Else</p><p>            Debug.Print drwFiles(i) & " Has been moved to " & FindDrwFolder(newName)</p><p>            Debug.Print ""</p><p>            End If</p><p>            drwFiles(i) = newName</p><p>        </p><p>    End If</p><p>    </p><p>    Set swDoc = Nothing</p><p>    destFolder = ""</p><p>    refExt = ""</p><p>    </p><p>    Next i</p><p></p>


SolidworksApi macros
Answers 2
SF
SF 2013-10-21 Best Answer

Just an update since i got Zero reply on this lol. If anyone is having similar issues, I've since had good luck using the non DM method  "SldWorks.ReplaceReferencedDocument"

SF
SF 2016-02-08

Hello. I had this same problem and I just found solution. You need to get references from file and just then replace them. I stored information of files in list of objects so I had clear information which reference should be replaced, what is new reference. I try just use referenceReplace() with arguments. I was wrong. Here is example of procedure for replacing references: http://wklej.org/id/1932893/

I believe it'll help someone in the future.