Solved

Writing to an MS word document from VB

Posted on 2002-06-06
8
147 Views
Last Modified: 2010-05-02
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
Comment
Question by:cupper
8 Comments
 

Expert Comment

by:HobbitHouse
Comment Utility
there's a "range" thing you have to set to the end of the text you just inserted, I think is your problem
0
 

Author Comment

by:cupper
Comment Utility
Could you be more specific.
0
 

Author Comment

by:cupper
Comment Utility
Could you be more specific.
0
 

Author Comment

by:cupper
Comment Utility
Could you be more specific.
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 16

Accepted Solution

by:
Richie_Simonetti earned 125 total points
Comment Utility
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
 
LVL 28

Expert Comment

by:iboutchkine
Comment Utility
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
 

Author Comment

by:cupper
Comment Utility
Richie,

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

cupper
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
Comment Utility
You are welcome and thanks for "A" grade and extra points!!
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now