views:

499

answers:

2

hi,

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.

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
+1  A: 

Here are some articles that may help:

Control Word from Excel using VBA in Microsoft Excel

Creating a Word Document with Excel VBA

Create formatted Word table from Excel data range

DOK
thanks for your reply, it's been a big help. I've decided to use a bookmark to do the insert the text into the word document. Is there any way of inserting the new text into bookmark and overwriting everything? Currently i'm inserting the text, but cannot get rid of the text already in bookmark?
Kojof
See if you can find your answer here http://www.thezcorp.com/VBACodeSamples.aspx
DOK
thanks for that. this was very helpful. I eventually ended up loading a Word Template from my VBA file and saving it as a different word file. My word file has bookmarks, so i can insert text from my excel document into it.
Kojof
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
Kojof
It would be OK for you to edit your original question and add this code in there. Sometimes people do that. That would bring this code sample to other people's attention better, and you could add proper code formatting. All of that would enhance your question's usefulness, which could motivate folks to vote it up.
DOK
how do you format the code? I tried using html <br>, but it did not work.
Kojof
To display text in code format, when in edit mode, you highlight the code, then click on the image with the 101 010.
DOK
A: 

Here's some code I wrote for replacing bookmark text in 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/

Dick Kusleika