At primary school and beyond, we are told that it is terrible and wrong to say me as in the sentence Jim and me went to town*. Of course, this is indeed wrong, because I am part of the subject of the verb. An English speaker should not say Me went to town* : hence the sentence with Jim in it is also wrong. So far, so good.
The problem is that people remember that Jim and me is wrong, so that they wil choose to say or write He gave the book to Jim and I* because they see it as wrong, sinful, terrible, awful, abominable to say "Jim and me". They are wrong. Nobody would ever dream of saying He gave the book to I*, yet they quite cheerfully persist in this misuse of the English language.
The rule is quite simple: if the speaker is the subject of the sentence or a part of the subject, use I and if the object or part of the object, use me. If you're not sure, try it without the other person and you won't ever get it wrong.
Strangely, this is only a problem for single people, as members of a group don't have the problem. Correct: Jim and we went to town. Incorrect: Jim and us went to town*. As part of the subject, use we. Correct: They screamed at Jim and us. Incorrect: They screamed at Jim and we*. Of course, you won't ever hear anyone say that, but you could well encounter They screamed at Jim and I* which is just as wrong.
It's a simple rule, and one that people get wrong frequently. Please get it right, so that I can listen to you without being distracted!