Copy Text from Range in Excel into Word Document

柔情痞子 提交于 2019-11-27 07:29:03

问题


how do you:

1) copy text from a range in an Excel Document.
2) Open a Word Document.
3) inserts the text into a specific part of the word document.

regards

Kojo

Edit: here is the approach

Dim wrdApp As Word.Application 
Dim wrdDoc As Word.Document 
Dim j As Integer 
Set wrdApp = CreateObject("Word.Application") 
wrdApp.Visible = True 
Set wrdDoc = wrdApp.Documents.Open("C:\Files\DailyStrategy.doc") 

With wrdDoc 
   If wrdDoc.Bookmarks.Exists("MarketCommentry") 
      Then wrdDoc.Bookmarks("MarketCommentry").Range.Text = shortString 
      wrdDoc.SaveAs "c:\temp\test.doc" 
   End If 
   ' close the document 
   Set wrdDoc = Nothing 
   Set wrdApp = Nothing 
End With

回答1:


Here are some articles that may help:

Control Word from Excel using VBA in Microsoft Excel

Creating a Word Document with Excel VBA

Create formatted Word table from Excel data range




回答2:


Here's some code I wrote for replacing bookmark text in Word

Sub FillBookmark(ByRef wdDoc As Object, _
    ByVal vValue As Variant, _
    ByVal sBmName As String, _
    Optional sFormat As String)

    Dim wdRng As Object

    'store the bookmarks range
    Set wdRng = wdDoc.Bookmarks(sBmName).Range

    'if the optional format wasn’t supplied
    If Len(sFormat) = 0 Then
        'replace the bookmark text
        wdRng.Text = vValue
    Else
        'replace the bookmark text with formatted text
        wdRng.Text = Format(vValue, sFormat)
    End If

    're-add the bookmark because the above destroyed it
    wdRng.Bookmarks.Add sBmName, wdRng

End Sub

More details here

http://www.dailydoseofexcel.com/archives/2004/08/13/automating-word/



来源:https://stackoverflow.com/questions/2006077/copy-text-from-range-in-excel-into-word-document

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!