none
Preserve the format of the copied table RRS feed

  • Question

  • Here is the code.

    Sub KopirovanieDatiVEtotGeFail()
    '-------calling matched table function----------------
    Dim strFIOTable As Range
    Set strFIOTable = MatchedTable("example") 'assigning function results to a range
    '-------assigning table to a bookmark range in a new template------------
    Documents.Add Template:="c:\Users\lord\Desktop\template.dot" 'creating template for a new file
    Dim FIOTableRange As Range
    Set FIOTableRange = ActiveDocument.Bookmarks("FIOTable").Range 'assigning bookmark range
    FIOTableRange.FormattedText = strFIOTable.FormattedText 'assigning table to range contained bookmark
    End Sub
    '------------Declaring Function MatchedTable-----------------
    Function MatchedTable(strMatch As String) As Range
    For Each aTable In ActiveDocument.Tables
    'aTable.Select
    Set tmpTable = aTable.Range
    '---------Creating RegExp Object For Match A Table-------------
    Dim tableMatch As New RegExp
    tableMatch.Global = False
    tableMatch.Multiline = True
    tableMatch.IgnoreCase = True
    tableMatch.Pattern = strMatch
    '---------Beginning Matching-----------------------------------
    If tableMatch.Test(tmpTable) Then
    Set MatchedTable = aTable.Range
    Exit Function
    End If
    Next aTable
    End Function



    The problem: table formatting in FIOTableRange.FormattedText is NOT saved after placed in a new document. I cannot use copyformat because cannot understand, how to use this method. When I using copy...pasteandformat method, it works, but I cannot use copy method because need to work with variables. Thanks for yoyr help.

    And after all - can someone tell me about more fully documentation of vba (instead of vba help in VBA Office Editor)? How can I get that documentation? Especially thanks for this.
    Tuesday, September 6, 2011 6:25 AM

Answers

  • Right, then I think the problem you're dealing with has to do with Styles. Word 2003 introduced the concept of "Table styles", which complicates things even more. As with text, every table you insert is linked to a specific table style. The default that's applied can be changed within a document and also for the entire Word application. So what you see isn't necessarily what someone else is going to see if they repeat your steps.

    OK, so you've created a table and it has a default table style.

    When you put it in the destination document, if that document has a table style of the same name, that is going to override what's coming in. So one thing that can help is to define your own table style, with its own name. When the table is imported, if the style isn't in the target document, the style will also be copied.

    Next thing to take into account is that a table should always be created from a starting paragraph formatted only as "Normal" - no other formatting of any kind. Otherwise, that formatting will cancel out any foramtting you've assigned to your style for the text in table cells.

    And, yes, when you insert your table into the target document you should take care that this target range is also formatted only with the Normal style.

    For more information on table styles, I recommend some reading on Shauna Kelly's site:
    http://www.shaunakelly.com/topic/word/styles

    The book: "Word Programmierung Das Handbuch" ISBN 978-3-86645-458-3. That's the newest version and mainly concentrates on 2007/2010.

    I believe the second edition is still available, which covers mainly 2003/2007. That's ISBN 978-3-86645-414-9

    The first edition covers Word 97-2003, but I don't think that's on the market anymore.


    Cindy Meister, VSTO/Word MVP
    Thursday, September 8, 2011 3:21 PM
    Moderator

All replies

  • Hello,

     

    I do not know if it will work, but try to use:

    FIOTableRange.InsertXML(strFIOTable.FormattedText, missing)
    
    Maybe it will give you the desired result.

     

    Best regards,l

    Silviu.


    http://www.rosoftlab.net/
    Tuesday, September 6, 2011 6:41 AM
  • Please specify the version of Word involved in the problem.

    Exactly what kind of formatting "goes missing" when you use FormattedText?

    I suspect the problem may have to do with styles, most likely table styles, but without more details it's impossible to say.

    If what you need to do is "extract" tables from a "storage file" in order to insert them into other files the better solution might be to use Building Blocks (or AutoText if this is an older version). But if my guess about the foramtting is correct, there will still be the same issue to be resolved.

    <<And after all - can someone tell me about more fully documentation of vba (instead of vba help in VBA Office Editor)? How can I get that documentation?>>

    Actually, I don't know of anything better that's up-to-date, in the English language. Publishers say Word VBA books don't sell, so they don't pay authors who have in-depth knowledge of Word to write anything that delves beyond what's in Help. If you can read German I can recommend one... But it's important to realize that Word is huge and complex, therefore, so is its object model. No single book can cover all the possibilities and permutations.


    Cindy Meister, VSTO/Word MVP
    Tuesday, September 6, 2011 6:54 AM
    Moderator
  • Forget, sorry.

    It's a Word2003 VBA.

    Exactly: I want to apply format to copied table as in source file. When I move table by code above, I get the format as in destination file.

    >>Exactly what kind of formatting "goes missing" when you use FormattedText?

    For example, the table's text in a source file was normal and Arial 12, but after extracting by code above it became bold and Times New Roman 14, as in destination file. But I want to preserve source file's format.

    Will try applying normal format after extracting.

    >>If you can read German I can recommend one

    Many thanks, please, recommend me.

    Tuesday, September 6, 2011 8:38 AM
  • The alingment of text in each cell after moving to a new file is also missed.
    Wednesday, September 7, 2011 4:38 AM
  • I try use copyformat....pasteformat methods, they works, but this answer is not exactly that I want.
    Wednesday, September 7, 2011 11:14 AM
  • Right, then I think the problem you're dealing with has to do with Styles. Word 2003 introduced the concept of "Table styles", which complicates things even more. As with text, every table you insert is linked to a specific table style. The default that's applied can be changed within a document and also for the entire Word application. So what you see isn't necessarily what someone else is going to see if they repeat your steps.

    OK, so you've created a table and it has a default table style.

    When you put it in the destination document, if that document has a table style of the same name, that is going to override what's coming in. So one thing that can help is to define your own table style, with its own name. When the table is imported, if the style isn't in the target document, the style will also be copied.

    Next thing to take into account is that a table should always be created from a starting paragraph formatted only as "Normal" - no other formatting of any kind. Otherwise, that formatting will cancel out any foramtting you've assigned to your style for the text in table cells.

    And, yes, when you insert your table into the target document you should take care that this target range is also formatted only with the Normal style.

    For more information on table styles, I recommend some reading on Shauna Kelly's site:
    http://www.shaunakelly.com/topic/word/styles

    The book: "Word Programmierung Das Handbuch" ISBN 978-3-86645-458-3. That's the newest version and mainly concentrates on 2007/2010.

    I believe the second edition is still available, which covers mainly 2003/2007. That's ISBN 978-3-86645-414-9

    The first edition covers Word 97-2003, but I don't think that's on the market anymore.


    Cindy Meister, VSTO/Word MVP
    Thursday, September 8, 2011 3:21 PM
    Moderator
  • Thanks for your recommends. I'll try the table styles.
    Friday, September 9, 2011 5:56 AM