none
word document - save as problem (wordinterop) RRS feed

  • Question

  •  I'm Trying to write a program which will fill added bookmarks in the word file. The first problem  is  after I press the button I can't bring save as dialog window, it only appears if save directory is  C:/, The second problem is speed, Program takes some time to start up (For the first time) after first usage  it gets much better. See the Code below: 
    thank you very much.

       private void button92_Click(object sender, EventArgs e)
            {
                _Application word = new Microsoft.Office.Interop.Word.Application();
                Document doc = word.Documents.Open(@"C:\word.docx");
    
                //biujeti
    
                if (radioButton21.Checked)
                {
                    doc.Bookmarks["book1"].Select();
                    word.Selection.TypeText(radioButton21.Text);
                }
                if (radioButton22.Checked)
                {
                    doc.Bookmarks["book2"].Select();
                    word.Selection.TypeText(radioButton22.Text);
                }
                //saxazino kodi
                if (radioButton26.Checked)
                {
                    doc.Bookmarks["book3"].Select();
                    word.Selection.TypeText(radioButton26.Text);
                    doc.Bookmarks["book4"].Select();
                    word.Selection.TypeText(radioButton26.Text);
                    doc.Bookmarks["book5"].Select();
                    word.Selection.TypeText(radioButton26.Text);
                }
                if (radioButton27.Checked)
                {
                    doc.Bookmarks["book6"].Select();
                    word.Selection.TypeText(radioButton27.Text);
                    doc.Bookmarks["book7"].Select();
                    word.Selection.TypeText(radioButton27.Text);
                    doc.Bookmarks["book8"].Select();
                    word.Selection.TypeText(radioButton27.Text);
                }
                //shemsrulebeli
                if (radioButton23.Checked)
                {
                    doc.Bookmarks["book9"].Select();
                    word.Selection.TypeText(radioButton23.Text);
                }
                if (radioButton24.Checked)
                {
                    doc.Bookmarks["book10"].Select();
                    word.Selection.TypeText(radioButton24.Text);
                }
                if (radioButton25.Checked)
                {
                    doc.Bookmarks["book11"].Select();
                    word.Selection.TypeText(radioButton25.Text);
                }
                //direqtori
                if (radioButton19.Checked)
                {
                    doc.Bookmarks["book12"].Select();
                    word.Selection.TypeText(radioButton19.Text);
                }
                if (radioButton20.Checked)
                {
                    doc.Bookmarks["book13"].Select();
                    word.Selection.TypeText(radioButton20.Text);
                }
                //sabaziso
                doc.Bookmarks["book14"].Select();
                word.Selection.TypeText(maskedTextBox8.Text); //xelshekrulebis nomeri
                doc.Bookmarks["book15"].Select();
                word.Selection.TypeText(maskedTextBox7.Text); //spa
                //mimwodebeli
                doc.Bookmarks["book16"].Select();
                word.Selection.TypeText(textBox88.Text); //dasaxeleba
                doc.Bookmarks["book17"].Select();
                word.Selection.TypeText(textBox87.Text);//warmomadgeneli
                doc.Bookmarks["book18"].Select();
                word.Selection.TypeText(textBox86.Text);//iuridiuli misamarti
                doc.Bookmarks["book19"].Select();
                word.Selection.TypeText(maskedTextBox9.Text);//sk
                //shemsyidveli
                doc.Bookmarks["book20"].Select();
                word.Selection.TypeText(comboBox10.SelectedItem.ToString());
                doc.Bookmarks["book21"].Select();
                word.Selection.TypeText(comboBox9.SelectedItem.ToString());
                //rekvizitebi
                doc.Bookmarks["book22"].Select();
                word.Selection.TypeText(comboBox8.SelectedItem.ToString());
                doc.Bookmarks["book23"].Select();
                word.Selection.TypeText(comboBox7.SelectedItem.ToString());
                doc.Bookmarks["book24"].Select();
                word.Selection.TypeText(maskedTextBox6.Text);
    
            
                //Risi shesyidva
                doc.Bookmarks["book34"].Select();
                word.Selection.TypeText(textBox85.Text);
                doc.Bookmarks["book35"].Select();
                word.Selection.TypeText(textBox85.Text);
                doc.Bookmarks["book36"].Select();
                word.Selection.TypeText(textBox85.Text);
                //CPV
                doc.Bookmarks["book37"].Select();
                word.Selection.TypeText(comboBox11.SelectedItem.ToString());
                doc.Bookmarks["book38"].Select();
                word.Selection.TypeText(comboBox11.SelectedItem.ToString());
                doc.Bookmarks["book39"].Select();
                word.Selection.TypeText(comboBox11.SelectedItem.ToString());
    
                //tarigi 
                doc.Bookmarks["book40"].Select();
                word.Selection.TypeText(dateTimePicker3.Value.ToString());
    
                ///tanxa da sityvierad
                doc.Bookmarks["book41"].Select();
                word.Selection.TypeText(maskedTextBox10.Text);
                doc.Bookmarks["book42"].Select();
                word.Selection.TypeText(textBox89.Text);
                //checkbox 
                if (checkBox18.Checked == true)
                {
                    doc.Bookmarks["book25"].Select();
                    word.Selection.TypeText(checkBox18.Text); // s
                }
                if (checkBox17.Checked == true)
                {
                    doc.Bookmarks["book26"].Select();
                    word.Selection.TypeText(checkBox17.Text); 
                }
                if (checkBox16.Checked == true)
                {
                    doc.Bookmarks["book27"].Select();
                    word.Selection.TypeText(checkBox16.Text);
                }
                if (checkBox15.Checked == true)
                {
                    doc.Bookmarks["book28"].Select();
                    word.Selection.TypeText(checkBox15.Text);
                }
                if (checkBox14.Checked == true)
                {
                    doc.Bookmarks["book29"].Select();
                    word.Selection.TypeText(checkBox14.Text);
                }
                if (checkBox13.Checked == true)
                {
                    doc.Bookmarks["book30"].Select();
                    word.Selection.TypeText(checkBox13.Text);
                }
                if (checkBox12.Checked == true)
                {
                    doc.Bookmarks["book31"].Select();
                    word.Selection.TypeText(checkBox12.Text);
                }
                if (checkBox11.Checked == true)
                {
                    doc.Bookmarks["book32"].Select();
                    word.Selection.TypeText(checkBox11.Text);
                }
                if (checkBox10.Checked == true)
                {
                    doc.Bookmarks["book33"].Select();
                    word.Selection.TypeText(checkBox10.Text);
                }
    
                ((_Application)word).Quit(WdSaveOptions.wdSaveChanges, WdOriginalFormat.wdOriginalDocumentFormat);
            }

                                                                         
    Sunday, March 19, 2017 8:21 AM

