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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2006077/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 11:10:39  来源:igfitidea点击:

Copy Text from Range in Excel into Word Document

excelvbams-wordexcel-vbaword-vba

提问by Kojof

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.

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

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

回答by Dick Kusleika

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

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

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