locked
Adding Copy-Paste options to a template or achieving the same goal with a Macro. RRS feed

  • Question

  • Dear Forum,

     

    This question has been asked on the Format Style section of this Forum but they suggested I should come here.

    Lately I'm having this problem with copy paste options (with regards to format) and templates.

    Here is the objective:

    To adjust the style and format of text in a couple of templates to always be like that. In other words, I want the style and font at each location to remain the same. A template does this fine as you can make sure that it is saved in a specific font, however, when copy pasting this goes south.

    I use headers in the document but when I try to copy paste, something into a document based on the template, its format changes.

    The option in Word to change the copy-paste content to that of the document template kind of works. HOWEVER, it causes problems with other documents. I do not want that option to be there in every document. I need that option only in that template. 

    How I think this problem could possibly be solved: (just spinning my wheels)

    Word has the option to keep the source format or use the format of the document to say which format to use. [[ Word options > advanced > when copy pasting, use source of keep text etc ]] However, if I set this option it would mean that it would use that for ALL my documents, I just want it to do that for the template and documents based on it. Giving this option to the template with a macro at start could be the trick..

    Im quite sure this option is hidden somewhere within word, I just do not know where to find it!

    Another option would be that each field within the document (I work with cells and tables only in this document) would have a specific font and font type (inc special colour) allowed. Afterwards this could be checked by a macro to see if it conforms to the style for that cell or not.

     ## (like auto format, only, it needs to go with that document and be executed afterwards!).
    I tried looking for code on this, see below.

     

     

    What I have tried so far:

    - Recording a Macro that tried to change the headers of the document afterwards. However, the Record a Macro function is probably not made for this as it "Jumps" trough the document changing different cells to styles, which I did not, told him to do so. (I think this has to do with navigation over a large document.)

    - Changing that option in word (However, it now does that for all my documents, not a salutation.)

    - Going trough the Microsoft help documents and tutorials. Couldn’t find it.

    - Started learning Visual Basic again to hope to gain insight into Microsoft's Word Programming code..

     

    Could someone help me out?

     

    Thanks a lot!

     Christiaan,

    PS: its not only for 2007 word, but for 2003 aswell. My school uses diffrent formats because of the teachers unwilling to upgrade...

    Having the Quick paste menu pop up under your mouse asking you what to do, defeats the point of a template! This is also not an option.

     

    Im looking ever more in Macro's and found this:

     

    With Me .Tables.Item(1).Cell(1, 1).Range

      .Text = "Name"

      .ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight

    End With

    Coudnt there be a Macro that went trough each table (I only have tables..) and then manually formatted them to set values? Say New Times Roman ft.11

    Would look something like

    Tables.Item(1).Cell(1, 1)

    .Format = Word.Font.Bold = wdToggle

    .Font.Name = "Arial Black"

     

     

    Wednesday, October 20, 2010 7:40 AM

Answers

  • This code will allow you to go through each table in your document and select each cell in the table.  The difficult bit is the formatting to be applied at the specific location.  As it appears to be different in different cells you will need to use the row(x )and (y) coordinates to identify what formats are applied where.  (Maybe a Case statement on x and y combined)

    Sub CellSelect()
        Dim tcount, ymax, xmax, x, y, t  As Integer
       
        tcount = ActiveDocument.Tables.Count

        t=1
       
        Do While t <= tcount
           
            Set mytable = ActiveDocument.Tables(tcount)
           
            x = 1

            ymax = mytable.Columns.Count
            xmax = mytable.Rows.Count
           
            Do While x <= xmax
                y = 1
                Do While y <= ymax
         
                    mytable.Cell(x, y).Select
                   
                    With Selection
                   
    ' formatting instructions go here
                   
                    End With
                    y = y + 1
                Loop
               
                x = x + 1
            Loop
           
        t = t + 1
        Loop
         
        Set mytable = Nothing
    End Sub


    G North MMI
    • Marked as answer by Jennifer Zhan Monday, October 25, 2010 1:37 AM
    Wednesday, October 20, 2010 11:33 AM
  • You can ensure that anything pasted into a document based on this specific template will use this template's style definitions, without changing the option for other documents (even ones that are currently open).

    Place this macro into a module in the template you want to affect. It intercepts the Paste command -- but only in documents based on this template -- and changes the option to the desired value. After doing the pasting, it restores the previous value of the option, whatever it may have been.

    Sub EditPaste()
        Dim origPasteOpt As WdPasteOptions
        origPasteOpt = Options.PasteFormatBetweenStyledDocuments
        Options.PasteFormatBetweenStyledDocuments = wdUseDestinationStyles

        Selection.Paste

        Options.PasteFormatBetweenStyledDocuments = origPasteOpt
    End Sub

    Note that Options.PasteFormatBetweenDocuments (without reference to conflicting style definitions) and Options.PasteFormatWithinDocument are separate options from the one in the macro. The macro could be expanded to set/reset those as well.


    Jay Freedman
    MS Word MVP  FAQ: http://word.mvps.org
    • Marked as answer by Jennifer Zhan Monday, October 25, 2010 1:38 AM
    Wednesday, October 20, 2010 2:06 PM

