![]() |
|
|||||||||
| |
Related Information...
|
|||||||||
|
Training > Tutorials > Microsoft Word > Convert Field Links |
||||||||||
|
On this page... Jump toFull Code
|
Convert Field Links If you link images and then need to move them, thereby breaking the link, this macro will help you reset the links to the new location. This article contains WordBasic Code for Word 6 and 95. For VBA code, for versions 97 and above, download the VBA version of this code (inside a template) here: VBA version. You may still want to read this article, as the general explanation of the code still holds. Some Basics If you've ever linked files to a document, then moved the files to another location, you know the pain of having to locate and edit all those field link paths. Now you can use this macro to convert all the links in your doc with the ease of one search and replace command. The Problem You have a document. You've taken the time to save space by linking a ton of graphic files to the document, rather than embedding them into the file. But now your System Administrator tells you that you have to move your files from one network driver to another drive. Great! All your picture field links are trashed. Although a simple Search/Replace may work in some instances. Many users require a restricted search to only edit field codes so as not to change any document text. How can you easily set up a search to find only fields and then change just the text in all your fields? Write a Macro Does it seem like whenever you have a problem in Word, someone says "Oh, you can fix that by writing a macro!" Well, many times that's true. And this is one of those times. Where Do I Start? The best way to start is to take a little time and think out what you want to accomplish with your macro.
Creating the Dialog Box Click Tools/Macro. Type LinkConverter in the macro name input box and click Create. A macro edit window will open with the first and last lines added for you. You'll be adding code between the Sub MAIN and End Sub lines. You'll notice that a new toolbar opens when you're working in the macro edit window. The last icon looks like a dialog box. This is the button that takes you to the DialogBox Editor utility. This will allow you to literally draw out your own dialog box. Then you can copy the dialog box back into your macro. All the hidden placement code will be entered into your macro. You can practice drawing your own dialog box later. But to become familar with the process, take my code (just below) and follow the next instructions to drop it into the editor box to see my dialog box. 1. Click the Dialog Editor icon on the macro editing window's toolbar, then highlight and copy the Dialog Box code below and paste it into the Dialog Editor. The dialog box I created should appear in the Editor. Now you can double click any object in the dialog to change the wording if you want. However, be careful if you're not familiar with using the Editor. There are underlying dialog record names. If you change these names in this code, you'll have to also change them in the variable statements or you'll receive an error.
2. After you have made any changes, click Edit/SelectDialog. This copies the underlying code to the clipboard. Now you can switch back to the macro editing windows and paste the dialog box into the macro. Set Your Variables Now we need to set some variables to get things rolling and hold the information we're requesting. First we'll declare our UserDialog and set a variable so we can find out if OK or Cancel has been clicked and deal with those events.
We'll test to make sure the Cancel button isn't pressed. If it is, we dump out of the macro by jumping down to the bye label at the end of the macro. If not, the macro will just continue on its merry way.
Note! There are many restricted words in WordBasic that can't be used as names of variables because they also perform actions. But by getting into the habit of placing a v (as in Variable) before all your variable names, you can avoid the problem of accidentally using a restricted word, such as Print versus vPrint. In our dialog box, the names of the input boxes have the Identifiers of .txtFromPath and .txtToPath. These are the dialog records we will use to set variables, to make them easier to handle.
Now you must test the status of the field codes to see if they are on or off. By setting a variable to the results of the ViewFieldCodes() function, we can use an If...Then statement to decipher whether the codes are already on or not. That way we'll know that our Toggle has turned on the view we need. Remember, we need to search out the text, so we need to ensure the field code text is visible, not the resulting pictures.
Also, note how apostrophes are used to barricade off comments and make them easily visible. You can use a simple single apostrophe or the letters REM to comment out words that are not part of the actual macro code. Gettin' Loopy We'll set a Loop that will first set a variable to the results of a successful search. We'll be searching out field codes, which are represented by ^d. The Loop will continue hunting through the document for all fields. When it finds a field, it stops and searches (only within this now highlighted field text) for the Old Path statement. If it finds a winner, it replaces it with the New Path statement. This information was inserted into the dialog box by the user and it's currently sitting around in our vFromName$ and vToName$ variables waiting for something to do.
Now we need to select all the text and update the new fields, so we can view the links to ensure they've been properly switched.
You should take the time to view the pictures to make sure you're actually getting new pictures and not a giant X in a box, meaning that the picture doesn't exist at this link.
Then return the cursor (view) to the top of the page to leave the view at the beginning of the document. Plus it will let them know the macro has finished the process.
Oooops! Finally, let's make sure to set some traps for any errors. We've already set up an If...Then statement so the macro will only continue if the OK button is pressed and will exit if the Cancel button is clicked. But what if some unforeseen error should occur. You'll want to get the error number so you can look it up in your Word Developer's Kit. This will help you figure out and correct the problem. You can just write a straight message, but I prefer to build the message using a variable. And we'll toss in a beep just to irritate the user! <hee, hee> Also, notice how we jump over the error message by inserting a Goto done statement before the trap: label. This will make the macro skip the error message, unless it's specifically sent there by an error. The string function, Str$(), converts the returned error number (integer) and displays the number in text form so it can be displayed in the message. Then Err = 0 resets the Error to Nothing, so it can deal with any future errors.
And Don't Forget! You must go back to the top of the code and enter the statement that sends all errors down to this trap. Normally, that would be one of the first lines you'd write, but I wanted to explain it before you wrote it. So scroll back up to the top of your macro. Just after the Sub MAIN line, go down a line or two and add this line:
That's it! Feel free to mess around with this code and customize it the way you want. Hope this saves you time and headaches! Below you will find the complete code. Feel free to copy it into your own macro. However, remember that Word automatically adds the Sub MAIN and End Sub lines for you. So, if you copy all this code, be sure to remove these lines or they will be duplicated in your code and you'll receive a nesting error.
|
|||||||||