Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 155
  • Last Modified:

Writing to an MS word document from VB

I am trying to write to an MS Word document by repeatedly copying something to the clipboard and pasting it to an open Word document. I am using the following


Dim msWord As New Word.Application
Dim msWordDoc As New Word.Document

On Error Resume Next

 Set msWord = GetObject(, "Word.Application") 'look for a running copy of Excel
'
If Err.Number <> 0 Then 'If Excel is not running then
    Set msWord = CreateObject("Word.Application") 'run it
End If
   
msWord.Visible = True


Set msWordDoc = msWord.Documents.Open(fileinfo.WordFile)


<Copy something to clipboard>

msWordDoc.Content.Paste

<Copy something to clipboard>

msWordDoc.Content.Paste

<Copy something to clipboard>

msWordDoc.Content.Paste


The trouble is that everytime I paste, it just replaces the previous image. I haven't used MS word in VB before.

Any suggestions?

0
cupper
Asked:
cupper
1 Solution
 
HobbitHouseCommented:
there's a "range" thing you have to set to the end of the text you just inserted, I think is your problem
0
 
cupperAuthor Commented:
Could you be more specific.
0
 
cupperAuthor Commented:
Could you be more specific.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
cupperAuthor Commented:
Could you be more specific.
0
 
Richie_SimonettiCommented:
Try with this (besides your code):
Dim myRange As Range

Set myRange = ActiveDocument.Sections(1).Range
With myRange
    .MoveEnd Unit:=wdCharacter, count:=-1
    .Collapse Direction:=wdCollapseEnd
    Selection.TypeParagraph
    .Paste
End With
0
 
iboutchkineCommented:
Dim mydoc as Word.Document

set mydoc = new Word.Document
mydoc.Range.Paste
mydoc.SaveAs "c:/temp/Mydoc.doc"
mydoc.Close
set mydoc = nothing


0
 
cupperAuthor Commented:
Richie,

Thank you much for your help!  An extra 25 points for your timely response.

cupper
0
 
Richie_SimonettiCommented:
You are welcome and thanks for "A" grade and extra points!!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now