将Excel中的文本粘贴到Word中的特定位置VBA

时间:2018-08-22 14:51:39

标签: excel excel-vba

我试图将已经建立的变量(在这种情况下,变量为QN_number)放入特定位置的Word文档中,过去我这样做,只是我将整个表格放入了word中,所以代码是不同。我还没有找到直接使用变量名的过去的方法,这是首选,而不是每次都必须引用一个范围。另外,我现在使用的方法效果不佳,因为它确实粘贴在适当的位置,但是将与变量关联的单词粘贴为可以移动和调整大小的选择框。我想像文档中的其他任何文本一样粘贴。这是我现在正在使用的代码。有人有建议吗?

Sub Export_Excel_To_Word()

'Connect to M-Word
Dim wordApp As Word.Application
Set wordApp = New Word.Application
wordApp.Visible = True
Dim wordDoc As Word.Document

i = 0

'Exits loop when QN#s are finished
Do
If Cells(i + 2, 2) = "" Then
Exit Do
Else: End If


'Opens the Template Word Document For Discus Complaints
Set wordDoc = wordApp.Documents.Open(File_Name)


 'Places the QN# in the Word Document
 With Worksheets("Data").Cells(i + 2, 2)
 .Copy
wordDoc.Application.Selection.Find.Execute "Complaint Description:" '***Places after a specfic word
wordApp.Selection.MoveDown Unit:=wdLine, Count:=-1, Extend:=wdMove '***Moves the placement up a line
wordApp.Selection.TypeParagraph
'wordDoc.Paragraphs.Add
wordApp.Selection.Paste
End With
end sub

1 个答案:

答案 0 :(得分:0)

我终于找到了自己的问题的答案

首先定义变量,然后使用以下代码

'Places the QN# in the Word Document
wordDoc.Application.Selection.Find.Execute "Complaint Description:" '***Places after a specfic word
wordApp.Selection.MoveDown Unit:=wdLine, Count:=-1, Extend:=wdMove '***Moves the placement up a line
wordApp.Selection.TypeText (QN_number)