I am having trouble with winform opening a drawing. The error I am getting says NullReferenceException was unhandled and is highlighting the pathway. any help is appreciated. Thanks
private void button2_Click(object sender, EventArgs e)
{
//Open Solidworks Drawing
ModelDoc2 swModel = default(ModelDoc2);
DocumentSpecification swDocSpecification = default(DocumentSpecification);
string sName = null;
long longstatus = 0;
long longwarnings = 0;
// Drawing document path and name
swDocSpecification = (DocumentSpecification)swApp.GetOpenDocSpec("C:\\location\\????.slddrw");//File Location
sName = swDocSpecification.FileName;
// Sheet name
swDocSpecification.SheetName = "BOM"; //Open to the BOM sheet
swDocSpecification.DocumentType = (int)swDocumentTypes_e.swDocDRAWING;
swDocSpecification.ReadOnly = true;
swDocSpecification.Silent = false;
// Open the specified sheet in the specified drawing document
swModel = swApp.OpenDoc7(swDocSpecification);
longstatus = swDocSpecification.Error;
longwarnings = swDocSpecification.Warning;
}
There are two possibilities as to why you’re getting the NullReferenceException
GetOpenDocSpecwon’t workIt should pretty easy to just check if swApp == null using your debugger. Using the autos or watch windows, hovering over the variable, ?swApp == null from the command window, etc.