Can't change assembly references in Addin.

I need to change some document reference in SW Addin. So I use C# API methods like this:

---Some Code---

var swDoc = (SwDMDocument8)swDocMgr.GetDocument(<FILENAME>,

                                                                         SwDmDocumentType.swDmDocumentAssembly,

                                                                         false, out oe);

--Some Code ---

swDoc.ReplaceReference(<OLD REFERENCE>, <NEW REFERENCE>);

When I execute this code in stand alone application old references chenge to new references. All going great.

But when i try to execute this code in SW Addin in which document <FILENAME> is already open this code:

var swDoc = (SwDMDocument8)swDocMgr.GetDocument(<FILENAME>,

                                                                         SwDmDocumentType.swDmDocumentAssembly,

                                                                         false, out oe);


return null (swDoc == null). So I tryed to close <FILENAME> document first. I tryed this:

SwApp.CloseDoc(<FILENAME>); // Return error "The object invoked has disconnected from its clients"

and this:

var swModel = (ModelDoc2) _iSwApp.ActiveDoc;

-- SomeCode--

swModel.Save();

swModel.Close(); // Return  "unimplemented exception"

How can I

1) replace reference in already opened document

      or

2)correctly close this document, replace reference and then open it?

Any issues will be great. Also, I'm new in SW.

SolidworksApi macros