Joke Collection Website - Blessing messages - Pass EXCEL data to WORD through VBA.

Pass EXCEL data to WORD through VBA.

Put excel and word in the same directory,

Create a button in excel, double-click it and enter the following code:

Private subcommand button 1_Click ()

Turn off screen refresh.

"Continue to the next step when an error occurs" to catch the error.

Dim oSt is Range and wdDoc is Word. Document, wdRange is Word. range

myPath = ThisWorkbook。 Path and path. "\2.doc" ""defines the word file path, and the name is modified by myself. I set it to 2.doc.

Set wdDoc = GetObject(myPath)' open word.

Dim key(2)' defines an array,

Key( 1) = "abcdefg "'Data to be replaced.

key(2) = "hijklmn "

Set wdRange = wdDoc. Content provides the document content of word to wdrange.

For the period i = 1 to 2'

Use wdRange. find

. Text = key(i)' search

. replacement . text = key(I)& amp; IIF (I = 1, cell (1, 1). Value, cell (5, 2). Value)'

. Forward = true

. Wrap = wdFindContinue

. Format = false

. MatchCase = False

. MatchWholeWord = False

. MatchByte = True

. Match wildcard = False

. MatchSoundsLike = False

. MatchAllWordForms = False

End with ...

WD range. Find it. execute Replace:= WD Replace All ' Replace All。

then

WdDoc。 Save the save word.

WdDoc。 Close the close word.

Set wdDoc = Nothing.

Application.screenupdating = true' opens screen refresh.

End joint

After testing, it has met the requirements of the landlord. Please add points to adopt. Ha ha.