Both are okaybut the meanings are not quite the sameso it depends on context.
If the person is about to go somewhere (more or less immediately)and this will involve passing by the windowyou can ask them to close it "on your way past"or "as you pass it".
"When" would be better suited to a situation where the anticipated passing is later. This would be roughly equivalent to "the next time you pass it" (whenever that might be).