Federer faced surprisingly strong resistance from the Czech qualifier, ranked 342nd, before smashing a crosscourt volley to break serve for a decisive 4-3 lead in the second set.
"It was difficult,'' Federer said. "When it happens you can't play your game and it isn't working, then it's a surprise. It was an awkward sort of match.''
Federer ran his win streak on grass to 56 matches, including 22 at Halle, dating back to 2003. The top-ranked Swiss has won four titles here, going on to win Wimbledon three weeks later each time.
Federer's win came against a player who has just one win on the ATP tour this season - in the first round here against Scott Lipsky. The 32-year-old Vacek usually plays on the lower-level Challenger tour.
Federer said a tough match was as important as an easy one, such as his first-round rout of Germany's Michael Berrer in just over an hour.
"Now I've played a righty and a lefty,'' Federer said. "On the easy wins you feel good and don't think about it; on the hard ones you think about what you have to do better.''
France's Michael Llodra also reached the quarterfinals by beating Steve Darcis of Belgium 7-6 (6) 7-6 (3).
Federer falls to Stepanek for 6th loss this year
EURO TOUR COULD TAKE THE MICK
DINWIDDIE SEES FLAG MIX-UP AS OMEN