Showing posts with label Macros. Show all posts
Showing posts with label Macros. Show all posts

Saturday, September 02, 2017

Indent Code

Realign a bunch


Indenting blocks of VBA code, such as statements within loops or If...Then statements, makes reading a procedure much easier.

You probably indent a code statement using the [Tab] key, and outdent by using [Shift][Tab].

However, you may not be aware that the [Tab] and [Shift][Tab] techniques also work when multiple code lines are selected.

The Visual Basic Editor also provides Indent and Outdent buttons on the Edit toolbar that allow you to easily reposition blocks of code.


See all Topics

Wednesday, August 10, 2016

Hidden Macro Names and SHortcuts

Revealed


Word has built in macros to perform routine actions such as using the Format Painter to copy formatting.

Rather than trying to guess the name or look up the shortcut keys, use this seldom mentioned trick to find toolbar macro names.

Press the three key combination of Ctrl, Alt, and + (the plus sign on the Numbers keypad).

The mouse pointer changes to a 4-leaf clover.

Click on a toolbar icon. Word will display a form revealing the macro name and the assigned shortcuts.



(It works the same way in 2007+)


See all Topics

Tuesday, July 21, 2015

List All Files

All files in a folder


Here is a macro that will produce a list of all the files in a selected folder.
  • The folder name for the listed files
  • The file names of the files found
  • The file sizes of the files found
  • The dates and times of the files found
  • The total number of files listed
Macro to List All Files in a Folder


See all Topics

Wednesday, May 27, 2015

Signing Macros

Security levels


There are three levels of Macro security:

High:
A computer user can open without a prompt a digitally signed project from a trusted publisher. Otherwise, the application blocks opening signed projects from untrusted publishers as well as unsigned projects.
Medium:
A computer user can open without a prompt a digitally signed project from a trusted publisher. In addition, you can also designate the publisher of a signed project as trusted so their projects will open without a prompt in the future. Unsigned projects are always prompted with a reminder that the file may contain potentially harmful code, but users can elect to open them anyway.
Low:
A computer user can open an unsigned project without a prompt. When users make a Low security setting, they're reminded that they aren't protected from potentially unsafe macros.
Securing Access Databases
"If you've used Access 2003, you've probably seen several security warning messages - Access 2003 cares about your security. An important part of Access 2003 security is digitally signing your code. As Rick Dobson shows, you can do it, but preparing for digital signing is critical.

A digital signature acts like shrink-wrap on your project: Clients know that they're getting a copy directly from you that no one else modified. Clients will also know that they're working with "your" code and not any version of it modified by a third party. As computing moves forward into a "security conscious" era, learning how to acquire and use a digital certificate is also important for interfacing with organizations that adopt policies of only running digitally signed Access 2003 projects: Your users may refuse to accept software from you that isn't shrink-wrapped."
Also:
Signing Access Projects
Other links:

How to make sure that your Office document has a valid digital signature in 2007 Office products and in Office 2003


See all Topics

Friday, May 22, 2015

Sequentially Number Invoices

Creating sequentially numbered documents


Use an Autonew macro to add a sequential number to a document and save it with that number.

In the template from which you create the document, insert a bookmark named Order in the location where you want the sequential number to appear and create an AutoNew macro, as follows:


Sub AutoNew()
Order = System.PrivateProfileString("C:\Settings.Txt", "MacroSettings", "Order")
If Order = "" Then
Order = 1
Else
Order = Order + 1
End If
System.PrivateProfileString("C:\Settings.txt", "MacroSettings", "Order") = Order
ActiveDocument.Bookmarks("Order").Range.InsertBefore Format(Order, "00#")
ActiveDocument.SaveAs FileName:="path" & Format(Order, "00#")
End Sub 


If you do not need to display the number in the document, but just want to save it with a sequential number, there is no need to create the bookmark in the template and you should then delete the second last line of the code.

Article contributed by Doug Robbins
Word MVP Site


Microsoft Knowledgebase:
Macro to Increment Invoice Number to New Form Document


See all Topics

Friday, April 24, 2015

All Fonts

List maker



