VBA Code for Word 2010 Format Painter

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.
Senniger1Asked:
Who is Participating?
 
MacroShadowConnect With a Mentor Commented:
I realize you are using 2010, the problem is I don't see that MS released a fix for this bug in 2010.
0
 
MacroShadowCommented:
Selection.CopyFormat will only copy the format of selected text, Selection.PasteFormat will apply that formatting to the new selection.
0
 
Senniger1Author Commented:
Thank you for that information.

I need the visual basic code for double-clicking the Format Painter button.
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
MacroShadowCommented:
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
 
Senniger1Author Commented:
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
 
MacroShadowCommented:
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
 
Senniger1Author Commented:
I tried to extract the EE-Demo01-.zip and it says the file is empty.
0
 
MacroShadowCommented:
Must have been a problem with the download, download it again. I just tried and it works.
0
 
Senniger1Author Commented:
I tried downloading and extracting multiple ways and multiple times.  Attached is what I'm getting.
Error.jpg
0
 
MacroShadowCommented:
Uploaded a 2003 document.
EE-Demo.doc
0
 
Senniger1Author Commented:
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
 
MacroShadowCommented:
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
 
Senniger1Author Commented:
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
 
MacroShadowCommented:
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
 
MacroShadowCommented:
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
 
Senniger1Author Commented:
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
 
Senniger1Author Commented:
Didn't really help my situation, but I appreciate the effort.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.