• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 159
  • 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
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

 
cupperAuthor Commented:
Could you be more specific.
0
 
Richie_SimonettiIT OperationsCommented:
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_SimonettiIT OperationsCommented:
You are welcome and thanks for "A" grade and extra points!!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

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