Search code examples
emailgoogle-apps-scriptms-wordattachment

How to attach a google doc file as Microsoft Word doc


My first time here in Stack overflow. I'm just an amateur programmer trying to learn Google Apps script. I created a google form to get answers from users. I have transferred the answers to a google doc formatted according to our use in the office. I need to email the google doc back to the for user in microsoft word format. Sending as pdf is not a problem, but msword is a bit tricky.

I tried copying the recommendations i found here but I can't seem to get it to run. I hope someone can tell me what i'm doing wrong and what i need to doe. here's the relevant portion of the code:

`doc.saveAndClose(); // end of the transfer from google sheet to google doc. now to email...

  var filename= ts+"-"+c_name1;

  var compfile = DriveApp.getFilesByName(filename);

  if (!compfile.hasNext()) 
  {
    console.error("Could not open file "+filename);
    return;
  }

  var template = HtmlService.createTemplateFromFile('email_message');

  var message = template.evaluate().getContent();


   MailApp.sendEmail({
    to:em, 
    subject: "Automated Complaint Assistant", 
    htmlBody: message,
    attachments: compfile.next()})  // attachment is pdf file type. HOW TO SEND AS WORD? 

`

I tried this function i found here:

`function EmailCompAsWordDoc(email, name, newCopyID) {

  var subject = "Word version of Complaint"; 
  var body = "\n\nHere is the Microsoft Word version of the draft Complaint";  
  var url = 'https://docs.google.com/feeds/download/documents/export/Export?id=' + newCopyID +         '&exportFormat=docx';
  var options = {
      headers: {
      Authorization: "Bearer " + ScriptApp.getOAuthToken()
      },
      muteHttpExceptions: true
      }
  var response = UrlFetchApp.fetch(url, options);
  var doc = response.getBlob();

  //Create the docx file in my TEMP folder in Google Drive and send
  var file = DriveApp.createFile(doc).setName('Draft Complaint '+ name + '.docx');
  DriveApp.getFolderById('<ID of my TEMP folder in Google Drive>').addFile(file);   
  var blob = DriveApp.getFileById(file.getId());

  if (MailApp.getRemainingDailyQuota() > 0) 
    GmailApp.sendEmail(email, subject, body, {
      Body: body,
      attachments:[blob]     
    });  

  //Remove the docx file from the TEMP folder
  DriveApp.getFileById(file.getId()).setTrashed(true);
}


var compid = DriveApp.getFileById(filename.getId()) // error here. How to get the file ID for use by this custom function??

EmailCompAsWordDoc(em, c_name1, compid)

`


Solution

  • From your provided script, I understood that the value of filename is the string value of var filename = ts + "-" + c_name1;. I thought that this is the reason for your current issue of filename.getId is not a function.

    In this case, how about the following modification?

    From:

    var compid = DriveApp.getFileById(filename.getId())
    

    To:

    var compid = DriveApp.getFilesByName(filename).next().getId();
    
    • In this case, it supposes that the values of filename and em and c_name1 of EmailCompAsWordDoc(em, c_name1, compid) are valid values. Please be careful about this.