Sorting word bookmarks by location in vba - vba

I am trying to cycle through the bookmarks in a word document by their location.
yes I have seen this: How to get list of all bookmark-elements from a Word document to an array in order by location: VBA / Word
no it didn't work. and for the life of me I am not sure why this doesn't since it is lifted from a MS support page.
Sub cycleBookmarks()
ActiveDocument.Bookmarks.DefaultSorting = wdSortByLocation
Dim bkm As Bookmark
For Each bkm In ActiveDocument.Bookmarks
Next bkm
End Sub
Anybody know how to properly cycle through bookmarks by document location? I don't care about headers and footers (though a complete answer would be nice)

According to this MSDN site DefaultSorting property:
Returns or sets the sorting option for bookmark names displayed in the
Bookmark dialog box (Insert menu). Read/write WdBookmarkSortBy.
Therefore you need to use #Mana solution from the link you provided in your question.

Related

Extract data from content controls in Word to Excel

I have a Word document that is "form-fillable", i.e. it has content control objects in it such as rich text and date picker content controls. I am looking to extract the data from specific fields into Excel. For example, every form has the project title, start date, and manager. I would like 1 row for that form with those three pieces of data. Eventually this will need to be done for a few hundred of these forms every few months, but for now I'd like to just start with one.
I envision having a button on the Excel sheet that will run VBA code to pull the data from the Word document, and populate the proper cells in the sheet. With the filepath for the Word doc being specified by the user.
I am new to VBA. How do I point my code at the right file, and then again at the specific field I need? Do I give the fields titles in the Word doc?
This is in MS Office '13
Your application is going to have a large number of specific details which are difficult to address without the specifics. To get started, here is some very simple code to get the Date from a drop-down in Excel from Word.
Note to work with Word, you need to create a reference to "Microsoft Word 15.0 Object Library" on the Excel side. Tools -> References in the VBA Editor.
When working across applications, it can help to write the VBA in Word and then move it over to Excel once you have the properties you want.
This VBA for the Excel side of the equation:
Sub GetDataFromWordFile()
Dim wrd As Word.Application
Dim file As Word.Document
Set wrd = New Word.Application
Set file = wrd.Documents.Open("test.docx")
Range("A1") = file.ContentControls(1).Range.Text
file.Close
wrd.Quit
End Sub
My Word file includes a single ContentControl. Not sure how you address the ones you want (trial and error?).
The Excel side drops the date in the right place.

MS Word Macro to replace all hyperlinks with a single word

I prepare a MS Word file with a large amount of news articles, each followed by a full, long clickable hyperlink (e.g. http://www.newssite.com/this-is-the-name-of-the-article.html).
I need to replace all the hyperlinks in the file with a single word: "Link", while keeping their url adresses and click functionality. I can use MS Word "change hyperlink" dialogue to do that, but doing it by hand takes incredible amount of time.
The hyperlinks, however, are not always formatted as 'hyperlink' style. Is there any condition for MS Word Macro replacement that will lookup hyperlinks by their functionality, not by their style or text?
Despite this task seems to be quite common, I could not find anything like it in the web.
Since you have the hyperlinks already created. It should just require looping through the documents hyperlinks collection and changing the 'TextToDisplay' property. This should get you started:
Public Sub ChangeHyperlinksText()
Dim hlink As Hyperlink
For Each hlink In ThisDocument.Hyperlinks
hlink.TextToDisplay = "Link"
Next hlink
End Sub

How to tell if a Word selection is revised in VBA

I am writing a VBA procedure in Word 2010 that searches a document for a search term. If it is found, I want to run some other code, but only if no portion of the resultant text is revised. So I can't just loop through the document revisions, because I need to find text that is not revised. I would like to either exclude revised text from the search (but I couldn't find an option under Selection.Find that did that), or check if the selected text is revised after the search executes (but I couldn't find an option under Selection.Range.Revisions that did that either).
It seems, based on quick try and test I've just made, that the answer is quite simple.
You need to find appropriate text range and check .Revisions property as follows (here, for Selection):
If Selection.Range.Revisions.Count > 0 Then
'selected range is inside revision
Else
'selected range is not revised
End if

Edit the style of all hyperlinks in a Word document

I have a word document that is littered with hyperlinks. The links themselves work fine, but for some reason, most of them are not blue anymore! All I'm really looking to do is try to find a way to use a macro or something to go through the document and add the "Hyperlink" style format to each hyperlink.
I tried to edit some macro code myself (one that changes all the links URLs), but I keep making the problem worse! I used to be good at VBScript, but it's been ages since then.
Any easy solution that doesn't involve manually changing each style?
As a side note, all of them currently are in "Normal" style, for some reason.
Try to execute this VBA script (best in debugging mode using the F8 key - have VBA and Word windows side by side so you can see what's happening):
Sub FormatLinks()
Dim H As Hyperlink
For Each H In ActiveDocument.Hyperlinks
H.Range.Select ' (A)
Selection.ClearFormatting ' (B)
H.Range.Style = ActiveDocument.Styles("Hyperlink") ' (C)
Next H
End Sub
This will
cycle through all hyperlinks in your document (A),
remove any formatting on the underlying text (B) and
assign the undrelying text to style "Hyperlink" (C)
(C) is not strictly necessary, as (B) should already sanitize your document, but maybe better to have hyperlinks really assigned to style "Hyperlink" because you may want to change the style later on.

Finding the Outline Level of a Office 2010 Word Document using VBA

I'm creating a VBA macro that will validate a submitted document, but I can't seem to find a way to check for the Outline Level of the document as a whole. What I need is a way to tell the outline level selected in the Outlining Ribbon, 1-9 or All Levels.
The setting you are after is an application setting which is not stored in the file.
You can set a specific level using the following VBA code:
ActiveWindow.ActivePane.View.Type = wdOutlineView
ActiveWindow.View.ShowHeading 6
The .OutlineLevel property of a Word document can apply to the Paragraph, ParagraphFormat or Paragraphs Collection objects. It is an enumeration that can take the values wdOutlineLevel1 - 9, or wdOutlineLevelBodyText.
To find the OutlineLevel of the first paragraph in the document, use:
Dim currOutlineLevel
With ActiveDocument
currOutlineLevel = .Paragraphs(1).OutlineLevel
End With
Note that calling Paragraphs(x).OutlineLevel errors if called with the active doc in Outline view which makes iterating an Word outline for export rather a bore. You have to toggle the view (which also doesn't appear to be directly scriptable) then toggle back. Hope that saves someone else wasting time...