Systematically Hyperlinking text in Word VBA - vba

I work for a company that does custom reports in Microsoft Word very frequently. I would like to write a VBA Macro that systematically goes through the following list and Hyperlinks ONLY the text (not paragraph symbols or tab arrows or list number) and links to a bookmark in the current document.
1 → Description of Figure 1 ¶
¶
2 → Description of Figure 2 ¶
¶
3 → Description of Figure 3 ¶
¶
4 → Description of Figure 5 ¶
¶
After the macro is complete, clicking on "Description of Figure 1" will go to bookmark Figure_01 which exists later in the same document. I appreciate any help that anyone can give!

We can't just go ahead and write it for you, that is not the nature of the site, however, I don't want to tell you to go off and come back with some more effort and we'll support that.
I want to provide some pointers for you to get you going. Once you have got so far but are getting stuck on specific issues (e.g. 'why do I get error X' or 'why is my loop not capturing everything'), that's the time this site will shine for you and give great support!
So where to start? I'm going to assume non VBA experience and the below a crash course.
First we need a procedure that we can run. In Word press Alt+F11, this opens the VBE (VBA-Editor)
From the project window on the top left side (usually) double click on 'ThisDocument' and the main window will become a space for us to write code. The first first thing to write at the very top is Option Explicit, this is telling the VBA runtime that all variables must be declared, which is good practice.
Next below that enter: -
Public Sub CreateLinks()
End Sub
You have now created a procedure called CreateLinks, while your insertion point (vertical blinking line) is between these two lines of code you can push F8 to step through code line by line, or F5 to run it all in one go.
That's the start of everything. Now what you need to do is.
Connect to the document
Find the text to link to
Find the text to link from
Create link
Repeat steps 2 - 5 until all links are done.
That should bring you to your answer.
As further pointers/hints to help you: -
The Application holds a collection of documents, you could look through them or open your document to connect to it
a Document has a collection of bookmarks and hyperlinks, you can loop through these while getting to your goal
Selection is that active selected text
I hope this is of help.

Related

CorelDRAW VBA Text Manipulation

I want to write a VBA script for CorelDRAW to assign an Object Style to various text strings within a Paragraph Frame.
Something like (psuedo code example):
- move cursor forward to next Start of Line
- select text from cursor to next Tab character
- set selected text to Object Style 1
I have not been able to find any examples or doco of using scripting to do text manipulation in CorelDRAW. I'd be happy to do it using Javascript (available in CDGS 2019) if that is easier.
The way I see this script working is that I would manually position the text cursor at the start of the paragraph frame, hit a shortcut key to invoke the script, and then repeat until all desired changes are made. Possibly add looping at a later step.
Can somebody point me in the right direction please.

Where do I insert this code to create the macro in CorelDraw X7?

I'm referring to this post here:CorelDraw X6 Macro to insert Date using DTPicker Control
I am creating a template for everyday use for saving artwork proofs to send onto customers for visual approval. I'd like the following boxes to auto populate the following information:
1. A box that will populate today's date
2. A box that will populate the work order number based on the first 6 digits of the filename
3. A box that will populate the filename as a whole
The rectangles/boxes will already be in the document that gets imported. I just want to create a macro, or multiple...to click in order to fill these boxes before exporting to a PDF for customers approval.
I'm a total coding n00b and can't find specific directions on where to copy the code listed in the link above.
In the Corel Draw menu, go to "Tools" then "Macros" then "Macro editor".
After that, you can paste it in the right hand side of the screen, on the white empty box.
That should do it.

Word VBA - Load part of doc into variable, and run .indexOf to search

Okay, I am a Javascript programmer and VBA is driving me insane - I know nothing about it and it is like pulling teeth to find simple documentation on the simplest thing.
I'm literally trying to run a little script to auto-format a document, partly based in content.
I want to grab the third line of document, or first 100 characters, I really don't care, and run the equivalent of String().indexOf('foobar') on it, to check if that part of the document contains a string.
I cannot for the life of me find how to:
a. Load text selection into a variable.
b. Run a sane semblance to indexOf.
Can someone please help? And maybe point me to a sane VBA documentation that is not Micrsoft?
a. Load text selection into a variable.
Identify a range (Word.Range) you want and get the Text property of that range.
For instance,
dim s as string
s = ThisDocument.Paragraphs(3).Range.Text
b. Run a sane semblance to indexOf.
Is InStr no good?
msgbox InStr(s, "foobar")

