I'm going preface this by saying that anything I don't back up with a source, I've learned by my experience and study as a Historic European Martial Artist (a HEMA practitioner, for the acronym minded). This answer has some background before actually proposing solutions, but that background helps to understand what you need to change.
The Modern Case for Swords
There are actually modern situations where a sword would do much better than a gun. The police have a "21 foot rule." Basically, a person with a knife (or sword) wins against a person trying to draw a gun. Even with a gun drawn, 10 feet beats a gun.
Body armor for bullets does not mean body armor against knives or blades, as seen here. In fact, some companies which attempt to provide prisons guards armor need to change their armor to adapt to stabs. (As you can see here, this armor is specifically meant for protection against stabs, not bullets! They also sell different armor for ballistic protection because they're not the same thing!) Additionally, it's been known for a long time that you aim for the weak spots, so don't stab/shoot someone in the gut when you can go for the unprotected face! That makes this whole armor argument a much more tricky!
So swords in warfare? This isn't something we've seen for a long time, mostly because it's hard to get a large force within 21 feet of solders. As seen in this reddit post, where veterans and solders talk about it, bayonets and some knives still see use in modern warfare. These blades are usually used in urban areas, where you may not have time to aim your gun.
Can a sword do a little better than a bayonet or knife? Well yes; you will (generally) have increased reach, so you can kill the other guy before he reaches you. Additionally, swords can have more cutting (or thrusting) power than a knife, and can also be used in grappling.
Actually, this is a general trend in combat in general; if a weapon allows you to kill them before they can even reach you, or allows you to fight at the same distance, you use that weapon first! (Tactical considerations aside...) I'm calling this the "kill them at distance" (KTAD) principle.
Why No Swords Today?
Coming from a Historic European Martial Arts background, I know that swords were more commonly sidearms than primary weapons. The weapon of war before the rifle was, generally, a staff weapon. (Staff weapons include spears, lances, and other pole-arms.) This is because of the KTAD principle.
Pike formations were even common into the late renaissance, where muskets were common. The bayonet became popular because you can get some advantages of staff weapons but also retain your ability for long-distance combat. (Although some people say bayonets may not actually have seen much killing...) During the colonial age, many solders used swords as a backup weapon because they were often outnumbered and their gun was not up to the task of killing that many people before they were overrun.
In any case, as guns became easier to use and their rate of fire and capacity for armor penetration increased, people took the KTAD principle and selected to use the weapon that kills the enemy without putting themselves in mortal danger. Early in the days of muskets, people did still wear plate, and it did help against musket fire. Eventually, though, the power of the bullets were simply too much for the armor of the time, so the increased weight of the armor was a liability and therefore ditched.
How Do We Bring Swords Back?
- Make gun operation more expensive. Some component of the gun, ammunition for the gun, or even raw materials for the gun may be too rare for guns to be widely adopted, such as cannons or guns in the Middle Ages of Europe.
- Fail to develop good methods of gunpowder manufacture.
- Make really good anti-ballistic armor. This is the case in DUNE series.
- Have battles take place in confined areas or areas of poor visibility, which requires the fast reaction time for combat.
- Have strong cultural preference for carrying swords, such as the case of the Gunto (Japanese WW2 swords) or British Officers during the colonial era.
- Develop a strong cultural preference for handguns. When the gun jams, or when the solder cannot reload, they switch to their side-swords. (This does set up your culture for an extreme shift in thinking about combat, such as that Shaka's shortspear did for the Zulu.)
- Fail to develop timing mechanisms, which then prevents machine guns from developing.