2015-11-16 21 views
1

我正在写一个宏用于从一个文档中的表格单元格中复制文本并将其作为平面文本插入到另一个文档中。尽管文本本身提取得很好,但标签却不见了。而且我不知道如何保持格式不在表格周围。如何在使用VBA宏将文本添加到Word文档时保持格式化

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 

任何帮助,将不胜感激。谢谢!

+0

嗨艾琳。你能否给你的代码示例添加注释,以便我们能更好地理解需要发生的事情?我对你的RegExp_Script行有点困惑 - 这不是一个Word对象,所以它不清楚它在做什么。我们可以假设你引用的TAB字符是单元格内容的一部分吗? FWIW .Text属性仅拾取纯文本,无格式;而TypeText只能以纯文本格式输入。为了获得格式,您需要在等式两边使用FormattedText属性。就像rngTarget.FormattedText = rngSource.FormattedText –

+0

@CindyMeister我还没有与'TypeText'属性删除字符串的一部分删除选项卡的问题。 @Irene是否意味着您希望插入的文本具有与表格中相同的段落位置?例如,在表格中,大约是距离页面边距10厘米的缩进,并且您不希望它被粘贴相同的缩进? –

+0

@ Jean-Pierre:看来你解释的问题与我的不同......当然,在没有看到文档的情况下,不可能确定这些是Tab或Indent(或通过按Tab + AutoFormat创建的缩进触发)。目前还不清楚是否格式只涉及“选项卡”或其他的东西,以及... –

回答

0

谢谢你的贡献@CindyMeister @ Jean-Pierre。 我正在讨论按Tab键创建的缩进,抱歉不准确。 也已经以不同的方式解决了这个问题:不是从表格单元中提取文本,而是通过单元格提取文本,并在将文本复制到新文档后移除表格边界。 那样:

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 
相关问题