How to tell if a Word selection is revised in VBA - 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

Related

Jump to found text in document

I am programming a loop which searches for highlighted text in a document.
If a text is found it should be shown in the Word window similar to the non-vba search function that you have in word.
How can I achieve this?
Here is the code that I use to find the highlighted text, and it works well.
But I don't now how to show the found instances in the word document.
With ActiveDocument.Range
.Find.Highlight = True
Do While .Find.Execute
' Jump to found text and prompt user to take action.
Loop
End With
PS: Once an occurrence is found, I want to prompt the user via to take action on this part of the text. The context is important for the user to decide, therefore he has to see the text. I'm clear on the prompting part, but I cannot figure out how to show the text to the user.
You need to use the .Select method. I'd recommend doing this on a .Duplicate of the current found range. e.g. Inside your do loop use '.Duplicate.Select' (NOT .Find.Duplicate.Select)

Convert automatic numbering and bullets to plain text

I have a word document with automatic numbering and bulleting.
I have selected the text where I need to convert automating numbering and/or bulleting to normal text.
In addition I need to keep both the formatting and numbers/bullets of the selected text.
What I have already tried:
cut the paragraphs and special pasted them (but it breaks formatting);
unpressed the "numbering"/"bulleting" button (but it erases all numbers and bullets);
used VBA-macro (but it returns an error):
Code (error, method or data member not found):
Sub convertNumbersAndBulletsToText()
Selection.ConvertNumbersToText
End Sub
What would you recommend me to do in order to keep both formatting and numbers/bullets?
You have practically done everything yourself!
This code will work:
Sub convertNumbersAndBulletsToText()
Selection.Range.ListFormat.ConvertNumbersToText
End Sub
Your example returns error because ConvertNumbersToText method doesn't work with Selection. It works with Range!
(look here: Change selected automatic numbered list to plain text in word)
Beware!
If you want to carry out many changes, you may find it easier to make them with ActiveDocument (look below).
But if want to do it manually (or through a loop),
then you'd better loop from the last element you want to convert till the first one
(not vice versa, because auto-numbers would then increment by one all the time)!
Small Tips
Personally I would recommend you to use this code instead:
Sub convertNumbersAndBulletsToText()
Dim myRange As Range
Set myRange = Selection.Range
myRange.ListFormat.ConvertNumbersToText
End Sub
Why this one? It is a little bit more flexible! Instead of Selection.Range you could use any other type of Range (ActiveDocument, ActiveDocument.Paragraphs, myRange.SetRange etc)
Here are some links from msdn to give you basic examples of Ranges: 1) Range Object (Word) (msdn), 2)
Range.SetRange Method (Word) (msdn).
Just for your information, you don't need to save VBA if you don't want to. You can use Immediate Window to launch VBA.
Press alt+f11 (VBA-editor), then ctrl+g (Immediate Window).
Paste the code bellow, press enter.
VoilĂ !
Code (for Immediate Window):
ActiveDocument.ConvertNumbersToText
(It converts auto-numbers and auto-bullets to normal numbers and bullets everywhere in ActiveDocument).
The result of any VBA here would be number+tab+text. If you want to have number+space+text you can:
either at the very end replace (press ctrl+h) this one .^t (dot and tab) for . (dot and whitespace),
or at the very beginning 1) select the list, 2) right click on it, 3) click "Adjuct list idents", 4) click "Follow number with: Space". (Look here: Adjust the spacing for a single list item (support.office))
You may need to have a leading zero in (auto-)numbering, then you can press ctrl+f9, write SEQ MyList \# "000" inside curly brackets, press alt+f9 to finish (look here: Insert fields in Word (support.office)). But this goes beyond the question, though you may find word fields really useful in some cases.
To sum up:
You can replace both bullets and numbers for plain text in Word:
for Selection (look above);
for ActiveDocument (look above);
with a Range (examples in msdn);
with a loop (examples are welcomed). But bear in mind that you are to loop from the end of the document to the beginning.

Event change of table in Word

Private Sub Document_Change(ByVal Target As Range)
Set table = ActiveDocument.Tables(1)
If Not Intersect(table, Target) Is Nothing Then
Target.AutoFormat ApplyColor: Red
End If
End Sub
I have the following code, but it does not seem to work in VBA Word.
Can anyone help me out?
I suppose that you copied the code from Excel VBA and tried to rebuild it a bit.
There are a few differences between Excel and Word VBA.
Intersect does not exist in Word VBA.
DocumentChange event in Word works differently from what you would expect - It occurs when a new document is created, when an existing document is opened, or when another document is made the active document. (https://msdn.microsoft.com/en-us/library/office/ff822189.aspx)
If you want to make changes in red, you may do the following:
Record the wholeDocument in a static string.
When there is a change - record again in a new string.
Compare the strings and color the differences.
However, this is a bit tough, as far as word does not have Changeevent as we are expecting it (e.g. as in Excel). Thus, you should run the VBA code a few times automatically.

automating word 2010 to generate docs

the webapp was already done on office2007 and i need to convert it so it'll work in office2010.
i was able to convert the header generator part of the code but i have problem with the body of the doc itself. the code copy the data from a "data" doc and paste it into the generated doc.
appword.activewindow.activepane.view.seekview = 0
'set appsel1 = appword.activewindow.selection
set appsel1 = appword.window(filepath).selection -that is the original one
appdoc1.bookmarks("b1").select
appword.selection.insertafter("some text")
appsel1.endkey(6) -the code stops here
appword.selection.insertafter("some other text")
the iexplorer debuger says ERROR:appsel1 object required. and when i view its data using the iexplorer debugger its data is "empty" instead of "{...}"
can anyone tell me what i'm doing wrong
if you need more of the code tell me.
From MSDN
After this method is applied, the selection expands to include the new
text.
If you use this method with a selection that refers to an entire
paragraph, the text is inserted after the ending paragraph mark (the
text will appear at the beginning of the next paragraph). To insert
text at the end of a paragraph, determine the ending point and
subtract 1 from this location (the paragraph mark is one character).
However, if the selection ends with a paragraph mark that also happens
to be the end of the document, Microsoft Word inserts the text before
the final paragraph mark rather than creating a new paragraph at the
end of the document.
Also, if the selection is a bookmark, Word inserts the specified
text but does not extend the selection or the bookmark to include the
new text.
So I suspect that you still have no selected text.
I wonder if you can do a Selection Collapse(wdCollapseStart) but that's just a thought.

How can I find the number of highlighted chunks of text in an MS Word Document

I am writing a macro in MS Word which should find all highlighted text in a document and perform some action on each. I am planning a loop to do the search and manipulation part and have no problem with this portion of the code.
But I don't know how to find how many iterations I'm going to need. Is there a way to determine the number of highlights in VBA?
Many thanks in advance.
With ActiveDocument.Range.Find
.Highlight = True
While .Execute
Debug.Print .Parent.Text
Wend
End With
There's no need to calculate the number of matches up-front. You can execute the search in a loop and it will stop once there are no more matches.
Make sure you apply the search to the right part of the document.I used ActiveDocument.Range, but any Range object will do. Maybe something more specific is better for your case.
Also, check out the many other properties of the Find object and set them to sensible values, this is better than going with the defaults (nobody can remember all defaults for all options, plus the Find object might already be set up by some earlier search).