Computing.Net > Forums > Office Software > macros in Word, Selected Text probl

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

macros in Word, Selected Text probl

Reply to Message Icon

Name: Nikos7
Date: November 13, 2007 at 23:20:01 Pacific
OS: Win XP
CPU/Ram: 1 Gig
Product: Compaq
Comment:

The first page of the document I am working on, in Word 2007, is basically a Table of Contents (not Office generated TOC, just written like any other page by myself -- this is NOT a TOC question).

So, there are hundreds of (let's just call them) paragraph numbers refered to in my Table of Contents, and these hundreds of paragraph numbers are all already bookmarked. I WOULD LIKE TO MAKE EACH ONE OF THESE INTO HYPERLINKS by means of a MACRO.

I have gotten along pretty well with writing a macro for this... simply using FIND, it finds my digit characters ("paragraph_^#^#^#" finds paragraph_234 ... etc, my paragraph numbers are named this way in the Toc).

Now that that selected the first of my paragraph numbers in my TOC, I click (as Macro is recording) hyperlink, and in the Address box type the number symbol "#" and THEN I PASTE (Control V).

HERE IS THE PROBLEM: This all works very well in instantly making my paragraphs hyperlinks, but the problem is, WHEN I RUN THE MACRO AGAIN, although it correctly makes hyperlinks, it keeps recycling the PASTED text I used when I ran the macro, thus: "paragraph_234" becomes a hyperlink, but then "paragraph_235" gets renamed to paragraph_234 and hyperlinks to 234 as well... and so on for hundreds and hundreds of paragraphs. THUS, the macro doesn't record to PASTE what was put on the clip board last, rather it records specifically the number that was pasted that first time, making my desired goal completely fruitless....

Should I throw in the towel... is this not possible, or is there a way to tell the macro to generically just insert text that was found last, not the specific number on the clipboard...?! THANKS SO MUCH FOR YOUR HELP!



Sponsored Link
Ads by Google

Response Number 1
Name: DerbyDad03
Date: November 14, 2007 at 05:34:28 Pacific
Reply:

If I may, there is no need to capitalize sections of your post. We can all hear you just fine.

As to your question, perhaps you need to clear the clipboard. Try incorporating this code into your macro. I didn't write it, but I did test it in Office 2003 and it seems to work in Word and Excel. Google is our friend.

Option Explicit
Public Declare Function OpenClipboard Lib "user32" (ByVal hwnd As Long) As Long
Public Declare Function EmptyClipboard Lib "user32" () As Long
Public Declare Function CloseClipboard Lib "user32" () As Long

Public Function ClearClipboard()
OpenClipboard (0&)
EmptyClipboard
CloseClipboard
End Function

Sub ccc()
Call ClearClipboard
End Sub


0

Response Number 2
Name: Nikos7
Date: November 15, 2007 at 20:34:22 Pacific
Reply:

Thank you to DerbyDad03 for the suggestion and work on this. Unfortunately I don't think this solves the problem. (However, as a FYI, you should know that I am a beginner on doing anything with Macros, which definitely holds true with editing the code of the Macros).

My problem with the code is not that something old on the clipboard gets posted, but rather that by selecting Control-C (copy) after Find found the desired text, the Macro does not record an instruction to COPY, but rather the specific text I copied when running the Macro. The following is the important part of the Code, with my comments in {{}}:

Selection.find.Execute
{{ Searching for: "parag_^#^#^#" }}

Selection.Copy
{{Generated by me hitting Control C after text was found (let's say "parag_2188") }}

ActiveDocument.Hyperlinks.Add
{{ I hit the enter Hyperlink button }}

Anchor:=Selection.Range, Address:="", _
SubAddress:="parag_2188", ScreenTip:="", TextToDisplay:="parag_2188"
{{I did "Control V" - Paste in the box}}

I could be missing it, but I don't think clearing the clip board is the problem ... for instance, in the code above, I could manually insert the code you found for clearing the clipboard, but we would still be left with ... <SubAddress:="parag_2188" ...>. It would be nice to be able to simply replace parag_2188 with some variable that equals the last text copied, like in Find and Replace, under Replace, there is the powerful option of "Find What Text" -- ^& -- which inserts the last text found. I inserted this (why not try?!) but to no avail.


I searched around Microsoft Visual Basic Help to find an answer... I found plenty of things that appear like they could be a solution ... essentially something having to do with "Select", i.e. Select.Paste ... but when I would insert these in the quotes of the code (see above) instead of what the Macro recorded ("parag_2188") none would work.

Perhaps there is a way to make "Select.Text" or something like that (containing the value of the last selection copied), to assign this to a Letter, Select.Text = K , and then enter K in quotes (and, this would have to be in the loop, K being reassigned each time)? I could see this being simply done, but its been 10 years since I've messed with Code, like swimming in the Pacific.

Thanks


0

Response Number 3
Name: DerbyDad03
Date: November 16, 2007 at 07:14:10 Pacific
Reply:

I'm still a little confused. (I'm also not an expert at Word macros, so take that into account)

If you recorded a macro to create a specific hyperlink, and ended up with this line in the code: Anchor:=Selection.Range, Address:="", _
SubAddress:="parag_2188", ScreenTip:="", TextToDisplay:="parag_2188"

then every time you run the macro, the hyperlink will have those exact properties regardless of what text is currently selected.

It would seem to me that the SubAddress:= and TextToDisplay:= should be variables, such as either

Anchor...SubAddress:= Selection

or perhaps

MyLink = Selection
Anchor...SubAddress:= MyLink

On the other hand...

If you single step through your code using F8, and check the Edit...Find dialog box after each .Find.Execute step, you'll probably see that the dialog box still contains the first thing that you found. Maybe clearing the Dialog box will help solve your problem.

See the tip and code at this site:

http://word.mvps.org/FAQs/MacrosVBA...



0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Office Software Forum Home


Sponsored links

Ads by Google


Results for: macros in Word, Selected Text probl

Vertical Text in Word www.computing.net/answers/office/vertical-text-in-word/5674.html

Simple Macros in Word www.computing.net/answers/office/simple-macros-in-word/2198.html

macros in word www.computing.net/answers/office/macros-in-word/9168.html