AHK: convert text selection to hyperlink with clipboard as target - vba

Using Autohotkey to achieve this behaviour:
copy URL from browser
select text in word doc
press keyboard shortcut
AHK converts selected text to hyperlink with clipboard as target.
This is what I have so far:
^b::
wd := ComObjActive("Word.Application")
url := %clipboard%
wd.ActiveDocument.Hyperlinks.Add(wd.Selection.Range, %url%,"","","")
return
I've tried many other variations, but alway get the error that the url variable contains illegal characters.
Any ideas? Thx!

Variable names in an expression are not enclosed in percent signs.
^b::
wd := ComObjActive("Word.Application")
url := clipboard
wd.ActiveDocument.Hyperlinks.Add(wd.Selection.Range, url,"","","")
return
or
^b::
wd := ComObjActive("Word.Application")
wd.ActiveDocument.Hyperlinks.Add(wd.Selection.Range, clipboard,"","","")
return

Related

How to replace a selected text in synedit?

I'm doing a custom text editor in Delphi with SynEdit and i'm at a loss with a simple need:
I want to have a popup button that, when clicked, replace the selected text with an uppercase version of that text. I think i have to use SelStart y SelEnd, but i'm not sure how can i do it.
I went into google and find mentions of a "La biblia de SynEdit" but the links were dead, so i ended here, hoping for a helpful soul who can answer my questions or has a copy of that bible.
Set SelStart to the beginning of the text, and SelLength to the length of that text (or, alternatatively, set SelEnd to the end of the text), and then assign your new text using SelText. This is the same way it works in the VCL.TRichEdit component.
SynEdit1.SelStart := 1;
SynEdit1.SelLength := Length(NewText);
SynEdit1.SelText := NewText;

Delete All Text After Bookmark

I am running a script on a word doc that generates a bunch of text. I have a button that I would like to be able to use to clear all of the text. How can I delete all text after a bookmark?
That's as simple as:
With ActiveDocument
.Range(.Bookmarks("MyBookmark").Range.End, .Range.End).Delete
End With

MS Word function to append a Range to a document

