.NET & Excel Interop: Copying User Selection to Clipboard - c#

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...
}
}

Related

How to Access the Undo Stack in Excel from C# via Office Interop?

I'm building a C# app that needs to interact with an Excel spreadsheet via Office Interop. I'd like to gain access to the full Undo stack so I can make a series of changes to the worksheet and then roll them back one at a time if necessary. I see that the Word Document object in the Interop assemblies has UndoRecord and some other niceties, including an Undo function. For example, I can call Microsoft.Office.Interop.Word._Document.Undo() with a parameter to undo multiple actions. Excel's Undo stuff, on the other hand, looks much more limited. Sure, there's Microsoft.Office.Interop.Excel._Application.Undo(), but it goes only one action deep, and if called again performs a "Redo" instead of going deeper down the stack.
I've found code online where VBA developers coded their own Undo histories for Excel because that was the only way to get the desired functionality. I don't see anything like this for C#, though. Is there any way to mimic the way Word's Interop undo works in Excel?
Alas, what's true for Word isn't for Excel. Probably because so many more people write Word VBA :). See this John Walkenbach page for what is possible in Excel VBA (and interop). Here's part of his description:
Making the effects of your subroutines undoable isn't automatic. Your
subroutine will need to store the previous state so it can be restored
if the user choose the Edit Undo command. How you do this will vary,
depending on what the subroutine does. In extreme cases, you might
need to save an entire worksheet. If your subroutine modifies a range,
for example, you need only save the contents of that range.
EDIT:
There is the Application.Undo method, but it's very limited. Here's the description from Excel 2010 VBA help:
Application.Undo Method
Cancels the last user-interface action.
Remarks This method undoes only the last action taken by the user
before running the macro, and it must be the first line in the macro.
It cannot be used to undo Visual Basic commands.
In regards specifically to Interop, I'm sure there's nothing available that's not in VBA itself.

Paste text to an active Word window using c#

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.

Integrating C# code in Excel

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

How to get copied text from memory without pasting?

I got one requirement which is when the user copy any text the system should get the copied text from the memory into the program without require the user to paste it in a txtbox or similar control. I searched on the internet but I didn't get any information. can somebody suggest or provide some references so that I can follow...????
any help would be highly appreciated...!!!!
From a web development standpoint, you can not access the clipboard directly. You will have to create either a Flash or Silverlight hook into the clipboard to get the data.
Example
Another Example
For security reasons, you will never be able to do that in Javascript.
As it is already pointed out accessing clipboard is either not possible or restricted for security reasons for all components running on a page in a browser (restricted == is unlikely to be enabled by anyone, especially for such "spy on clipboard" purpose).
For standalone application you can either scan clipboard all the time or use clipboard filters.
Native functions are around SetClipboardViewer and GetClipboardData.
Managed: Clipboard.

Drag and Drop a Groupwise Email to C# Winform

I have a need to be able to drag and drop a groupwise email to my winform application and am not having any luck at all.
I am able to get a filename as such but it is only a name, not a path.
Every time I call
e.Data.GetData("FileContents", true))
or
e.Data.GetData("FileContents", false))
or
e.Data.GetData("FileContents"))
I get null returned.
Also, the GetFormats returns something strange and a simple Google search returned nothing. One of the formats was WPOF_DBOBJ_DRN.
If anyone has some insight on this it would be greatly appreciated.
Update:
Just to add a few more details about this, there is also nothing on the clipboard from the drag and drop. Also took a look at the groupwise editor and word perfect isn't even installed on my pc and is most definatly not the editor.
Have a look at the following links (I know, some of them are for Delphi, but you should get the idea):
Drag and drop from an email file attachment in GroupWise to a .NET application
http://www.experts-exchange.com/Programming/Languages/Pascal/Delphi/Q_21198933.html
http://www.experts-exchange.com/Programming/Languages/Pascal/Delphi/Q_23015275.html
I'll take a look at trying that. I've also tried to implement the IStorage interface into an IDataObject interface using unmanaged code and still aren't having any luck. (The Interface is also used to get Outlook messages that are dragged and dropped and works fine.)
That format is most definatly one of the formats i get back from calling e.Data.GetFormats();
I'll take a look at that link. Thanks
WPOF_DBOBJ_DRN looks like WordPerfect Office Format - Database Object. I can't figure out what the DRN stands for though. See if you can open the unreadable file with Word, or WordPerfect if you have it. My guess is that the setup of GroupWise that you are using has WordPerfect as its editor.

Categories

Resources