To automatically set the COM port number in a Tera Term macro, use the COMx command, where x represents the desired port number. You can also retrieve the available COM port dynamically by using the getserial function.