I am developing an application that will receive data through COM port and needs to paste it into a Microsoft Office program (for now, I'm working with Word, but it will support Excel, PowerPoint, ...).
I thought that a nice idea would be copying this data to clipboard, then paste it to whatever application window is open (so, for instance, LibreOffice apps and Notepad work perfectly). For this, I've coded:
if (booleanIncomingData)
{
booleanCopy = true;
}
else if (booleanOutcomingData)
{
copy = false;
}
if (copy)
{
Clipboard.SetText(myString);
}
else
{
SendKeys.Send("^V");
SendKeys.Send("{ENTER}");
}
As I said, LibreOffice programs and Notepad work perfectly, but Word and Excel aren't doing what I thought they should (basically display any data, then jump to the next line).
Also, a simple
Clipboard.GetText()ç
doesn't work (and my feeling is because it tries to paste the content into the active window.
Looking for Interop.Word solutions over the web, had no success so far. Does anyone have an idea what can I do or where I should look for? Instead of a regular Win32 application, should I implement an Office add-on?
In case any other information is necessary, please feel free to ask for it.
César.
It got much more complex than I thought. But my current application now works with several softwares (Word, Excel, Notepad, LibreOffice, ...).
COPY/PASTE DATA
stackoverflow.com/questions/3546016/how-to-copy-data-to-clipboard-in-c-sharp
msdn.microsoft.com/en-us/library/system.windows.forms.sendkeys.send(v=vs.110).aspx
GET THE ACTIVE WINDOW
www.csharphelp.com/2006/08/get-current-window-handle-and-caption-with-windows-api-in-c/
stackoverflow.com/questions/893669/determine-whether-program-is-the-active-window-in-net
stackoverflow.com/questions/2635404/how-to-get-process-name-and-title-of-the-top-window-on-windows-c-sharp
MICROSOFT OFFICE
hubpages.com/hub/How-To-Program-with-Excel-and-C-using-a-Ribbon
msdn.microsoft.com/en-us/library/vstudio/6b9478cs.aspx
stackoverflow.com/questions/13403504/send-text-to-excel-sheet-using-winapi
stackoverflow.com/questions/2378206/how-to-get-current-or-focussed-cell-value-in-excel-worksheet-using-c-sharp
All in all, Microsoft Office applications were more cumbersome, and as #Brian suggested, PIA (Primary Interop Assemblies) were crucial in this task.
Related
I have an Excel file with many different, but very similar, userforms.
When I click on a cell in my worksheet a specific userform will open according to the cell text.
I wanted to know if there is a way to add a C# form to Excel which will behave as a userform? Since the visual studio interface is much simpler and a lot more generic than VB.
I know you can create C# add-in's for excel, but these are used to add buttons, ribbons, etc.
what I need is a way to add a dll which will be activated when clicking on a cell, this dll must be easy to update, and it should not include installation (like the add-in's).
I got the following answer from a different forum:
You have some possibilities
1) Actually Excel (and practically all Office application) support Add-ins. Thus you could make a legacy excel add-in. There are several tutorials out there, but this is not the simplest one.
2) An other interesting approach exceldna.codeplex.com/[^]
3) You can call managed code from excel vba, but it is not the wisest one, since vba runs in a rather undeterministic way. Start here: richnewman.wordpress.com/2007/04/15/…^]
4) Interop Forms Toolkit[^] might be also an alternative – user2134909 2 mins ago edit
I haven't thoroughly examined them, but they seem promising
I've written a small application as a test to see what Excel VSTOs are capable of. However, I've run into a problem.
When I start Excel my clipboard is cleared. I've traced this down via Google to be a problem with Add-Ins (specifically COM add-ins) that when not initialized correctly will clear the clipboard automatically. Unfortunately for me I don't know how to preserve the clipboard in a VSTO and Google isn't much help. I don't call any functions that would obviously clear the clipboard. The only thing in the ThisAddin function is an if/else block that reads from some properties and sets some flags.
I've mulled over the idea of copying data on the clipboard to some object and then just replacing this but that seems very hacky and probably won't work.
Can anyone shed some light on how/why my VSTO clears the clipboard when Excel starts and how I can remedy it?
EDIT:
After tracing a little bit deeperI was wrong about it being my person test add-in. I figured out that it was either the Team Add-In or Adobe Contribute. Given Adobe's track record with these things I would go ahead and say it was probably the contribute add-in. Thanks everyone for your replies. This will teach me for using a non-vanilla installation of Excel for VSTO practice.
I will leave this topic open so that more people can contribute to possible reasons why a VSTO Excel Add-In (or any Add-In for the office suite) would clear the clipboard on start.
It would be best to identify the addin that's trashing the clipboard and removing it.
If that's not an option checkout some of the answers in Chris' link. I've done this using the Win32 API before. I think what's lacking in the listed answers is calls to OpenClipboard( http://msdn.microsoft.com/en-us/library/windows/desktop/ms649048%28v=vs.85%29.aspx) and CloseClipboard.
It's folly to try to backup/restore the clipboard, when complex data types are present. And you will generate unwanted clipboard events. See my prior answer here:
https://stackoverflow.com/a/2579846/289135
The COM addin Adobe PDF Maker, Bluetooth module clears the clipboard when Excel starts. Disable it to stop this behaviour.
When printing to PDF using C# + Excel Interop what is the best practice for detecting when the file has successfully printed or if there was a problem?
I need to know so that my app can take the generated PDF file, rename it, move it to a shared network drive, email it, etc. (Perhaps I should build a dedicated SSIS app to handle that part... hmmm... then I have an excuse to learn SSIS/BIDS)
Simply polling the directory until File.Exists() or we reach some maxTries cap is not a very satisfying way to handle it.
I am using VS 2008 Pro, Excel 2003 (11.0 Object Library), and Adobe Acrobat 7.0 Pro. Running on WinXP SP3.
I think the polling technique is fine. Its a common technique. I do agree that it'd be nice to have an AfterPrint(StatusCode) to match BeforePrint, but the Automation API doesn't provide for that, so poll away!!! It works for almost every process on linux, many windows services, and applications.
I've been writing a shared addin for Excel 2003. It adds a menu (CommandBarPopup) and a couple menu items off it (CommandBarButton) that when clicked open a WPF window to read a couple values and manipulate the worksheet.
Is there a way to write it so that it would work in office 2007/2010 as well, or is it impossible?
If so, can anyone provide instructions for how that is done?
MS-Office libraries are quite well back-compatible, so your addins should work in future version just as they are.
This really shouldn't be trouble at least for commandbarbuttons and popups. If you're about to do something harder, I strongly recommend Add-in Express (sorry for advertising, I am just a satisfied customer ;-) )
Pz, the TaskConnect developer
Fairly straightforward scenario, for which I can't seem to find an answer anywhere:
I am using a Microsoft WebBrowser
ActiveX control (AxSHDocVw.AxWebBrowser) in a Windows Forms
application.
The WebBrowser control
is used to display Excel workbooks in
the application.
I can obtain a
reference to the internal document as
a
Microsoft.Office.Interop.Excel.Workbook.
What I cannot seem to find, despite
hours of searching the Web, is any
reliable guidance on how to determine
the range of cells currently selected
by the end-user.
Here's the problem. Since the sheet is displayed in the browser, it doesn't have the Excel toolbar displayed, and I'd like to provide at least some rudimentary functionality (cut, copy, paste, undo, redo, bold, italic, underscore, left, right, center, and so on) from my application's main window. In order to do that, I need to be able to forward those commands onto the active sheet. Some of those commands require knowledge of the currently selected range if I want to properly execute them.
If anyone knows how to do this, I would be forever in your debt. (Well, maybe not forever, but you get the point.)
[NOTE: I use the old ActiveX control so that I can trap the NavigateComplete2 event to capture the reference to the internal document. The .NET WebBrowser control doesn't expose this functionality the same way.]
If you can get the workbook reference, you should be able to do .Application.Selection to get the current selected Range.
SpreadsheetGear for .NET includes an Excel compatible Windows Forms control which you might find easier to use in your .NET applications.
You can download a free trial here.
Disclaimer: I own SpreadsheetGear LLC
Check using the following code....
if (wkbk.Application.Selection != null)
{
if (wkbk.Application.Selection is Excel.Range)
{
//your code to obtain an Excel.range object goes here...
}
}