0

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.

Kayla
  • 165
  • 2
  • 4
  • 13
  • Why do you want this? What do you want to achieve in common? – Roman Dibikhin Sep 22 '15 at 08:49
  • 1
    [1] When user saves file for 1st time, SFD will return filepath (with filename) that user chose. Store this path in some variable. [2] When save button is clicked again, check for this path variable, if it has value then skip SFD and do update logic. [3] Clear this path variable at appropriate location in code when user/that file session is completed. – Nikhil Vartak Sep 22 '15 at 09:40
  • @vnikhil Thanks for your reply. I've been looking at it. I have created the file path. `string FilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Name of File");` So I have that, and I think that will work, but how do I apply it? I've found an example, but it uses StreamWriter and I'm using Microsoft Word. I'm not sure how to do steps 2 and 3. If you have an example to refer to, that would be great. – Kayla Sep 23 '15 at 14:53

0 Answers0