简体   繁体   中英

How to keep formatting while adding text to a word document with VBA Macros

I am writing a macro for copying text from a table cell in one document and inserting it in another as plane text. Though the text itself is extracting well, tabs are missing. And I have no clue how to keep formatting not keeping the table around the text.

For Each oCell In oRow.Cells

     sCellText = oCell.Range
     sCellText = Left$(sCellText, Len(sCellText) - 2)

     If (RegExp_Script.Test(sCellText) = True) Then

        num1 = RegExp_Script.Replace(sCellText, "$1")
        num2 = RegExp_Script.Replace(sCellText, "$2")

        Set docSingle = Documents.Add
        Selection.TypeText (sCellText)

        docSingle.Range.Find.Execute Findtext:="^m", ReplaceWith:=""

        strNewFileName = Replace(docMultiple.FullName, ".doc", "-e" & num1 & "_" & num2 & ".doc")
        docSingle.SaveAs strNewFileName 'save the new single-paged document

        docSingle.Close

    End If

  Next oCell

Any help would be appreciated. Thanks!

Thank you for your contribution, @CindyMeister , @Jean-Pierre . I was talking about indents, created by pressing Tab, sorry for inaccuracy. Also already solved this issue in a different way: extracted not the text from a table cell, by the very cell and removed the table borders after copying it to the new document. Like that:

oCell.Range.Copy 'copy the page into the Windows clipboard
Set docSingle = Documents.Add 'create a new document           
docSingle.Range.Paste 'paste the clipboard contents to the new document
Dim oTable As Word.Table
For Each oTable In ActiveDocument.Tables
    oTable.Rows.ConvertToText Separator:=wdSeparateByParagraphs, _
    NestedTables:=True

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM