Are you dropping to filesystem temporary contents generated on the fly, right? Is it really needed? just asking. Normally is better idea to keep temp data on memory and just drop to filesystem the final result (in this case the dox file I guess). I really would ask you to look at some modules including the Msf::Exploit::FILEFORMAT :-)