My flight lands late at evening. The ticket offices are closed at midnight. I can't buy a Navigo card in the machines.
How can I buy a RER B or Roissybus tickets at CDG if I don't have a Navigo card?
Alternatively, how can I buy a Navigo card in case ticket offices are closed?
RER B train tickets from CDG to Paris are sold at ticket vending machines at the CDG Airport train stations. But keep in mind, from now until Sept 29, 2023 the last RER B train departs CDG Terminal 2 at 22:59 (10:59pm) due to construction works. From Sept 30, 2023 onwards, the last RER B train from CDG Terminal 2 train station departs at 23:48 (11:48pm, the usual last train from CDG T2). After 11:48pm in the evenings the only public transportation option from CDG to Paris is the N143 night bus from CDG Terminal 2 to Gare du Nord, costing 4.20€, which runs every 30 minutes throughout the night until RER B begins service again at 04:50.
You cannot buy a Navigo card when ticket offices are closed. They are not sold from automated ticket vending machines. You'll need to visit a ticket window or office during open hours. If you're in Paris city centre you can do this at most Metro, RER, Transilien ticket windows. (I say most as from time to time a ticket window may run out of card blanks and they'll direct you to find another.)