我需要复制一个总是以精确字符串"p1“开头的单词,并将该单词复制到另一个文档中的表中。然后总是在完全相同的字符串之间复制一个句子,并将该句子复制到同一个表中。
让我举个例子来解释一下。下面是我需要抄袭的几段文字:
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列
p1consid
p1restles
p1somatic
第2列
SDQ: Considerate (Parent1)
SDQ: Restless (Parent1)
SDQ: Headache, stomach-ache (Parent1)
谢谢!
发布于 2018-08-07 06:28:43
这段代码中有一堆假设,但先试着这样做。
它假设源文档的格式如您所述,并且目标文档中存在一个包含2列的表。该表是第一个表,没有标题行,它是一个单行表。
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
https://stackoverflow.com/questions/51713947
复制相似问题