All replies

  • From where is the data coming that you want to insert into the document?

    Depending upon exactly what it is that you are doing, if you used docvariable fields in the template at the locations where you want the data inserted, you could use the \* charformat switch on those fields to set the formatting of the data that would be displayed in those fields and you could use code to set the values of variables in the documents created from the template to the data that you are copying and have the code update the fields so that the information is displayed in them.

    -- Hope this helps.

    Doug Robbins - Word MVP,
    dkr[atsymbol]mvps[dot]org
    Posted via the Community Bridge

    "C.verhoef" wrote in message news:639da091-36ae-487a-8f53-2e7e32723759@communitybridge.codeplex.com...

    *Dear Forum, *

    This question has been asked on the Format Style section of this Forum but they suggested I should come here.

    *Lately I'm having this problem with copy paste options (with regards to format) and templates. *

    *Here is the objective: *

    *To adjust the style and format of text in a couple of templates to always be like that. In other words, I want the style and font at each location to remain the same. A template does this fine as you can make sure that it is saved in a specific font, however, when copy pasting this goes south. *

    *I use headers in the document but when I try to copy paste, something into a document based on the template, its format changes. *

    *The option in Word to change the copy-paste content to that of the document template kind of works. HOWEVER, it causes problems with other documents. I do not want that option to be there in every document. I need that option only in that template.  *

    *How I think this problem could possibly be solved: (just spinning my wheels) *

    *Word has the option to keep the source format or use the format of the document to say which format to use. [[ Word options > advanced > when copy pasting, use source of keep text etc ]] However, if I set this option it would mean that it would use that for ALL my documents, I just want it to do that for the template and documents based on it. Giving this option to the template with a macro at start could be the trick..*

    *Im quite sure this option is hidden somewhere within word, I just do not know where to find it! *

    Another option would be that each field within the document (I work with cells and tables only in this document) would have a specific font and font type (inc special colour) allowed. Afterwards this could be checked by a macro to see if it conforms to the style for that cell or not.

    *## (like auto format, only, it needs to go with that document and be executed afterwards!).
    I tried looking for code on this, see below.*

    *  *

    *  *

    What I have tried so far:

    - Recording a Macro that tried to change the headers of the document afterwards. However, the Record a Macro function is probably not made for this as it "Jumps" trough the document changing different cells to styles, which I did not, told him to do so. (I think this has to do with navigation over a large document.)

    - Changing that option in word (However, it now does that for all my documents, not a salutation.)

    - Going trough the Microsoft help documents and tutorials. Couldn’t find it.

    - Started learning Visual Basic again to hope to gain insight into Microsoft's Word Programming code..

    *Could someone help me out? *

    *Thanks a lot! *

    Christiaan,

    *PS: its not only for 2007 word, but for 2003 aswell. My school uses diffrent formats because of the teachers unwilling to upgrade... *

    */Having the Quick paste menu pop up under your mouse asking you what to do, defeats the point of a template! This is also not an option. /*

    Im looking ever more in Macro's and found this:

    With Me .Tables.Item(1).Cell(1, 1).Range

      .Text = "Name"

      .ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight

    End With

    Coudnt there be a Macro that went trough each table (I only have tables..) and then manually formatted them to set values? Say New Times Roman ft.11

    Would look something like

    Tables.Item(1).Cell(1, 1)

    Format = Word.Font.Bold = wdToggle

    Font.Name = "Arial Black"


    Doug Robbins - Word MVP dkr[atsymbol]mvps[dot]org
    Wednesday, October 20, 2010 7:59 AM
  • Dear Doug Robbins ,

     

    Thank you very much for your quick reply's, I feel honored that two highly skilled forum members are assisting me with my problem.

     

    However, I do not use "docvariable fields", or at least, not that I know of. Though, this does sound very intresting, Do you have more information on these "docvariable fields"? Perhaps I can take a look at it once this other thing is sorted.

    Do you have any other suggestions? To prevent a misunderstanding: I am VERY GREATFUL for your assistance!

    The Macro that I'm building with graham mayor looks very promising so far! And should help the problem completly. Also, its a very nice programming expierance for me (something I lack).

    • Edited by C.verhoef Wednesday, October 20, 2010 10:11 AM
    Wednesday, October 20, 2010 8:53 AM
  • Dear Doug Robbins

     

    Do you prehaps know how to adress cells in a Macro?

    My google seaches have so far turned me empty handed..

    okayy... its not Set oTable = ActiveDocument.Tables(1).Cell(Row:=1, Column:=1)

     

    Kind regards,

     

    Christiaan Verhoef

    Wednesday, October 20, 2010 9:28 AM
  • This code will allow you to go through each table in your document and select each cell in the table.  The difficult bit is the formatting to be applied at the specific location.  As it appears to be different in different cells you will need to use the row(x )and (y) coordinates to identify what formats are applied where.  (Maybe a Case statement on x and y combined)

    Sub CellSelect()
        Dim tcount, ymax, xmax, x, y, t  As Integer
       
        tcount = ActiveDocument.Tables.Count

        t=1
       
        Do While t <= tcount
           
            Set mytable = ActiveDocument.Tables(tcount)
           
            x = 1

            ymax = mytable.Columns.Count
            xmax = mytable.Rows.Count
           
            Do While x <= xmax
                y = 1
                Do While y <= ymax
         
                    mytable.Cell(x, y).Select
                   
                    With Selection
                   
    ' formatting instructions go here
                   
                    End With
                    y = y + 1
                Loop
               
                x = x + 1
            Loop
           
        t = t + 1
        Loop
         
        Set mytable = Nothing
    End Sub


    G North MMI
    • Marked as answer by Jennifer Zhan Monday, October 25, 2010 1:37 AM
    Wednesday, October 20, 2010 11:33 AM
  • You can ensure that anything pasted into a document based on this specific template will use this template's style definitions, without changing the option for other documents (even ones that are currently open).

    Place this macro into a module in the template you want to affect. It intercepts the Paste command -- but only in documents based on this template -- and changes the option to the desired value. After doing the pasting, it restores the previous value of the option, whatever it may have been.

    Sub EditPaste()
        Dim origPasteOpt As WdPasteOptions
        origPasteOpt = Options.PasteFormatBetweenStyledDocuments
        Options.PasteFormatBetweenStyledDocuments = wdUseDestinationStyles

        Selection.Paste

        Options.PasteFormatBetweenStyledDocuments = origPasteOpt
    End Sub

    Note that Options.PasteFormatBetweenDocuments (without reference to conflicting style definitions) and Options.PasteFormatWithinDocument are separate options from the one in the macro. The macro could be expanded to set/reset those as well.


    Jay Freedman
    MS Word MVP  FAQ: http://word.mvps.org
    • Marked as answer by Jennifer Zhan Monday, October 25, 2010 1:38 AM
    Wednesday, October 20, 2010 2:06 PM