hushaq
2 discussion posts
Hi,
My work requires me to collect various snippets of text into a single text file. I have been trying to write a macro, which opens a notepad and pastes the present clipboard text into that notepad. But if notepad is open, then I want it to append the clipboard text to the specified text file. So far I have succeeded in opening the notepad and pasting text in it for first time, but if the notepad is open, I am unable to get focus to it, which in turn does not let the text to be pasted in it.
My code is as follows:
using System;
using System.IO;
using System.Threading;
using System.Diagnostics;
using System.Runtime.InteropServices;
public static class ClipboardFusionHelper
{
public static string ProcessText(string text)
{
Process[] localByName = Process.GetProcessesByName("notepad");
int i = localByName.Length;
if(i==0)
{
System.Diagnostics.Process myProcess = new System.Diagnostics.Process();
myProcess.StartInfo.FileName = "notepad.exe";
myProcess.StartInfo.Arguments = "C:\\123.txt";
myProcess.Start();
myProcess.WaitForInputIdle(2000);
IntPtr hWnd = myProcess.MainWindowHandle;
MacroAPI.PasteText(text+" ");
return text;
}
else
{
System.Diagnostics.Process myproc = localByName[i-1];
myproc.Refresh();
myproc.WaitForInputIdle(2000);
IntPtr hWND = myproc.MainWindowHandle;
MacroAPI.PasteText(text+"next text");
return text+"snippet";
}
}
}
Any help or an alternate way to achieve my goal would be great.