Search code examples
c#textboxms-wordfilenamesoffice-interop

c# textbox-as-filename problems


I have been trying to make a program and it saves mechanics invoices. So I have got this far;

oWord.Application.ActiveDocument.SaveAs2("C:/BMW/Invoices/" + Regbox.Text + "/thing.doc");

which saves the word doc in a folder that is specified by the registration of the bike - this works fine. but what I really want is the date to be used as a filename...I couldn't figure that out, so I made a date label and plan on using the text from it as the filename instead (I know, its a long way round...but it works). Anyways, I have tried;

oWord.Application.ActiveDocument.SaveAs2("C:/BMW/Invoices/" + Regbox.Text + "/" + label19.Text + ".doc");

this was an "invalid filename"

oWord.Application.ActiveDocument.SaveAs2("C:/BMW/Invoices/" + Regbox.Text + "/label19.Text.doc");

this saved it as "label19.Text.doc"

oWord.Application.ActiveDocument.SaveAs2("C:/BMW/Invoices/" + Regbox.Text + "/" + label19.Text, ".doc");

This threw the error "(DISP_E_TYPEMISMATCH)"

All I need to do is get label19 text to work as a filename with a .doc extension...or another way of getting the date as a filename


Solution

  • If you need to use current date as file name then you can use:

    oWord.Application.ActiveDocument.SaveAs2("C:/BMW/Invoices/" + Regbox.Text + "/" + DateTime.Now.ToString("MM-dd-yyyy") + ".doc")