Answers

  • Hello,

    >>after I press the button I can't bring save as dialog window

    You could save file using a SaveFileDialog, please visit How to: Save Files Using the SaveFileDialog Component.

    E.g.

       SaveFileDialog saveFileDialog1 = new SaveFileDialog();
                saveFileDialog1.Filter = "Word Documents|*.docx|All Files|*.*";
                saveFileDialog1.Title = "Save the File";
                saveFileDialog1.ShowDialog();
                if (saveFileDialog1.FileName != "")
                {
                  string  saveFileName = saveFileDialog1.FileName;
                  doc.SaveAs2(saveFileName);
                }

    You could also use a built-in dialog, please visit How to: Programmatically Use Built-In Dialog Boxes in Word

    E.g.

    using Word = Microsoft.Office.Interop.Word;
    
      Word._Application word = new Microsoft.Office.Interop.Word.Application();
                Word.Document doc = word.Documents.Open(@"D:\test.docx");
    
                dynamic dialog = word.Application.Dialogs[Word.WdWordDialog.wdDialogFileSaveAs];
                dialog.Name = "Testing";
                dialog.Show();

    Regards,

    Celeste


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by kotechkh Thursday, March 23, 2017 12:10 PM
    Tuesday, March 21, 2017 4:49 AM
    Moderator

All replies

  • Hi kotechkh,

    Thank you for posting here.

    According to your question is more related to Word, I will move it to Word for Developers forum for suitable support.

    The Visual C# discuss and ask the C# programming language, IDE, libraries, samples and tools.

    If you have some grammar or code errors, please feel free to contact us. We will try our best to give you a solution.

    Thanks for your understanding and cooperation.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, March 20, 2017 6:49 AM
  • Hello,

    >>after I press the button I can't bring save as dialog window

    You could save file using a SaveFileDialog, please visit How to: Save Files Using the SaveFileDialog Component.

    E.g.

       SaveFileDialog saveFileDialog1 = new SaveFileDialog();
                saveFileDialog1.Filter = "Word Documents|*.docx|All Files|*.*";
                saveFileDialog1.Title = "Save the File";
                saveFileDialog1.ShowDialog();
                if (saveFileDialog1.FileName != "")
                {
                  string  saveFileName = saveFileDialog1.FileName;
                  doc.SaveAs2(saveFileName);
                }

    You could also use a built-in dialog, please visit How to: Programmatically Use Built-In Dialog Boxes in Word

    E.g.

    using Word = Microsoft.Office.Interop.Word;
    
      Word._Application word = new Microsoft.Office.Interop.Word.Application();
                Word.Document doc = word.Documents.Open(@"D:\test.docx");
    
                dynamic dialog = word.Application.Dialogs[Word.WdWordDialog.wdDialogFileSaveAs];
                dialog.Name = "Testing";
                dialog.Show();

    Regards,

    Celeste


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by kotechkh Thursday, March 23, 2017 12:10 PM
    Tuesday, March 21, 2017 4:49 AM
    Moderator
  • Thank you very much  it worked. 
    Thursday, March 23, 2017 12:10 PM
  • thank you. 
    Thursday, March 23, 2017 12:10 PM