如何将MS Excel中的单元格的值复制到MSWord文件的VB字段中?

我需要在MS Word 2003中有一个VOB代码,复制Excel文件中的特定单元格,并将其粘贴到单词(字段)中。 以下是我所做的,并导致错误。

Sub cmdGetNumber() Dim XL As Object Dim WBEx As Object Dim ExelWS As Object Dim appwd As Object Dim wdApp As Word.Application '''' 'On Error GoTo OLE_ERROR Set XL = CreateObject("Excel.Application") Set wdApp = CreateObject("Word.Application") 'Open Excel document Set WBEx = XL.Workbooks.Open("C:\Documents and Settings\121567\Desktop\tafket1.xls") Set ExelWS = WBEx.Worksheets("Sheet1") XL.Visible = True 'appwd.Visible = True ExelWS.Range("c2").Select 'Selection.Copy 'wdApp.Selection.PasteSpecial Placement:=wdInLine, DataType:=wdPasteMetafilePicture 'wdApp.Documents.Save Set wdApp = Nothing Set ExelWS = Nothing Set WBEx = Nothing End Sub 

由于这个macros是在Word中,所以你不需要显式地打开一个单词实例。 你可以做Documents.Add来添加一个新的文档,或者Documents.Open来打开一个已经存在的Documents.Open

尝试这个:

 Sub cmdGetNumber() Dim XL As Object Dim WBEx As Object Dim ExelWS As Object Dim wdDoc As Word.Document 'On Error GoTo OLE_ERROR Set XL = CreateObject("Excel.Application") 'Open Excel document Set WBEx = XL.Workbooks.Open("C:\Documents and Settings\121567\Desktop\tafket1.xls") Set ExelWS = WBEx.Worksheets("Sheet1") 'XL.Visible = True ExelWS.Range("C2").Copy Set wdDoc = Documents.Add wdDoc.Activate wdDoc.Select Selection.Paste WBEx.Close XL.Quit Set WBEx = Nothing Set ExelWS = Nothing Set XL = Nothing End Sub 

上面的代码将打开你的Excel文件,复制单元格C2,然后打开一个新的Word文档,并粘贴在那里。

我看到你提到了一个(filed)在你的问题。 你是指一个Field还是一个File ? 如果它是一个Field那么你可能想用相关的字段名称replaceSelection.Paste