Hi, I'm trying to copy the contents of a Word document and paste it into another. Does anybody knows how to do that with the Word Object library or any other way ?? Thanks
Printable View
Hi, I'm trying to copy the contents of a Word document and paste it into another. Does anybody knows how to do that with the Word Object library or any other way ?? Thanks
Here is an example of Peets code adapted:
VB Code:
Option Explicit Private Sub Command1_Click() Dim objWord As Object 'instead of Word.Application Dim wd As Object 'instead of Word.Document On Error GoTo Err_Handler If objWord Is Nothing Then Set objWord = CreateObject("Word.Application") Else Set objWord = GetObject(, "Word.Application") End If DoEvents 'Open you first Document Set wd = objWord.Documents.Open("c:\FirstDoc.doc") wd.ActiveWindow.Selection.WholeStory wd.ActiveWindow.Selection.Copy 'Open you second Document Set wd = Nothing Set wd = objWord.Documents.Open("c:\SecondDoc.doc") wd.ActiveWindow.Selection.Paste objWord.Visible = True 'Show both Documents Exit Sub Err_Handler: 'quit word If Not (wd Is Nothing) Then Set wd = Nothing If Not (objWord Is Nothing) Then objWord.Application.Quit If Not (objWord Is Nothing) Then Set objWord = Nothing MsgBox "Number: " & Err.Number & vbCrLf & _ "Description: " & Err.Description, vbOKOnly + vbCritical, "Error!" End Sub
Thank you very much :) . But just wondering is there a difference if I declare directly as Word.Apliication and same for document ??
Probably so you don't need a Reference to MSWord Object Library.Quote:
Originally posted by maxl
Thank you very much :) . But just wondering is there a difference if I declare directly as Word.Apliication and same for document ??
(If you use "Dim oWd as Word.Application", then you'll need the Lib reference).
Bruce.