As the title says, I'm trying to create a "Save" button which will prompt the SaveFileDialogue upon the first click (the user names the file, etc.) and when the "Save" button is clicked again, the file is re-saved (updated) and the SFD isn't prompted again.
Here is my "Save" button click method:
private void btnSave_Click(object sender, EventArgs e)
{
if (SaveDoc.ShowDialog() == DialogResult.OK) //SaveDoc is a SaveFileDialogue placed on the designer.
{
CreateWordDocument(tFilename.Text, SaveDoc.FileName, pathImage);
tEnabled(false);
}
}
Here is the full code.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//Method: Find and Replace:
private void FindAndReplace(Microsoft.Office.Interop.Word.Application wordApp, object findText, object replaceWithText)
{
object matchCase = true;
object matchWholeWord = true;
object matchWildCards = false;
object matchSoundLike = false;
object nmatchAllForms = false;
object forward = true;
object format = false;
object matchKashida = false;
object matchDiactitics = false;
object matchAlefHamza = false;
object matchControl = false;
object read_only = false;
object visible = true;
object replace = 2;
object wrap = 1;
wordApp.Selection.Find.Execute(ref findText,
ref matchCase, ref matchWholeWord,
ref matchWildCards, ref matchSoundLike,
ref nmatchAllForms, ref forward,
ref wrap, ref format, ref replaceWithText,
ref replace, ref matchKashida,
ref matchDiactitics, ref matchAlefHamza,
ref matchControl);
}
string pathImage = null;
//Method: Create the document :
private void CreateWordDocument(object filename, object saveAs , object image)
{
object missing = Missing.Value;
string tempPath=null;
Word.Application wordApp = new Word.Application();
Word.Document aDoc = null;
if (File.Exists((string)filename))
{
DateTime today = DateTime.Now;
object readOnly = false; //default
object isVisible = false;
wordApp.Visible = false;
aDoc = wordApp.Documents.Open(ref filename, ref missing, ref readOnly,
ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing);
aDoc.Activate();
//Find and replace:
this.FindAndReplace(wordApp, "<name>", tFirstname.Text);
this.FindAndReplace(wordApp, "<Lastname>", tLastname.Text);
this.FindAndReplace(wordApp, "<tel>", tPhone.Text);
this.FindAndReplace(wordApp, "<Company>", tCompany.Text);
this.FindAndReplace(wordApp, "<Date>", DateTime.Now.ToShortDateString());
//insert the picture:
Image img = resizeImage(pathImage, new Size(200, 90));
tempPath = System.Windows.Forms.Application.StartupPath + "\\Images\\~Temp\\temp.jpg";
img.Save(tempPath);
Object oMissed = aDoc.Paragraphs[1].Range; //the position you want to insert
Object oLinkToFile = false; //default
Object oSaveWithDocument = true;//default
aDoc.InlineShapes.AddPicture(tempPath, ref oLinkToFile, ref oSaveWithDocument, ref oMissed);
}
else
{
MessageBox.Show("file dose not exist.");
return;
}
//Save as: filename
aDoc.SaveAs2(ref saveAs, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing);
//Close Document:
aDoc.Close(ref missing, ref missing, ref missing);
File.Delete(tempPath);
MessageBox.Show("File created.");
}
private void Form1_Load(object sender, EventArgs e)
{
}
//Method Enabled Controls:
private void tEnabled(bool state)
{
tCompany.Enabled = state;
tFirstname.Enabled = state;
tPhone.Enabled = state;
tLastname.Enabled = state;
btnLogo.Enabled = state;
}
//Load the Template Document:
private void button1_Click(object sender, EventArgs e)
{
if (LoadDoc.ShowDialog() == DialogResult.OK)
{
tFilename.Text = LoadDoc.FileName;
tEnabled(true);
}
}
//Load the logo picture :
private void btnLogo_Click(object sender, EventArgs e)
{
if (LoadPic.ShowDialog() == DialogResult.OK) //LoadPic is a OpenFileDialogue placed on the designer.
{
pathImage = LoadPic.FileName;
btnSubmit.Enabled = true;
}
}
//Create your document:
private void btnSave_Click(object sender, EventArgs e)
{
if (SaveDoc.ShowDialog() == DialogResult.OK) //SaveDoc is a SaveFileDialogue placed on the designer.
{
CreateWordDocument(tFilename.Text, SaveDoc.FileName, pathImage);
tEnabled(false);
}
}
}
Does anybody have any ideas on how to approach this? Thanks in advance.