I'm using a webbrowser control. How can I move the insert position for an execCommand to the end of the word, that is currently selected?
Example:
| <- current caret position
Som|eword -> move -> Someword| -> execCommand executes after current word
What I want to do is insert a line without braking the word. What happens now is:
Somew|ord -> line
Somew
ord
What should happen is:
Somew|ord -> line
Someword
This is so hacky that I'm almost embarrassed to post it, but ... you can accomplish "Inserting a line without breaking the word" "using a webbrowser control" by doing something like
webBrowser1.Url =
new Uri("javascript:" +
"var tr=document.selection.createRange();" +
"tr.expand('word');" +
"tr.collapse(false);" +
// "tr.select();" // Necessary to actually move the caret
"tr.pasteHTML('<hr>');");
After the webbrowser has loaded the document that you want to manipulate, and the user has selected the text that they'd like to insert a line after. If you really need the caret moved as well, you'd need a tr.select() after the tr.collapse().
It doesn't use the execCommand though, so it may not be suitable for your purposes. Maybe someone else can figure out a way to make this a little cleaner...
Related
So my current project is comming to an end.
But I have an issue, i need to get a selection from any window and insert that into my current method, and then paste a new string into the selection.
This means that if i mark the following "This is a simple line", and i press my shortcut, i want "This is a simple line" to go into my method, and transform the text to "This line is more advanced", when i press my global hotkey.
Currently the method takes a string and returns a string (So the method works fine), i just need for it to copy the selection, do the method and then paste the new text, when i use my shortcut.
Any ideas?
I've solved it, the "fix" was just to simulate/emulate the "ctrt + c" "ctrl + v" shortcut in the project. I've gotten my inspiration from: This youtube video. Big thanks to "Matthew Watson" for the suggestion
Clipboard.SetText(textBox1.Text); //To copy your text to your clipboard
Clipboard.GetText(); //To get your text from your clipboard
Credit: https://stackoverflow.com/a/10140582/12345062
I have created a add-in for MS word. I have two buttons. Click on first move me forward by highlighting a range of words. On every second button click I want to go to the previous highlighted word. Can anybody help me in second button functionality. On button click one I have this code working fine.Now how to go the previously highlighted word range on every button2 click??
private void adxRibbonButton1_OnClick(object sender, IRibbonControl control, bool pressed)
{
object missing = System.Type.Missing;
Word.Document document = WordApp.ActiveDocument;
foreach(Word.Range docRange in document.Words)
{
if(docRange.HighlightColorIndex.Equals(Microsoft.Office.Interop.Word.WdColorIndex.wdRed))
{
docRange.HighlightColorIndex = Microsoft.Office.Interop.Word.WdColorIndex.wdBlue;
docRange.Font.ColorIndex = Microsoft.Office.Interop.Word.WdColorIndex.wdWhite;
break;
}
}
}
There's more than one way to approach this:
Use Word's built-in Find to search backwards in the document for the first instance of the changed highlighting.
Set two bookmarks, one for the current position of the code in the question and one for the previous position. The code sample below is for this variation.
string CurrentBkm = "_bkmCurrent";
string LastBkm= "_bkmLast";
if(docRange.HighlightColorIndex.Equals(Microsoft.Office.Interop.Word.WdColorIndex.wdRed))
{
docRange.HighlightColorIndex = Microsoft.Office.Interop.Word.WdColorIndex.wdBlue;
docRange.Font.ColorIndex = Microsoft.Office.Interop.Word.WdColorIndex.wdWhite;
if (document.Bookmarks.Exists(CurrentBkm))
{
document.Bookmarks.Add(LastBkm, document.Bookmarks[CurrentBkm].Range.Duplicate);
}
document.Bookmarks.Add(CurrentBkm, docRange);
break;
The code for button2 simply goes to the bookmark "_bkmLast":
string LastBkm= "_bkmLast";
document.Bookmarks[LastBkm].Range.Select();
Note that the bookmark name starts with an underscore _. This hides the bookmark in the Word UI so that it won't irritate the user in case the application settings show bookmark non-printing characters.
Note, also, that the code in the question could also work with Word's built-in Find functionality to search the formatting. This would almost certainly be more efficient than "walking" each word in the document and testing its highlight formatting. If you were to change your code to use Find the solution I provide with the bookmarks would still work.
I want to change the text of a Powerpoint shape only during presentation (i.e. while the slide is shown), but not change the text permanently so that it modifies the PPT file.
Currenty I'm subscribing to the event
Application.SlideShowNextSlide += OnNextSlide;
and then I change the text on one specific shape like this:
shape.TextFrame.TextRange.Text = "Hello world";
However, when the presentation ends, that text is in the PPT file and Powerpoint asks me whether I want to save the changes.
I want to avoid that my Powerpoint Add-In makes changes to the file.
UPDATE
Based on Steve's answer and your feedback in the comments. So here is an idea for you:
Why don't you just do the code that we mentioned, add Steve's line (it's actually a good way to avoid save questions), and since you'd have restore the original value at the end then just save through programmatic function in C# no matter what. So that even if the user has done any changes you would have saved file...no questions asked ;)
step by step:
1) run like normally
2) add Steve's remark
3) do the trick with the variable change I mentioned bellow
4) do save no matter what at the end of the presentation so that any user changes are saved with the document
code snippet for saving are in this answer very well documented
Remark: Remaining issues with this logic is only when the user does intentional changes and at the end does not want to save the work but this is a case not foreseen above. All the other users will not have to suffer through the "Want to save?" question :)
try to save previous state first and then before moving away or closing re-assign that old value back to it ;)
1st do
String oldValue = shape.TextFrame.TextRange.Text;
if you need to store outside the frame, use IO to store on file temporary or pass the variable on an outside function/variable value. Then do your code
shape.TextFrame.TextRange.Text = "Hello world";
and after you finish just do again
shape.TextFrame.TextRange.Text = oldValue;
or read the value from where you left it :)
hope it helps
In addition to the suggestion #oetoni made, you'll want to set the presentation's .Saved state to True after making each change or before allowing the user to quit the show/presentation.
That way, PowerPoint won't think any changes have been made, so won't offer to save the presentation when the user quits.
ActivePresentation.Saved = True
I have a url that goes like this index.aspx, but when I click on a button that is linked to a anchor tag it adds this #video
so it will look
index.aspx#video
is there away to remove #video from url?
I haven't gotten much feedback, so here are three options due to the ambiguity of your question.
Option 1 - Use the anchor location, remove the anchor, rescroll the window appropriately.
You can access the # value using location.hash as referenced by this article. Unfortunately as soon as you set the location.hash = '', the page re-navigates to the top of the screen (and leaves the # symbol in the URL). If you set the location.href, the page navigates away from the page losing any local variables stored.
One possible work around that you could do is something like the following:
function RemoveHash(){
var y = location.hash.substring(1, location.hash.length);
location.hash = '';
window.scrollTo(0, y);
}
The downside of this is that the screen will flash as it moves the screen back to the top and back down to your location. Another way you could do it is instead of using the anchors, find the location of the elements on the page, but this, in my experience, has been inaccurate and varies between browsers and becomes a maintenance issue. JQuery libraries may help this some though, may be worth looking into if you go down this route.
Option 2 - Get the Url without the anchor tag
You can do the replacement RegEx to remove the anchor from the url, you can use something like the following:
location.href.replace(/\#\w+/g, "");
Option 3 - Don't use anchors and use JavaScript to scroll
You could just not use anchors and just have the page scroll to the correct location using JavaScript by following this tutorial. The basic idea is getting the scroll offset of the element and scrolling the screen to that location.
function elmYPosition(eID) {
var elm = document.getElementById(eID);
var y = elm.offsetTop;
var node = elm;
while (node.offsetParent && node.offsetParent != document.body) {
node = node.offsetParent;
y += node.offsetTop;
} return y;
}
And use the hyperlink to call the javascript:
<a href="javascript:void(0);" onclick="window.scrollTo(0, elmYPosition('myAnchor'))>myAnchor</a>
I would like to know how can I get highlighted text from any window for example: (excel, ie, firefox,…).
please note that the following message not work in the above application
WM_GETTEXT,WM_COPY,EM_GETSELTEXT.
I have also tried control C (copy) and get selected text from clipboard but it is not a good idea.
Language used: C#
I haven't tried it myself, but the Microsoft UI Automation API should have the functionality that you need.
The UI Automation API is what you would use if you were building a screen reader to assist blind people. So it should definitely be able to access the selected text in an arbitrary application.
A good place to start would be with the "Text Pattern Overview" at http://msdn.microsoft.com/en-us/library/ms745158.aspx
Also keep your eye on question 517694. I think you'll find that answers to that question will solve your problem.
No answers huh? Well, I know you can get it from Excel, Word etc using interop. Look into that. It might give you som ideas on how to proceed with ie and ff. But basically the recieving application must have some sort of fascility for letting you do this and I don't think there's any general way which works all the time.
There is no general purpose answer to this question. Each window class will have a different solution.
For instance, if the hilighted text is in an edit window, then you can use EM_GETSEL to get the range of the selection, then WM_GETTEXT to get the text (and then throw the unselected part a way) or EM_LINEFROMCHAR to turn that range into line indexes, and then EM_GETLINE to get the selected text one line at a time.
But this won't work for any other window class.
No need to write this in C# from scratch. What's wrong with using the clipboard? This script ensures that it restores what was on the clipboard when it has finished.
Autohotkey makes this much simpler.
; Hotkey: Ctrl Shift t
^!t::
; Remember what was in the clipboard
clipboardPrev = %clipboard%
; Clear the clipboard
clipboard:=
Sleep,200
; Send a Ctrl C to copy the current selection
SendInput, {Ctrl down}c{Ctrl up}
Sleep,200
; Get the current selection from the clipboard
selectedText=%Clipboard%
if SelectedText =
{
; If the first attempt didn't get any test, try again
Sleep,200
; Send a Ctrl C to copy the current selection
SendInput, {Ctrl down}c{Ctrl up}
; Get the current selection from the clipboard
selectedText=%Clipboard%
}
; Restore the clipboard
clipboard=%clipboardPrev%
MsgBox, %selectedText%
return