How to extract specific URLs from a large Word document - vba

I have a 621 page document with about 100 URLs I'm trying to compile together. They all begin with "https://open." Can you help me extract these URLs into a single list? I tried to do this manually and it hasn't been much fun. I am still a newbie to visual basic, but I have a better handle on it than any other language. Thanks!

The task is to find each occurrence of "https://open", extend the found range to the end of the address, write it somewhere, and go to the next. The fastest way of doing this would be to use Word's Find function (on the ribbon's Home tab). Click Find and all occurrences will be highlighted. Click the first one, extend the selection, copy, switch to another document, paste, return to the source document, move to the next highlighted URL and repeat.
If you want VBA, start by googling something like "find all occurrences". You will find code for that. Then look for the "Selection object". You will find code to extend the selection of the found items. Next, learn how to open a new, blank document. Google for "Documents.Add". After that all you have to do is to switch back and forth between your two open documents, copy from one and paste to the other.

Related

VBA to show/hide data in Word

I am writing a huge procedural manual and would love to be able to hide/show sections as needed. I found the correct way to do it, through bookmarks, check boxes and VBA but am unable to get all three to come together.
These are the instructions I am using:
Link to Instruction Sets
They seem simple enough but I am unable to "view code" by right clicking on the checkbox. Is this a necessary step to add the code or can I just open a new macro and insert the code there?
Other ideas to achieve hiding/showing sections with bookmarks are welcome, too. Thank you!

How to create a new custom style with specific heading

Please help as I am new to Vba and custom style in MS Word 2013.
I want to create a new custom style that provides heading as INTRODUCTION when a paragraph is been selected, and the rest of the text should come in normal style of paragraphs. Is this possible to make it in custom style or should Vba macro be used. If Vba macro is the only option then how can we add this into the particular custom file.
Styles are for formatting only, they don't contain text (such as INTRODUCTION). If I understand your request, you will need a simple macro you can write, or, actually, record.
So, let's say you have a paragraph in your document. You would like to make this paragraph have the "Normal" style. You would like to insert BEFORE this paragraph the word "INTRODUCTION", and give it the style "Heading 1".
Record the Word macro: InsertIntroduction
Position the cursor at the start of the paragraph.
Enable "Record a macro" - varies by release of Word, but, for 2010, View > Macro > Record. Be sure to specify a good name and a carefully thought out Description. Do NOT shirk this step! Put in the Description! If you are going to use it a lot, assign a keystroke to it also, that will save you a lot of time.
Type INTRODUCTION. Using cursor keys (not mouse), select it (perhaps up-arrow, Home, Shift-End). Apply style "Heading 1" (for this, you can use the mouse: Home > Style group > Heading 1 or a keystroke, usually Ctrl-Alt-1).
Arrow down to the next paragraph. Select the full paragraph using the keyboard, not the mouse (perhaps by hitting the F8 key four times, then ESC).
Apply the appropriate style (again, you can use the mouse, but the Normal style is usually linked to Ctrl-Shift-N).
You've completed the steps you wanted, based on your original request. Note that you can add a lot more, of course. But, let's say you're done.
View > Macro > Stop recording.
You've now written a program and it will be saved. Use it going forward. Cool, hunh? And you can write these things a lot faster yourself than asking others "how do I...?"
Like any other program, it can be modified to do more, or fix little errors. Let's say you misspelled INTRODuCTIoN. Tap Alt-F11 to see your program. Adjust.
9 steps seems like a lot, but, believe me, it's easier done than said. Give it a try. Do it carefully. In theory, if you make a mistake, you can simply modify the program to fix it. But, at your level of experience, you may find it easier to simply Stop recording and start over.
To Use Macro
Position cursor at start of relevant paragraph.
Execute macro. If you took my advice and assigned it to a keystroke, just tap the keystroke. If not, tap Alt-F8 to see a list of your macros. Select the one you want, and click Run.

How to programmably select elements of a Word document and copy to Excel