Suppose I want to add some text at the end of a document and immediately access
it as a Range object so I can set some properties of it without affecting the preceding text. Ideally, the Range.InsertAfter
method would return a Range object which would be perfect for this, but it doesn't.
It irks me that Word must know perfectly well what range defines the result of calling InsertAfter, but on the face of it, I need to calculate it "after the fact" from the length of the inserted text, or in some other way.
So I've devised a simple-minded work-around. In pseudo-code (actually it's Delphi
code but I hope that won't discourage VBA answers) what I do is this
ARange := Document.Range
ARange.Text := 'AAA'
AEnd := ARange.End - 1 // AEnd is an integer
ARange.SetRange(AEnd, AEnd)
ARange.Text := 'XXX'
ARange.Bold := True
and it seems that I can carry on indefinitely adding blocks of text to the end
of a document by repeating the second block of code.
The line
ARange.SetRange(AEnd, AEnd)
as I understand it, seems to construct a new Range at the end of the existing one (unlike calling Collapse on an existing range),
and works fine for the simple test cases I've tried. But it leaves me wondering
whether I'm missing a trick somewhere. Is there a more direct way to append a range
to a document and get a reference to it?
PS: I should have been a bit clearer that I'm trying to do this without using the Selection object (for a variety of reasons, including the fact that you can only have one of them at at time).
There are various ways to get the Range at the end of the document. You've discovered one, but as you say, it's somewhat circuitous. My preference:
Word.Range rngEndOfDoc = Document.Content;
//Content returns a Range object and is a property, not a method like Range()
rngEndOfDoc.Collapse(Word.WdCollapseDirection.wdCollapseEnd);
rngEndOfDoc.Text = "Text at the end of the document";
Collapsing the Range is conceptually like pressing the Right (or Left) arrow key when you have a selection. So rngEndOfDoc becomes a "point" rather than containing the entire content of the main body of the document.
FWIW I can never think of a situation when I'd use EndKey for this (emulate user actions) and I'd only change the Selection when I want to leave the user at the end of the document so that he can start typing at that location.
With thanks for the three admirable answers from others, I thought I would add my
own. The following are two versions of the function that I started out wishing Word provided
natively, returning an appended range.
The first version uses the MS Word objects in the MS Word Type Library import
unit that traditionally comes with Delphi (e.g. the Word2000.Pas one), and uses "early binding" automation,
while the second version does the same thing using late binding.
function AppendRange(InputRange : Range) : Range;
var
Direction : OleVariant;
begin
Result := InputRange;
Direction := wdCollapseEnd;
Result.Collapse(Direction);
end;
function AppendRangeLB(InputRange : OleVariant) : OleVariant;
begin
Result := InputRange;
Result.Collapse(wdCollapseEnd);
end;
Usage is e.g.
AppendedRange := AppendRange(ExistingRange);
AppendedRange.Text := 'YYY';
Think of the Document.Range as the union of all the possible ranges in the main document (i.e. apart from headings, footers, floating stuff etc.). It always starts before the first character and ends after the last character (that, in my experience, is always a hidden paragraph mark). It is not possible to define another range that is after the current Document.Range or, conceptually "append" anything after Document.Range, because there is no "after" the Document.Range (it always reaches to the end).
Thus, to extend a document with new text or other inline objects, you have to insert them at the end of the Document.Range - actually just before the end, as that last, hidden, paragraph mark must still be the final character of the document. This is just what you are doing in your pseudocode.
In VBA, an empty range at the end of the document can be referenced with
Document.Range(Document.Range.End-1, Document.Range.End-1)
Attempting to define it at Document.Range.End actually raises an error, as it points to after that last (hidden) character - and that is out of the Document - that is why we need the "-1".
After getting this range we may fill it with stuff - expanding it and, logically, the Document.Range. For example, to add text at the end of the active document, one would write
ActiveDocument.Range(ActiveDocument.Range.End-1, ActiveDocument.Range.End-1).Text = "New Text"
Notice that the same "final" empty range can be reached with:
Document.Bookmarks("\EndOfDoc").Range
The following works for me in Word 15 (Office 365) using the Word 2010 type library (I'm using early binding, but it should work the same with late binding as well). Word is a TWordApplication, and aRange is a WordRange.
Word.Selection.EndKey(wdStory, wdMove);
aRange := Word.Selection.Range;
aRange.Text := 'This is new text added at the end.';

Carriage Return converted into symbol ( Vertical Tab, male symbol, symbol for Mars ) by PowerPoint Text shape Object in VB.net

I have assigned string having carriage return to PowerPoint shape's texframe text but when I get from it. the carriage return is now Ascii 11 VT. I have searched google but I didn't find any solution exception to use linefeed but that does not work in other scenario.
Here is Code
_answerText = "Yes" + Chr(CharCode:=13) + "No"
powerPointShapeObject.TextFrame.TextRange.Text = _answerText
Could some one please help me to understand why it happen. Thanks in Advance
PowerPoint uses different characters for line endings depending on PPT version and on whether the text is part of a slide title or any other text on a slide.
This page on my PPTFAQ site explains in more detail:
Paragraph endings and line breaks
http://www.pptfaq.com/FAQ00992_Paragraph_endings_and_line_breaks.htm
Try this instead:
_answerText = "Yes" & "\r" & "No"
powerPointShapeObject.TextFrame.TextRange.Text = _answerText

Replace "Shift-Enter" line break with "Enter" in word document using Microsoft office API

I have a number of word documents that will be converted to HTML. It is required the paragraphs in the word documents should be converted to <p> elements.
After some tests with the Microsoft Office API's SaveAs method to convert the documents to the HTML, I realized the paragraphs with manual line breaks (break by "Shift-Enter") couldn't be placed in a separated <p> element, instead the paragraphs are grouped in a same <p> element.
In order to separate them, I have been trying to replace the "Shift-Enter" line breaks with the "Enter"/Carriage return before doing the conversion. However, I couldn't find a suitable way to do the line break replacement job. I have tried the WdLineEndingType parameter in the SaveAs method, but it seems not effective for the issue.
For those looking in MS Word: use Control-H (Find & replace).
Find Special character: manual Line break (^l, lowercase L)
Replace with: Paragraph mark (^p)
Replace All will do the whole document.
Edit: changed to lowercase characters.
The ms-word office API provides a find function in the Range object, enabling to search and replace the strings.
The following code is to find the manual line breaks("^l") with the carriage return("^p").
Range r = oDoc.Content;
r.WholeStory();
r.Find.Execute("^l", ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, "^p", WdReplace.wdReplaceAll);
Then use SaveAs to convert the word document to HTML, it will properly place each lines in <p> elements.
Paragraph mark ( Paragraph mark )
^p (doesn't work in the Find what box when the Use wildcards option is turned on), or ^13