As Thanee says, you can make a ranged sneak attack by simply attacking while invisible, or during the surprise round where opponents who haven't acted yet are considered flat-footed.
You cannot flank an opponent with a ranged weapon or ranged attack. Read the rogue class' description of Sneak Attack and the Combat chapter of the Player's Handbook or the System Reference Document. You cannot sneak attack just any flanked creature, you can only sneak attack a creature you are flanking personally, by being in melee reach (your own; it doesn't matter if the foe has longer reach, only your own reach matters for purposes of you flanking someone). You CAN, however, sneak attack a foe with a ranged weapon if that foe is currently denied a Dex bonus to AC against your attack, such as when you're invisible (if the foe can see you invisible, then the foe isn't denied its Dex bonus to AC).
Flanking does not deny the opponent Dex to AC. Flanking only means that you get +2 on attack rolls versus the flanked opponent(s), and for rogues it means that the opponent(s) being flanked is/are now susceptible to sneak attacks.
Sneak attack applies any time you flank an opponent, and also applies any time your opponent is denied a Dexterity bonus to AC. If your rogue flanks an opponent, they can deal Sneak Attack damage (in 3.5 Edition only) with every melee attack they make against the opponent they flank. If your opponent is somehow denied a Dex bonus to AC against all your attacks at a given time, your rogue can do the same. However, most effects that deny a Dex bonus to AC only last for 1 actual attempted attack.
Remember that Invisibility ends immediately after you make one attack against anyone. Improved Invisibility ends immediately after the second attack you make against anyone; it doesn't have to be a second attack in the same round, just the second attack you try during the Improved Invisibility's entire duration. During a surprise round, you normally only get a single partial action (which normally doesn't allow a full-attack action, nor use of two weapons), and that's only if you did the surprising.
Anything that isn't vulnerable to critical hits, such as Undead, Constructs, Plants, and objects, is also NEVER vulnerable to sneak attacks.
All of these are rules you will find in the Player's Handbook, Dungeon Master's Guide, or System Reference Document.