Can I create automatic headings in a Microsoft Word document which has around 500 pages? - vba

I need to add headings automatically in a Word document which has 500 pages. All the topics written with the font 'Courier' and Size '24' needs to be given style - 'Heading 1'. Can someone help me write this using VBA?

Do you have to do this using VBA?
The Advanced Find function let's you highlight all text in your document based on parameters like font and size.
Set these parameters using the "Format"-dropdown -> "Font..."
Then use the "Find In"-dropdown -> "Main Document" to select all text matching your criteria.
See attached screenshot, and then manually set the style for the entire highlighted selection.

Related

How to pull text from a shape in a Word document using VBA?

I'm trying to grab the text from inside a shape on a Word document.
Sub textgrab()
MsgBox ActiveDocument.Shapes("Rectangle 85").TextFrame.TextRange.Text
End Sub
I get the error:
Run-time error '-2147024809 (80070057)':
The item with the specified name wasn't found.
In the Word document when I go to the top menu, hit the shape format tab, and in the arrange section, I select 'selection pane', I get a list of all the shapes, 'Rectangle 85' is there.
When I select it, it highlights the box i'm trying to grab the value from.
This is a pdf that I've opened in Word. I'm trying to automate a process that will open a pdf invoice, grab the dollar total, and pull it into Excel.
Solution for those that stumble upon this later. I used the following:
ActiveDocument.ActiveWindow.Panes(1).Pages(1).Rectangles.Item(i).Range
Word can only extract text from Drawing objects. These are inserted in the UI, for example, from Insert/Shapes. Shape.TextFrame.TextRange has no OCR capabilities, so can't be used to get text "embedded" in other kinds of graphic objects, such as an embedded PDF file or a JPG or anything similar.
When uncertain whether a particular Shape supports reading or writing text, right-click it in the UI and see if the menu selection Add Text or Edit Text is available.

Can I use the Shrink Method for the whole document in word

I don't know anything about code. I work with e-learnings in Storyline 3. I sometimes localize these e-learnings and use the translation tool in Articulate which basically exports an MS Word file. Sometimes the target languages are longer and I need to decrease the font size by percentage for the whole document. Usually, there are at least 3 different font sizes that I need to decrease accordingly. I am wan to develop a macro that I will use for multiple documents.
I couldn't find a way to do this by percentage, but looks like the Shrink or Grow Methods will do the work! I found this code in the reference page but looks like it works only for a selected object. The issue is that the exported MS Word file is in a table with each text box in the storyline separated to a cell. When I select the whole table it does not work.
If Selection.Type = wdSelectionNormal Then
Selection.Font.Grow
Else
MsgBox "You need to select some text."
End If
Could you please help me and let me know if this would be possible for the whole document, or the selected table? It would be very much appreciated. Thanks in advance.
It is unclear from your question whether the table in the Word document contains the actual text boxes or just the text they contain.
If it is just the text then Shrink may work. I tested this on a document with a single table containing only text:
ActiveDocument.Range.Font.Shrink

Unable to extract and re-insert MS Word Content Control using VBA and InsertXML

This question is related to my other question: Range.InsertXML using Transform
In MS Word it is easy to insert a content control using VBA, for example:
ThisDocument.ContentControls.Add wdContentControlRichText, Selection.Range
I've recently started exploring more in the XML side of things, e.g.:
Debug.Print ThisDocument.Range.XML seems to (or actually does) produce the XML for a Word document. However, if I create a NEW, BLANK document and add a Content Control I am unable to extract and reinsert the Content Control (oCC).
My steps:
added 2 blank paragraphs to a new document
added oCC to the 2nd paragraph
selected the oCC paragraph
immediate window: thisdocument.Paragraphs(1).Range.InsertXML selection.Range.XML
At first glance it LOOKS like the Content Control was duplicated, BUT on closer inspection, it was deleted and only the formatted text remains (see image, top paragraph is actually just formatted text).
Thinking I could out smart MS Word I set the properties of the Content Control to '...can not be deleted', but that didn't help.
I've also tried to insert into a separate document in case the issue had something to do with duplication of something that ought to have been unique.
In a nutshell:
To answer this question I need a way to insert a Content Control to a document using a combination of VBA and XML (or confirmation that what I am attempting is not possible).
Just realized I should use Selection.Range.WordOpenXML instead of Selection.Range.XML

Traversing word-by-word in VBA through the contents of a Word textbox

Given a Microsoft Word textbox filled with formatted text, how do I use VBA to:
Iterate through the textbox content, word-by-word
At each iteration, copy/cut out that one word (including its formatting) out of the text box
Append (i.e. paste) that cut formatted word into the end of a second Microsoft Word textbox
[My situation is as follows: I have multiple continuous related texts (i.e. text + footnotes + a second set of footnotes, etc.), and my goal is to programatically lay all of that out in Word text boxes on pages of a Microsoft Word document.
I will set up the second box (that one that the words are being pasted into) to be auto-sizing, and I will then stop the "cut-paste" process once the second box gets to a certain size (which will be programatically determined based on page margins, other textboxes already occupying the page, etc.).]
I'm breaking my head over this for weeks with no results! Thanks a LOT to anyone who can send me in the right direction...

How to assign paragraph / character styles automatically in word 2010

Hi I've a 200 pg fully formatted word document. The person who had worked on it earlier had not applied any style to the whole document and he has done it manually. Though the formatting was done very neatly it has become my job to assign character/paragraph styles to each and every paragraph. Does any one know of a script which assigns character/paragraph styles automatically to the existing word 2010 document?
You'd better use Word VBA to deal with your issue.
What you should do is:
Find the style you want to replace with a style (see MSDN or some tips)
Apply style to the found selection (see this SO thread: How do I apply a style to multiple selections in Word using VBA?)
If you want to check the right style, you can record a macro and adapt it.
You can use the Find and Replace dialog to find text based on its format (e.g. Bold, Size 14) and replace it with a Style (e.g. Heading 2).
This article sums it up pretty well....
http://www.howtogeek.com/howto/microsoft-office/search-and-replace-specific-formatting-fonts-stylesetc-in-microsoft-word/