Here is a macro that will produce a list of all of the installed fonts.


  1. Open Word.
  2. Use Alt+F11 to open the Visual Basic editor.
  3. Choose Insert>Module from the Menu.
  4. Copy and Paste this code in the module.
  5. Return to Word and go to: Tools>Macro>Macros.
  6. Select and run "InstalledFonts".
Sub InstalledFonts()

Dim F As Integer
Dim InstalledFonts As Table

 'Open a fresh document

Set FreshDoc = Documents.Add

'Create a table and define the header

Set InstalledFonts = FreshDoc.Tables.Add(Selection.Range, FontNames.Count + 1, 2)
With InstalledFonts
.Borders.Enable = False
.Cell(1, 1).Range.Font.Name = "Arial"
.Cell(1, 1).Range.Font.Bold = 1
.Cell(1, 1).Range.InsertAfter "Font Name"
.Cell(1, 2).Range.Font.Bold = 1
.Cell(1, 2).Range.InsertAfter "Example"
End With

'Loop through all the fonts and add them to the table

For F = 1 To FontNames.Count
With InstalledFonts
.Cell(F + 1, 1).Range.Font.Name = "Arial"
.Cell(F + 1, 1).Range.Font.Size = 10
.Cell(F + 1, 1).Range.InsertAfter FontNames(F)
.Cell(F + 1, 2).Range.Font.Name = FontNames(F)
.Cell(F + 1, 2).Range.Font.Size = 10
.Cell(F + 1, 2).Range.InsertAfter "ABCDEFG abcdefg 1234567890 &$@"
End With
Next F
'Sort the names of the fonts

InstalledFonts.Sort SortOrder:=wdSortOrderAscending

End Sub

Also see a more sophisticated macro using Excel from Erlandsen Data Consulting: Display all installed fonts (Excel)


See all Topics

Monday, March 23, 2015

Change Case

CAPS - No - caps



Sometimes mistakes are made in setting the case for sentences.
There are four general categories of capitalization:

Sentence Case - The first letter of a sentence is capitalized

Lowercase - all words are in lowercase

Uppercase - ALL CAPITALS

Title Case - All Words Are Capitalized
(This is, really, "Proper case". Title case would be "All Important Words are Capitalized".
Nouns, verbs, adjectives, and adverbs should be uppercase. Common articles, prepositions, and conjunctions should be lowercase
.)
You can make changes to selected text by going to
Format>Change Case
and choosing the correct style. (Including tOGGLE cASE)
You could also use a keyboard shortcut.
Select the text and then hold down the SHIFT key and tap the F3 key to toggle through three of the main cases – All Cap, Lowercase, and Title.

SAP Design Guild:
Quick Guide to Capitalization in English

From The Editorium.com:
Here's a macro to change Heading styles to true Title case:
TITLE CASE MACRO, VERSION 2
By Jack M. Lyon

Word Tips:
Capital after colon

Automatically correct capitalization in most any MS 2007 App.


See all Topics

Monday, March 02, 2015

Word Ranges

Pre-defined locations



When entries are made in a document, Word creates a Story Range to identify what part of the document is being used. These ranges can be used in macros to search for items , change text, or other actions.

This macro, for instance, changes the text in just the header of the first section:

Sub HeaderFooterObject()
Dim MyText As String
MyHeaderText = "This would be your text"
With ActiveDocument.Sections(1)
.Headers(wdHeaderFooterPrimary).Range.Text = MyHeaderText
End With
End Sub

When you use Edit>Replace in Word, it does a fine job of locating all occurrences of the target in the body of the document or in the header or footer.

Something fails, however, when you record the action and try to run it as a macro. To make it work, you must loop through the built in ranges of a Word document.

The exercise is interesting if only for the exposure to the built in ranges such as:

  • wdCommentsStory
  • wdEndnotesStory
  • wdEvenPagesFooterStory
  • wdEvenPagesHeaderStory
  • wdFirstPageFooterStory
  • wdFirstPageHeaderStory
  • wdFootnotesStory
  • wdMainTextStory
  • wdPrimaryFooterStory
  • wdPrimaryHeaderStory

    and
  • wdTextFrameStory.
See this article for more information: Word.MVPS.org:
Find and replace with VBA
 

Also: Microsoft KB
VBA macro examples to insert text into a document


See all Topics