Solved

VBA Code for Word 2010 Format Painter

Posted on 2014-12-24
17
300 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
  • 9
  • 8
17 Comments
 
LVL 26

Expert Comment

by:MacroShadow
Comment Utility
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
Comment Utility
Thank you for that information.

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

Expert Comment

by:MacroShadow
Comment Utility
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
 

Author Comment

by:Senniger1
Comment Utility
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 26

Expert Comment

by:MacroShadow
Comment Utility
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
Comment Utility
I tried to extract the EE-Demo01-.zip and it says the file is empty.
0
 
LVL 26

Expert Comment

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

Author Comment

by:Senniger1
Comment Utility
I tried downloading and extracting multiple ways and multiple times.  Attached is what I'm getting.
Error.jpg
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 26

Expert Comment

by:MacroShadow
Comment Utility
Uploaded a 2003 document.
EE-Demo.doc
0
 

Author Comment

by:Senniger1
Comment Utility
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 26

Expert Comment

by:MacroShadow
Comment Utility
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
Comment Utility
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 26

Expert Comment

by:MacroShadow
Comment Utility
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 26

Expert Comment

by:MacroShadow
Comment Utility
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
Comment Utility
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 26

Accepted Solution

by:
MacroShadow earned 500 total points
Comment Utility
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
Comment Utility
Didn't really help my situation, but I appreciate the effort.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
In this video, we show how to convert an image-only PDF file into a PDF Searchable Image file, that is, a file with both the image (typically from scanning) and text, which is created in an automated fashion with Optical Character Recognition (OCR) …
This video shows the viewer how to set up and create Footnotes in their document. Click on the References tab: Select "Insert Footnote": Type in desired text:

762 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now