Scripting SQL Server Replication
User who runs the package must have access to the folder to which you are writing the file. User also needs access to SQL Server with sysadmin rights.

Also, verify you have properly added all libraries.

For more, please read my blog article at http://www.dbadrive.in/2013/03/ssis-package-to-backup-replication_25.html.

Please share some more details of your code if the error persists.