Using VBA in MS Word 2007 to define page elements?

I'd like to be able to create a page element which I can feed text and it will form itself into the preferred layout. For instance:
{MACRO DocumentIntro("Introduction to Business Studies", "FP015", "Teachers' Guide")}
with that as a field, the output should be a line, the first two strings a certain size and font, centred, another line and then the third string fonted, sized and centred.
I know that's sort of TeX-like and perhaps beyond the scope of VBA, but if anyone's got any idea how it might be possible, please tell!
EDIT:
Ok, if I put the required information into Keyword, as part of the document properties, with some kind of unique separator, then that gets that info in, and the info will be unique to each document. Next one puts a bookmark where the stuff is going to be displayed. Then one creates an AutoOpen macro that goes to that bookmark, pulls the relevants out of the keywords, and forms the text appropriately into the bookmark's .Selection.
Is that feasible?
You're certainly on the right track here for a coding solution. However, there is a simpler way with no code - this is the type of scenario that Content Controls in Word 2007 were built for and with Fields/Properties, you can bind to content controls (CC). These CC can hold styles (like centered, bold, etc.). No VBA required.
The very easiest thing to do is to pick 3 built-in document properties that you will always want these to be. For example, "Title" could be your first string, "Subject" your second string and "Keywords" your third. Then, just go to the Insert ribbon, Quick Parts, Document Properties and insert, place and format those how you like. Then go to Word's start button (the orb thingy) and then under Prepare choose Properties. Here you can type, for example "Introduction to Business Studies", into the Title box and then just deselect it somehow (like click in another box). The Content Control for Title will be filled in automatically with your text.
If you want to use this for multiple files, just create this file as a .dotx (after CC insertion/placement/formatting and before updating the Document Properties' text). Then every time all you'll have to do is set these three properties with each new file.
Well, yes, it did turn out to be feasible.
Sub autoopen()
Dim sKeywords As String
sKeywords = ActiveDocument.BuiltInDocumentProperties(4)
ActiveDocument.Bookmarks("foo").Select
Selection.Text = sKeywords
End Sub
Okay, I have some filling out to do, but at least the guts of it are there.

Formatting text from Mulitline text box in word with VBA

I'm putting together a template in Word, using a form for the user to fill in to then populate some of the document.
The bit I'm currently stuck on is at the end of the document, where the cc's are listed.
The form has a multiline text box into which the user puts in their cc's, one per line.
I then want to add to the end of the document the contents of the text box, but in the right format. Specifically, it should look like:
cc: First CC contact
Second CC contact
so on and so forth
I attempted to do this using 2 bookmarks, so my code currently is:
' If 'CC' box has content, add it
If doc_CC.TextLength > 0 Then
.Bookmarks("CC").Range.Text = vbCr + "cc:"
.Bookmarks("CCs").Range.Paragraphs.Indent
.Bookmarks("CCs").Range.Text = doc_CC + vbCr
End If
However, when this is run, on the page it looks like:
cc: first contact
second contact
and so on
Realise that the 2 bookmark method is a bit messy but it seemed like a good idea at the time - obviously this is not the case! Have done some searching for a way to do it with Split but am not making much progress down this path - suspect I'm googling for the wrong thing.
How do I do this so that the formatting is as desired? Any help is greatly appreciated.
Try inserting a tab character? + Chr(9) or even + vbTab may work.
Have found a work around which, while doesn't answer the actual question of how to do it, does produce a result to the same effect.
Have used a 2 column table without no lines instead with contents of a1 being "cc:" and contents of a2 being whatever was entered into the multiline text box. If there is nothing in the text box, then the table is deleted.
I'll keep on eye on this question though so if some one does have the proper answer I can mark it accordingly.
Another possibility would be to format the cc paragraph with a hanging indent (like is used for bullets or numbering). Use a newline character - Chr(11) - instead of vbcr to separate each entry. The text should all line up,then...