Is there a way to get some sort of collection of "paragraphs, images, headings1, headings2" and other "word elements" with a VBA function call. I was looking into this, I ran accros ActiveDocument.fields, but it doesn't seem to be it.
What I need to do is copy these elements, one by one, into an excel document, but I do not know how to access them in the first place. Is there any collection or what would be the syntax for accessing these. My idea is to make a loop and use it there, I could be going about this wrong.
In Word:
Hit Alt+F11 to get the VB editor.
Hit F2 to get the Object Browser.
Type "Document" into the search box and hit Enter.
Click on the row that says "Word" under Library and "Document" under Class.
In the "Members of 'Document'" list you will see all sorts of goodies! That includes things like Document.Paragraphs :) . You can then search on MSDN for details of how to use the listed fields. Prepare for lots of searching and reading online!
Once you have found the fields you are interested in, you will be able to try some things. Once you have code that almost works, but not quite, you will be ready to post detailed questions here asking for further help. See the SO tour for more on that process.

Control Button to initiate the writing of a new VBA macro

Would it be possible to have a control button (or any mechanism) that once clicked on, a textbox would appear with fields like macro name Sub "NewMacroTitle"(), a field for the date, a field for reference sources ("Getting Links/URL from a webpage-Excel VBA"found this here"), a brief description of what the macro is to do. All of these fields, except Sub "NewMacroTitle"(), would start with a ' to show a comment and all of this would be inserted into the VBA editor, or enter it into cells on a spreadsheet that could then be copied and pasted into the VBA editor.
My problem is this. I'm new to VBA. I'm also the world's worst at documenting macros. I get an idea for a macro, I'm off into the VBA editor writing away, maybe even get the macro complete. A week later I'm reviewing the macro and the first thing is "what the heck does this thing do?"
I hope you see what I'm trying to do, essentially some way to force me to document the macro before even starting the actual code.
There are tools that will add pre-defined text to the header and/or footer of a sub. I use the one from MZ Tools for error handling code and adding my name and date to the header of each proc. It is worth a look.

VBA script to insert anchor position for autoshapes in Microsoft Word 2007

I'm beyond the limits of my kindergarten-level VBA skills so would like to ask if anyone here can help me write a VBA script to help me work stepwise through my Word document and adjust the anchoring position of all the AutoShapes in the document.
I have a 400-page book in Microsoft Word with at least one and sometimes several marginalia (sidenote) on each page in a thin column to the left of the main body text column. These sidenotes are a concise 'pointer' to a particular point made in the body text and need to appear directly to that point's left.
Each sidenote is in its own AutoShape (within a textbox in that shape). The AutoShapes were placed by the author in an inconsistent way. Mostly they are anchored to a position on the page.
However, I need to repaginate. This will cause the body text to flow differently and I need the sidenotes to flow with it (approximately or exactly)!
So before I repaginate I want to make sure I anchor each AutoShape to the paragraph it belongs to and not to the page it is currently on.
I don't know if that can be done automatically, since I don't know how Word could deduce a purely spatial relationship between an AutoShape and a paragraph.
So I'm guessing I have to make do with a "semi-automatic" process. Something like this:
Press a button to start VBA script
Select next AutoShape
Prompt for user to enter cursor in body text where anchor is to be placed
Resume macro
Place anchor for that AutoShape in that position
Change vertical position paramater of AutoShape to "relative to paragraph"
and "0 mm"
(Alternative, not 0 mm but another value deduced to more
accurately position AutoShape)
Change width parameter of AutoShape to
a particular fixed value 37 mm (some of them were a little
inaccurately drawn)
End cycle and go back to beginning to
Select next AutoShape
Hope that is all understandable.
I've tried to record a VBA script to do some of this but have no idea how to build in the user prompt.
Any help much appreciated!
Craig
You may be better off creating a macro that works on one shape at a time, or anchor all the shapes manually (which you're almost doing anyway) and then write a macro to take care of the various parameters and settings you want.
If you really want to prompt the user while the macro is running you'll need to look into modeless dialogs (not possible on the Mac). See this link: http://www.bettersolutions.com/vba/VXV113/SE846743531.htm
Basically, you'd have to create your own dialog and then show it modelessly so that the user could still place the cursor before hitting OK. If you're new to VBA, this could be difficult to set up.
While searching for solutions to this, I found a post on another forum from someone trying to do something very similar. He was developing a set of tools for editors using Word and until I find the ultimate solution I am using his "Shapes" tool which allows me to change parameters on each AutoShape I select at-a-click instead of opening up and closing a dialogue box each time, plus selecting a different tab each time... His tools can be downloaded at his Editors' Toolkit website.