Posts

Showing posts from February, 2018

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")

Adding text

Assign text to paragraph ActiveDocument.Paragraphs(1).Range.Text = "New text for Paragraph 1" After current text, type a new paragraph and format it  With Selection             ' Do not accidentally over-write selected text             .Collapse Direction:=wdCollapseStart 'Type new line             .TypeParagraph             .Style = ActiveDocument.Styles(wdStyleNormal)             .TypeText Text:="Your written text"         End With

Formatting

Format selected text (change font, size, etc)  With Selection.Font  .Name = "Times New Roman"  .Size = 14  .AllCaps = True  End With  With Selection.ParagraphFormat  .LeftIndent = InchesToPoints(0.5)  .Space1  End With Apply formatting to a range of paragraphs  Dim rngFormat As Range  Set rngFormat = ActiveDocument.Range( _  Start:=ActiveDocument.Paragraphs(1).Range.Start, _   End:=ActiveDocument.Paragraphs(3).Range.End)  With rngFormat  .Font.Name = "Arial"  .ParagraphFormat.Alignment = wdAlignParagraphJustify  End With For more adjustments: Dim rngFormat As Range  Set rngFormat = ActiveDocument.Range(Start:=0, End:=0)  With rngFormat  .InsertAfter Text:="Title"  .InsertParagraphAfter  With .Font  .Name = "Tahoma"  .Size = 24  .Bold = True  End With  End With  With ActiveDocument.Paragraphs(1)  .Alignment = wdAlignParagraphCenter  .SpaceAfter = InchesToPoints(0.5)  End Wi

Interacting with another app

Creating a new word document Set wrd = CreateObject("Word.Application") wrd.Documents.Add wrd.Visible = True Setting the start up path wrd.Options.DefaultFilePath(wdStartupPath) = "c:\user\" Quitting word wrd.Quit Starting word from another app Set wrd = CreateObject("Word.Application") Create and launch another app from Word Set myobject = CreateObject("Excel.Application") MsgBox myobject.StartupPath Set myobject = Nothing Using DDE Channel to send data to another app, like excel: Dim lngChannel As Long lngChannel = DDEInitiate(App:="Excel", Topic:="System") DDEExecute Channel:=lngChannel, Command:="[OPEN(" &; Chr(34) _  &; "C:\NameOfExcelFile.xls" &; Chr(34) &; ")] DDETerminate Channel:=lngChannel lngChannel = DDEInitiate(App:="Excel", Topic:="NameOfExcelFile.xls") 'Go to row 1 column 1 and key in data DDEPoke C

Coding basics

If else statement If Selection.Paragraphs.Count > 1 Then         'What will happen     Else 'What will happen     End If

Getting started

Record a macro To edit in VBA, you can record a macro View->Macro Record a macro - do some stuff that you need done and automated Give it a name Stop recording View->Macro Step into code You can also edit in VBA editor One a PC: Alt+F11 In Macbook: Go to preferences ->  ribbon -> developer tab Add visual basic editor to the ribbon You should spot the visual basic icon at the top with the save icon Here is how to write your first macro In the VBA editor, type: Sub NameOfMacro() End Sub To run it, either press F5 or click on the 'Run' button (the one shaped like a play button). When saving the macro, you can save it in a way that it is able to run on another computer Type your code. Add a button in the ribbon and a shortcut key to run your macro. Save the file as a .dotm file.

Page setup

Create a new page break which is in landscape format (in between pages)  If Documents.Count > 0 Then         ' The user has a document open, so insert a         ' landscape section.         With Selection             ' Do not accidentally over-write selected text             .Collapse Direction:=wdCollapseStart             ' Insert section breaks with blank paragraphs             ' in the new section.             .TypeParagraph             .Style = ActiveDocument.Styles(wdStyleNormal)             .InsertBreak Type:=wdSectionBreakNextPage             .TypeParagraph             .TypeParagraph             .TypeParagraph             .InsertBreak Type:=wdSectionBreakNextPage             .MoveUp Unit:=wdLine, Count:=3             ' Set the orientation of new section to landscape.             .PageSetup.Orientation = wdOrientLandscape             ' Provide guidance to the user.             .TypeText Text:="Your landscape section start

Toggle showing text boundaries

  With ActiveDocument.ActiveWindow.View         .ShowTextBoundaries = Not .ShowTextBoundaries     End With

Tables

Refer to the text in a cell in an Active Document ActiveDocument.Tables(1).Rows(3).Cells(2).Range.Text

Dialogue box (input, pop up, custom)

MsgBox "This is the message."

Sorting

Sort selection if there is more than one paragraph If Selection.Paragraphs.Count > 1 Then         Selection.Sort     End If