2010-01-05 69 views
1

你怎么了:
复制文本的范围在Excel到Word文档

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 

回答

1
+0

感谢您的回复,它是一个很大的帮助。我决定使用书签来将文本插入到word文档中。有没有将新文本插入书签并覆盖所有内容的方法?目前我正在插入文字,但无法摆脱书签中已有的文字? – Kojof 2010-01-05 15:37:38

+0

看看你是否可以在这里找到你的答案http://www.thezcorp.com/VBACodeSamples.aspx – DOK 2010-01-05 15:53:36

+0

谢谢你。这非常有帮助。我最终最终从我的VBA文件中加载了一个Word模板并将其保存为一个不同的文件。我的Word文件有书签,所以我可以从我的Excel文档插入文本到它。 – Kojof 2010-01-06 15:18:04

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/

相关问题