将Excel中的范围中的文本复制到Word文档中

时间:2010-01-05 12:49:23

标签: excel vba ms-word excel-vba word-vba

你怎么样:

1)从Excel文档中的范围复制文本 2)打开Word文档。
3)将文本插入word文档的特定部分。

问候

科乔

编辑:这是方法

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

2 个答案:

答案 0 :(得分:1)

答案 1 :(得分:0)

这是我为替换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

此处有更多详情

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

相关问题