首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >MS Word。如何将精确的字符串复制到另一个文档?

MS Word。如何将精确的字符串复制到另一个文档?
EN

Stack Overflow用户
提问于 2018-08-07 02:51:28
回答 1查看 63关注 0票数 -1

我需要复制一个总是以精确字符串"p1“开头的单词,并将该单词复制到另一个文档中的表中。然后总是在完全相同的字符串之间复制一个句子,并将该句子复制到同一个表中。

让我举个例子来解释一下。下面是我需要抄袭的几段文字:

代码语言:javascript
复制
variable labels p1consid 'SDQ: Considerate (Parent1)'.

variable labels p1restles 'SDQ: Restless (Parent1)'.

variable labels p1somatic 'SDQ: Headache, stomach-ache (Parent1)'.

因此,"p1consid“应该转到表的第1列,"SDQ: Considerate (Parent1)”应该转到同一表的第2列。

第1列

代码语言:javascript
复制
p1consid

p1restles

p1somatic

第2列

代码语言:javascript
复制
SDQ: Considerate (Parent1)

SDQ: Restless (Parent1)

SDQ: Headache, stomach-ache (Parent1)

谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-08-07 06:28:43

这段代码中有一堆假设,但先试着这样做。

它假设源文档的格式如您所述,并且目标文档中存在一个包含2列的表。该表是第一个表,没有标题行,它是一个单行表。

代码语言:javascript
复制
Sub CopyStrings()
Dim docSrc As word.Document, docDst As word.Document
Dim rng As word.Range, tbl As word.Table, tRng As word.Range
Set docSrc = Documents.Open("Your Source Doc")
Set docDst = Documents.Open("Your Destination Doc")
Set rng = docSrc.Content
Set tbl = docDst.Content.Tables(1)
With rng.Find
    .ClearFormatting
    .Format = False
    .Forward = True
    .Text = "p1"
    .Wrap = wdFindStop
    .Execute
    Do While .found
        rng.MoveEnd word.WdUnits.wdWord, Count:=1
        Set tRng = tbl.rows(1).Cells(1).Range
        tRng.MoveEnd word.WdUnits.wdCharacter, Count:=-1
        tRng.Collapse word.WdCollapseDirection.wdCollapseEnd
        tRng.Text = rng.Text & vbCr
        rng.Collapse word.WdCollapseDirection.wdCollapseEnd
        rng.MoveStart word.WdUnits.wdWord, Count:=1
        rng.MoveStart word.WdUnits.wdCharacter, Count:=1
        rng.MoveEnd word.WdUnits.wdParagraph, Count:=1
        Set tRng = tbl.rows(1).Cells(2).Range
        tRng.MoveEnd word.WdUnits.wdCharacter, Count:=-1
        tRng.Collapse word.WdCollapseDirection.wdCollapseEnd
        tRng.Text = rng.Text
        rng.Collapse word.WdCollapseDirection.wdCollapseEnd
        .Execute
    Loop
End With
End Sub
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51713947

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档