ERCP can be instrumental in cancer diagnosis by allowing direct visualization and access to the bile and pancreatic ducts. During the procedure, physicians can obtain biopsy samples for histological examination, which is crucial for confirming the presence and type of cancer. This can guide the formulation of an appropriate treatment plan.