Insert several Autotext items in order - vba Word - vba

I am working with a Macro enable Word 2016.
I have a macro that will insert several AutoText entries (4) which are entered in a loop. This portion is working ok but I notice that they are not entered in order.
The reason why they're not entered in order is because in each loop iteration I always reference a specific bookmark (as a range). I wonder if there is a way to update the range at each iteration so the AutoText entries are entered one after the other ones?
Thank you!

After some trial and error, I found a way to do this. What I did was the following:
Moved the selection (cursor) to the desired bookmark
Insert the AutoText at the Selection.range location
For all the next items, I always used the Selection.range instead of using the bookmark, since the Selection (cursor) will update as I'm adding text.

Related

How to use a macro to "tab" between form fields, in a table, in a protected Word document?

I'm writing a macro which copies data from an excel spreadsheet to a word document (to save myself time copy/pasting). The word document is protected and has legacy form fields to enter data. I'm trying to figure out how to iterate between these fields. For most of the fields, Selection.Next(Unit:=wdParagraph, Count:=1).Select works just fine. However, some of the fields are in a table, which that solution doesn't work for.
Selection.Next(Unit:=wdParagraph, Count:=1).Select will get me into the form in the first cell of the table. But it stops there, failing to go to the next field. I've tried a number of variations, such as increasing the Count, and for Unit tried wdParagraph, wdItem, wdCell, wdColumn, wdTable, wdSection, and wdCharacter. No matter what I try, the selection stays in the first cell of the table.
I've also tried Selection.TypeText, Selection.NextField.Select and Selection.MoveDown (and similar), which throw 4605 errors that I assume are due to document protection.
Found the solution:
<document variable here>.FormFields(<index of form field in table>).Select

Extracting table data from Outlook email body

I am getting Outlook email, were the Body contains a Table consisting of several string Labels and associated string Text. I want to extract three Values associated with the respective Label. The first two searchs are one column right of the Label and the last one is one row below the Label.
I tried InStr() which works great for retrieving to the right of the Label, but it doesn't seem to work for the Value that is one cell below the Label.
Can I use MoveRight and MoveDown from Excel/Work inside the Outlook VBA or use "Range(Rng.Offset(0, 1).Address).Value" and "Range(Rng.Offset(1, 0).Address).Value" to select the Value to the right and below in Outlook VBA.
Key point here is that it is a structured table in the email body.
i have one email with a table that happens to be nested inside another table
that is why Tables(1).Tables(1) is used
the following code is for exploring the table.
first line highlights the cell in the table, so that you can check if you are referring the correct cell
the second and third lines shows how to get the value of a table cell
do not try to examine any range object in the watch window or the locals window. it causes outlook to crash. (no idea why)
if you want to explore any attributes of the range object then copy the email body to msWord. it does not crash when examining the range object
i could not find anything that refers to column headers
Sub aaaaa()
Application.Inspectors(1).WordEditor.Tables(1).Tables(1).Rows(1).Cells(1).Select
stop
Debug.Print Application.Inspectors(1).WordEditor.Tables(1).Tables(2).Rows(3).Cells(4).Range.Text
Debug.Print ActiveDocument.Tables(1).Tables(1).Cell(1, 1).Range.Text
End Sub

VBA to copy text from excel to on specific location in wordfile

Problem: Pasting copied data from excel to specific location in a word file.
Currently I have code which can paste the value, but it does so to "paragraph1"
myDoc.Paragraphs(1).Range.Paste
How do I specify the exact location (by line) in which to paste the data?
Let me know if more info is required.
Thanks!
Mohd Akhtar
Word gives a number to each character in the document's body, from 1 up. It then defines a range with Range.Start to Range.End So, Paragraphs(1).Range might be equal to Range(Start:=1, End:=120).
The text contained in that range is Range.Text, Read/Write. Therefore, Paragraphs(1).Range.Text = "My new paragraph text" will replace the existing text in the document's first paragraph. ActiveDocument.Range(0, 0).Text specifies the range before the first character in the document.
In order to insert text at a specific location you have to find the location, meaning the Range. As you have seen above, if the range has a length of 0 you can insert before or between existing text, and if it has any length the new text will replace whatever was there before. New and old text need not have the same length.
Counting paragraphs is helpful to find a range. You can also count words or sentences. You can search for a specific word combination. Or you can use a bookmark. In all of these cases you define a range the text of which you can replace outright, or which you can use to find a location relative to it where to insert the text, such as the beginning or end or after the 3rd word or whatever.
You could also use some bookmarks:
You can choose where you put your bookmark and then write on it like this
ThisDocument.Bookmarks("NAME_OF_THE_BOOKMARK").Range.Text = THE_EXCEL_DATA
To place a bookmark you have to click on the selected area and then go on Insert->Bookmarks and then name it.

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.