Posts

Menus and toolbars

Adding a new toolbar item for word count to the Standard Menu Note that each predefined command has an ID. In this case, the word count command has an ID of 792.  Dim btnNew As CommandBarButton     CustomizationContext = NormalTemplate     Set btnNew = CommandBars("Standard").Controls.Add _         (Type:=msoControlButton, ID:=792, Before:=6)     With btnNew         .BeginGroup = True         .FaceId = 700         .TooltipText = "Word count"     End With Adding a double underline command to the formatting toolbar The double underline command has a standard ID of 60  CustomizationContext = NormalTemplate     CommandBars("Formatting").Controls.Add _         Type:=msoControlButton, ID:=60, Before:=7

Paragraphs

Looping through all the paragraphs and changing the space setting before the paragraph Dim parCount As Paragraph     For Each parCount In ActiveDocument.Paragraphs         If parCount.SpaceBefore = 12 Then parCount.SpaceBefore = 6     Next parCount

Headers/footers

Adding text to the header With ActiveDocument.ActiveWindow.View          .SeekView = wdSeekCurrentPageHeader          Selection.HeaderFooter.Range.Text = "Header text"          .SeekView = wdSeekMainDocument      End With Selecting the primary footer of the first section of the document and delete it Dim rngFooter As Range      Set rngFooter = ActiveDocument.Sections(1) _          .Footers(wdHeaderFooterPrimary).Range      With rngFooter          .Delete              End With  After deleting the text, add filename field, two tabs and author field .Fields.Add Range:=rngFooter, Type:=wdFieldFileName, Text:="\p"          .InsertAfter Text:=vbTab &; vbTab          .Collapse Direction:=wdCollapseStart          .Fields.Add Range:=rngFooter, Type:=wdFieldAuthor 

Views

Changing the view to print view ActiveDocument.ActiveWindow.View.Type = wdPrintView

Find and replace

Find and select text With Selection.Find  .Forward = True  .Wrap = wdFindStop  .Text = "Text you need to find"  .Execute End With Bold the text that you are looking for: With ActiveDocument.Content.Find  .Text = "The text you are looking for"  .Forward = True  .Execute  If .Found = True Then .Parent.Bold = True End With Replace all occurances With Selection.Find  .ClearFormatting  .Text = "Text you are looking for"  .Replacement.ClearFormatting  .Replacement.Text = "Text you are replacing it with"  .Execute Replace:=wdReplaceAll, Forward:=True, _  Wrap:=wdFindContinue End With Look for bold formatting and remove it With ActiveDocument.Content.Find  .ClearFormatting  .Font.Bold = True  With .Replacement  .ClearFormatting  .Font.Bold = False  End With  .Execute FindText:="", ReplaceWith:="", _  Format:=True, Replace:=wdReplaceAll End With

Working with ranges

Set range to the first word in the active document Set Range1 = ActiveDocument.Words(1) Assigning one range to another, so that an action on one will affect the other Set Range2 = Range1 Duplicating a range, but changing one won't affect the other: Set Range2 = Range1.Duplicate

Open, save, quit

Quitting, with prompt Application.Quit SaveChanges:=wdPromptToSaveChanges Save the current document  Application.ActiveDocument.Save Save As: Application.ActiveDocument.SaveAs ("New Document Name.docx")