1. I will have thought Spanish for ten years by 2018.

2. I will have thought Spanish for ten years in 2018.
(Can we use both prepositions?)

3. He will have gone to Rome when you come next month.
4. He will have gone to Rome if you come next month.
5. He will have gone to Rome by the time you come next month.
6. He will have gone to Rome by next Sunday.
(Can we use all the senteces? Are all grammatical?)

3, 4, 5, and 6 are all correct and usable, yes.

I don't understand 1 or 2.

Thank you. I corrected errors.

1. I will have taught Spanish for ten years by 2018.
2. I will have taught Spanish for ten years in 2018.
(Can we use both prepositions?)

Yes.

1. I will have thought Spanish for ten years by 2018.

Yes, this sentence is grammatically correct. By using the preposition "by," you are indicating that by the year 2018, you would have completed ten years of studying Spanish.

2. I will have thought Spanish for ten years in 2018.

This sentence does not sound natural. It seems like you are trying to express that in the year 2018, you will have already studied Spanish for ten years. In this case, it would be more common to use the preposition "for" instead of "in." The correct sentence would be, "I will have studied Spanish for ten years in 2018."

3. He will have gone to Rome when you come next month.

Yes, this sentence is grammatically correct. It suggests that by the time you come next month, he will already have gone to Rome.

4. He will have gone to Rome if you come next month.

This sentence is grammatically correct, but it has a conditional meaning. It implies that if you come next month, he will have gone to Rome. In other words, his trip to Rome is dependent on you coming next month.

5. He will have gone to Rome by the time you come next month.

Yes, this sentence is grammatically correct. It indicates that before you come next month, he will have already gone to Rome.

6. He will have gone to Rome by next Sunday.

Yes, this sentence is grammatically correct. It means that before next Sunday arrives, he will have already gone to Rome.