My application gets the string from the clipboard:
IDataObject dataInClipboard = Clipboard.GetDataObject();
string stringInClipboard = (string)dataInClipboard.GetData(DataFormats.Text);
Then it manipulates the data and copies the result string to the clipboard:
Clipboard.SetText(result);
It's supposed to have a table from the Excel worksheet as the input clipboard and a string separated by [\t\r\n] as the output clipboard.
SetText() is supposed to clear the clipboard and I really have the required output clipboard. However when I manually paste it to the same Excel worksheet the previous (input) clipboard content is pasted.
Meanwhile if I previously close my application the correct output string is pasted. Moreover, if I previously finish the Excel process and then start it again the correct output is pasted as well.
Where is the bug?
I noticed that the problem disappears when the application window is closed. Moreover, I decided to change the program logic: if it is supposed to work only with the Excel why not to turn it into the add-in? So, thank you guys for the assistance, the problem is solved for now.
Related
So, the problem is there I already have the formatted text in RTF format but when I save the file it gets "wrong", the header disappears.
I'm using the RichTextBox Class to create the file, where finalText is the formatted RTF text, making the code look something like this:
System.Windows.Forms.RichTextBox RichTextBox = new System.Windows.Forms.RichTextBox();
string finalText = "{\rtf1\deff0{\fonttbl{\f0 Calibri;}{\f1 Arial;}{\f2 ..." //Formatted RTF here
RichTextBox.Rtf = finalText;
RichTextBox.SaveFile(_RtfPath_);
When I do this the final version of the generated file is almost complete, but the header disappears.
Editing the file in notepad++ I noticed that it removes the header and the information contained in it. After this I decided to manually create an RTF file. I opened the almost complete RTF file in notepad++, erased everything and pasted the finalText.
When I reopened this file the final version was the way I wanted it.
Is there any way I can save the text as RTF without having to use the RichTextBox Class to format the output file?
I have a button in wpf that when clicked does the following:
Clipboard.SetText("a\u0000b")
When I try and paste the contents of the clipboard into notepad all i get is:
a
How can I get the entire string?
If I render this string in a wpf control, i see a[square thing]b. In other words, the view control does not terminate at a null unicode character.
I tried to find if you can copy text with NUL in Windows, and it presumably is not possible. Maybe you could temporarily replace NUL with some other character, which should never appear in any text you process (although it probably will one day, as Murphy's law states), then open the text file and convert all occurrences of this character back to NUL?
var text = "a\u0000b";
var textToCopy = text.Replace("\u0000", "\u3f45");
Clipboard.SetText(textToCopy);
// Next paste the contents to the file and reverse the replacement there
It is a workaround, but if you're the one using those text files, it might be worth a try.
How to paste HTML ( tables ) code into Excel or PowerPoint?
I've overcome some issues concerning pasting HTML into Excel and PowerPoint and noticed that a lot of people are asking that.
I'd like to share my research, solution I made out for it.
Let's say we have a html file named html and we would like to access it in Excel, let's do following:
Clipboard.SetText(html);
We copy our html into the Clipboard. The clipboard generates from the html a real table or image/chart from the input file.
System.Threading.Thread.Sleep(2000);
Let's wait a second to have a preview
sheet.Range(cellmapp).PasteSpecial();
Now, we paste the content into a range that we could like to paste it, by defining cellmap.
System.Threading.Thread.Sleep(1000);
Let's wait a second to see the output
sheet.UsedRange.Copy(Missing.Value);
Now, in order to copy the table image into PowerPoint, we must work the with UsedRange.Copy, because it will copy the currently selected Excel area.
In order to check that we paste it into the correct Powerpoint slide
foreach (PowerPoint.Slide slide in presentation.Slides)
{
foreach (PowerPoint.Shape pptshape in slide.Shapes)
{
if(<your condition satisfies>)
{
slide.Select(); // some position in any slide
pptshape.Delete();//delete old content that was in that slide
ppApp.ActiveWindow.View.PasteSpecial(); //paste the Excel content
}
}
}
Of course there are other solutions, like making an image out of the html code and pasting that, which was my initial idea.
Another post refering that manipulation:
Showing HTML in PowerPoint
May be this question is asked in past but I have searched and not found its solution yet.
I have tried all the options that I have found till now but all in vain.
SendKeys doesn't work as it does not fill the file input box with file path, that is to be uploaded.
Cannot set file input box "SetAttribute" value as there is no value attribute available:
thats all.
If I use element.focus() it pops up "choose file to upload" dialog and now I don't know how to fill it programmatically and open it in file input box.
I want it to be automated completed so that user does not have to interact with the application.
Application shall pick the file from hard disk from given file path and fill other fields of form then start uploading, all using webbrowser control in windows form application.
No solutions found!
Can anyone help please? (This is my first ever question on stackoverflow, therefore if I am doing anything wrong then please guide, I mean if I am not allowed to post such question!)
Here is the code:
HtmlElementCollection heCollection = doc.GetElementsByTagName("input");
foreach (HtmlElement heSpan in heCollection)
{
string strType = heSpan.GetAttribute("type");
string strName = heSpan.GetAttribute("name");
if (strType.Equals("file") && strName.Equals("file"))
{
heSpan.Focus();
//heSpan.SetAttribute("value", "test.jpg");
SendKeys.Send("C:\\1.txt");
//heSpan.InnerText = "c:\\1.txt";
}
//Title for the attachment
if (strName.Equals("field_title"))
{
heSpan.InnerText = "1.txt";
}
}
When this code executes, cursor starts blinking in fine input box (as I have set heSpan.focus()) but the file path doesn't show in the file input box.
If I implement
heSpan.InvokeMember("click");
It opens the choose a file to upload dialoge/popup window and there I get stuck, because I don't know how to fill that popup dynamically and then insert the file path in file input box.
Try setting the focus to the WebBrowser control right before you set the focus to the input field.
That worked for me.
How can I load a file in a c# app so that I can see it formatted.
If I use:
richTextBox2.LoadFile("a.txt", RichTextBoxStreamType.PlainText);
I did it with:
textBox2.Text = File.ReadAllText("a.txt");
The text is not formatted at all, new lines and CR's are totally missing, also the tabs
also the file open ok in notepad, but it opens bad in wordpad
Can I load the file in a textbox? (use multi-line)
Have you tried loading it as RichText?
richTextBox2.LoadFile("a.rtf", RichTextBoxStreamType.RichText);