Solved

VBA Code for Word 2010 Format Painter

Posted on 2014-12-24
17
601 Views
Last Modified: 2016-02-11
We have Word 2010.  If you double-click the Format Painter button, you can apply the same formatting to multiple places in a document just by clicking different places in the document.

I need the visual basic code for the equivalent of this for a project I'm working on.

Selection.CopyFormat doesn't work.
0
Comment
Question by:Senniger1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 8
17 Comments
 
LVL 27

Expert Comment

by:MacroShadow
ID: 40516825
Selection.CopyFormat will only copy the format of selected text, Selection.PasteFormat will apply that formatting to the new selection.
0
 

Author Comment

by:Senniger1
ID: 40516843
Thank you for that information.

I need the visual basic code for double-clicking the Format Painter button.
0
 
LVL 27

Expert Comment

by:MacroShadow
ID: 40517375
You don't need to since the PasteFormat is applied by running a macro, you can run the macro as many times as you want.

Alternatively, If you want to mimic Word's built in functions follow these steps:

1. Add this code to a new module:
Dim objClass As New clsWordApp

Sub Register_EventHandler()
    Set objClass.appWord = Word.Application
End Sub

Sub CopyFormat()
    Selection.CopyFormat
End Sub

Open in new window

2. Add this code to a class module (change it's name to clsWordApp):
Dim objClass As New clsWordApp

Sub Register_EventHandler()
    Set objClass.appWord = Word.Application
End Sub

Sub CopyFormat()
    Selection.CopyFormat
End Sub

Open in new window

3. Run the Register_EventHandler macro.
4. Run the CopyFormat macro to copy the current selection's format.
5. Select any other text and see what happens...

If you want this functionality in all documents, add the above code (regular and class modules) to the "Normal" VBA project, and add the following code to the "ThisDocument" module of your "Normal" VBA project:
Private Sub Document_New()
    Register_EventHandler
End Sub

Private Sub Document_Open()
    Register_EventHandler
End Sub

Open in new window

Now you can skip step 3.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Senniger1
ID: 40523598
I tried it, but nothing happened.  

When you double click the Format Painter icon, your cursor has a little paintbrush in front of it and you can apply the same formatting to multiple places in a document simply by clicking any place in a paragraph.

In other words, once you double click the Format Painter icon your cursor has a little paintbrush in front of it and anywhere I click in the document I'll get the format I copied.  

This is what I'm trying to accomplish with code.  I want to position my cursor on a paragraph, run a macro which will double click the Format Painter icon.  Then I will manually click on other paragraphs in the document to copy the format.

Does this make sense?
0
 
LVL 27

Expert Comment

by:MacroShadow
ID: 40525014
Ouch, my bad the code for the class module should be:
Option Explicit

Public WithEvents appWord As Word.Application

Public Sub appWord_WindowBeforeDoubleClick(ByVal Sel As Selection, Cancel As Boolean)
    Sel.PasteFormat
End Sub

Open in new window


Attached is a sample.
In the sample I added a routine to stop the pasteformat.
EE-Demo-1-.zip
0
 

Author Comment

by:Senniger1
ID: 40532366
I tried to extract the EE-Demo01-.zip and it says the file is empty.
0
 
LVL 27

Expert Comment

by:MacroShadow
ID: 40532907
Must have been a problem with the download, download it again. I just tried and it works.
0
 

Author Comment

by:Senniger1
ID: 40533923
I tried downloading and extracting multiple ways and multiple times.  Attached is what I'm getting.
Error.jpg
0
 
LVL 27

Expert Comment

by:MacroShadow
ID: 40534582
Uploaded a 2003 document.
EE-Demo.doc
0
 

Author Comment

by:Senniger1
ID: 40535564
In my Sample1.doc with your macros, I position my cursor before the word "The".
   [0001] The sample...

I ran the Register_EventHandler.  I ran CopyFormat.  I selected the text in the second paragraph and nothing happens.

Going back to my original request and using my Sample2.doc, I position my cursor before the word "The".
   [0001] The sample...

I double click the Format Painter icon, now my cursor has a little paintbrush in front of it.  When I click any of the other paragraphs in my document my formatting is applied.

This is what I'm trying to accomplish with code.  I want to position my cursor on a paragraph, run a macro which will double click the Format Painter icon.  Then I will manually click on other paragraphs in the document to copy the format.

I really appreciate your help.
Sample1.doc
Sample2.doc
0
 
LVL 27

Expert Comment

by:MacroShadow
ID: 40535634
The code as is will apply formatting when you double click on text, if you want it to apply the formatting when changing the selection, locate this line:
Public Sub appWord_WindowBeforeDoubleClick(ByVal Sel As Selection, Cancel As Boolean)

Open in new window

and replace it with the following one:
Public Sub appWord_WindowSelectionChange(ByVal Sel As Selection)

Open in new window

0
 

Author Comment

by:Senniger1
ID: 40536098
Okay, that appears to be working like I need, however, if I create a new document and try to use my Format Painter icon, it now appears to be broken and doesn't work at all.
0
 
LVL 27

Expert Comment

by:MacroShadow
ID: 40537224
I don't know why the code broke the built-in format painter. I'm currently looking for a solution.
You may want to ask this as a new question so it gets some fresh eyes on it.
0
 
LVL 27

Expert Comment

by:MacroShadow
ID: 40537242
Seems to be a bug in Word 2003 which MS released a hotfix for (and later included it in SP3) http://support.microsoft.com/kb/900927
No info on 2010.
0
 

Author Comment

by:Senniger1
ID: 40540169
My original post indicates we are using Word 2010 so issues with Word 2003 may not affect me.  Can you alter the macro to work with Word 2010?

Thanks!
0
 
LVL 27

Accepted Solution

by:
MacroShadow earned 500 total points
ID: 40540249
I realize you are using 2010, the problem is I don't see that MS released a fix for this bug in 2010.
0
 

Author Closing Comment

by:Senniger1
ID: 40779053
Didn't really help my situation, but I appreciate the effort.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
This Micro Tutorial well show you how to find and replace special characters in Microsoft Word. This is similar to carriage returns to convert columns of values from Microsoft Excel into comma separated lists.
This Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question