1
votes

Background: I am creating application to capture the screen and paste the screenshot in the word document one below other.

Technical Part: When I run the application, a word document opens along with a form having 'Capture' button. The button when clicked captures the screen and an window is opened asking for the caption and then screenshot with the caption is pasted in currently open word doc.

My Question When user clicks Capture button, I want to check whether the document opened previously is still open or not. If document is not open, I will prompt user to open a new blank document.

I searched many forums and internet, but mostly all suggested to get the filename from the currently running processes. Note that my word document is unsaved, so it will have names like 'Document 1' etc. which will be a bad way to check.

I have pasted my code below for reference.

Any inputs will be appreciated. Thanks in advance.

WordProcessing.cs

namespace WordProcessing
{
    class MSWord
    {
        Word.Application wordApp = new Word.Application();   //Creates new Word Instance

        public MSWord()
        {
            wordApp.Visible = true;
            Word.Document oDoc = wordApp.Documents.Add(ref useDefaultValue, ref useDefaultValue, ref useDefaultValue, ref useDefaultValue);
        }
   }
}

Screencapture.cs

namespace Screencapture
{
    public partial class form_capture : Form
    {
        MSWord word = new MSWord();

        public void button1_Click(object sender, EventArgs e)
        {
           /* Here I want to check whether document opened by 'word' object is still open */

            ScreenCapture screen = new ScreenCapture();
            screen.CaptureScreenToFile("D:/image.png", System.Drawing.Imaging.ImageFormat.Png);

            //Form to ask for caption
            DataForm textarea = new DataForm();
            textarea.ShowDialog();
            textarea.Focus();

            word.InsertText(DataForm.textarea_text);
            word.InsertImage(@"D:\image.png");

        }
    }
}

PS: Ignore any syntax errors or missing function definitions. I have pasted shorter version for better understanding.

2
Please do not post incomplete, incorrect, or non-working code. Code examples are best - for better understanding - when they are minimal, complete, and verifiable.Enigmativity

2 Answers

0
votes

How about a try/catch on focusing the document?

try
{
    oDoc.Activate();
    //or on the application
    wordApp.Activate();
}
catch
{
  //Open a new document or ask
  Messagebox.Show("Please open a new Word Document");
}
0
votes

You could try using the File.Open() method. If the document is open, it should provide you with an exception which can print your custom prompt:

  FileStream stream = null;
    bool isOpen = false;
    try
    {
       stream = File.Open(@"DFilePath&Name",FileMode.Open, FileAccess.ReadWrite, FileShare.None);
    }
    catch(IOException)
    {
      isOpen = true;
      //Show your prompt here.
    }
    finally
    {
        if (stream != null)
            stream.Close();
    }
    if(!isOpen)    
      Process.Start(@"FilePath&Name");