Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
MSDN Visual Basic Community
 
 
 
 
 
 
TitlePaste a picture into a Word file
KeywordsOffice, Word, picture, paste
CategoriesOffice
 
Open Word as a server. Make the server object open the file and go to the point where you want the image. Use the Clipboard object to copy the image to the clipboard. Then call the Word server's Select.Paste method.

Note that this project contains a reference to the Word object library. You may need to use a different reference for your version of Word.

 
Private WordServer As Word.Application

Private Sub Command1_Click()
Dim file_name As String
Dim file_path As String
Dim file_title As String
Dim txt As String
Dim new_txt As String
Dim pos As Integer

    Screen.MousePointer = vbHourglass
    Command1.Enabled = False
    DoEvents

    ' Open Word.
    file_name = txtFilename.Text
    file_title = Mid$(file_name, InStrRev(file_name, "\") + _
        1)
    file_path = Left$(file_name, Len(file_name) - _
        Len(file_title))

    ' Uncomment to show Word.
'    WordServer.Visible = True

    WordServer.ChangeFileOpenDirectory file_path
    WordServer.Documents.Open _
        FileName:=file_title, _
        ConfirmConversions:=False, _
        ReadOnly:=False, _
        AddToRecentFiles:=False, _
        PasswordDocument:="", _
        PasswordTemplate:="", _
        Revert:=False, _
        WritePasswordDocument:="", _
        WritePasswordTemplate:="", _
        Format:=wdOpenFormatAuto

    ' Go to the bookmark.
    WordServer.Selection.GoTo _
        What:=wdGoToBookmark, _
        Name:="Disclaimer"
    WordServer.Selection.Find.ClearFormatting
    With WordServer.Selection.Find
        .Text = ""
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With

    ' Copy the image to the clipboard.
    Clipboard.Clear
    Clipboard.SetData Picture1.Picture, vbCFBitmap

    ' Paste the image into Word.
    WordServer.Selection.Paste

    ' Comment out to keep Word running.
    WordServer.Quit True
    Set WordServer = Nothing

    Screen.MousePointer = vbDefault
    Command1.Enabled = True
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated