Solved

MS_Word vba: Placing a .jpg graphic at specific positions in text; all appended automatically

Posted on 2014-11-21
3
198 Views
Last Modified: 2014-11-21
I have a multi-step process in Excel that needs to be reported to a Word file.
Each step has a characteristic graphic icon, which is to be followed by text reporting parameters for that  step.
The data and graphic details are read from arrays sequentially, so I think the Word doc should grow by appendation.

So the layout would be:
Step A Graphic (say 40x40 in size)
       Step A parameters .....

Step B Graphic (say 40x40 in size)
       Step B parameters .....

etc.

Though I will need to use Excel in early binding with MW Word to create the Word report, at present I'm concentrating on using Word vba ALONE to place graphics in a word file, and I'm finding difficulties.

For Text, I'm using FileSystemObject.TextStream.Write with vba in excel and writing to a doc file:

Sub appendRecordReport_txt()
    Dim fso As New FileSystemObject
    Dim stream As TextStream

    Set stream = fso.OpenTextFile("c:\tstTextFiles\testfile.doc", 8, TristateFalse) 'where 8 = ForAppending ...
    stream.Write "This line uses the Write method nov20."                      '.... and appends this text
    stream.Close
End Sub

For Graphic, (using vba in word right now) I have got the following, which I've run after simply clicking the word page to which the graphic is to be sent:
       Dim shp As Shape
       With ActiveDocument
              Set shp = ActiveDocument.Shapes.AddPicture(fileName, msoFalse, msoTrue)
       end with

However, though i have code to size, crop, and position the graphic, I these parameters interact and make it difficult to work out how they work, or even whether I've selected the right starting points:
             shp.Top
             shp.Left
             .PictureOffsetX = 15 'picWidth / 10
             .PictureOffsetY = 16 'picHeight / 10
            .PictureHeight = picHeight * 0.5
            .PictureWidth = picWidth * 0.5
           
            ' Now modify the cropping by changing the shape of the container. Changing the shape alters
            ' the portion of the picture that you see:
            .ShapeHeight = 140
            .ShapeWidth = 140
            .ShapeLeft = 110
            .ShapeTop = 120  

(sourced from: https://code.msdn.microsoft.com/office/Word-2010-Add-Picture-ddb5c728)

My first Question is: Have I selected the right methods for text and graphic, and can you help me with the principles of placing graphics (sized) at the right point in text?  Issues such as text margins and font size can follow later.

I hope you can help me!

Thanks
Kelvin
0
Comment
Question by:Kelvin4
  • 2
3 Comments
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 500 total points
ID: 40458843
Personally, I don't know how well FSO can interpret Word objects and methods, so I stick to coding from the Word object library.

You should be aware that pictures can be represented in Word as two entirely different objects and processes, though the Word GUI somewhat blurs that. In terms of the coding, pictures and other graphical objects can be considered as Shape or as InLineShape objects. In the latter, pictures, and other graphical objects are treated as if they were characters (albeit probably big) with a fixed position in the text, which might be what you need here

Shape objects, on the other hand are merely 'anchored' to a particular text paragraph. In the Word application interface they can be dragged about, though their final position in any printout is subject to particular rules that can depend on the outcome of pagination. The 'wrapping' setting determines how the text reacts to the presence of the shape.
0
 

Author Comment

by:Kelvin4
ID: 40458960
GrahamSkan, that got me started..
At this source: http://technet.microsoft.com/en-us/library/ee692911.aspx , which seems to follow your advice.
After replacing   objSelection.TypeParagraph() with   objSelection.TypeParagraph, and adding SaveAs,

The resultant code worked for me, thanks!

Sub inLine1()
    Dim wrdApp As Word.Application
    Dim objDoc As Word.Document
    Dim objShape As Word.InlineShape
    Dim objSelection As Word.Selection
   
    Set wrdApp = CreateObject("Word.Application")
    wrdApp.Visible = True
   
    Set objDoc = wrdApp.Documents.Add()

    Set objSelection = wrdApp.Selection

    objSelection.TypeText "Here is a sentence preceding the picture."
    objSelection.TypeParagraph

    'Set objShape = objSelection.InlineShapes.AddPicture("D:\aaThink_Build\Thk_Building\xlImages\cross.jpg")
    Set objShape = objSelection.InlineShapes.AddPicture("D:\aaThink_Build\Thk_Building\xlImages\openVisio.jpg")
    objShape.Height = 50
    objShape.Width = 50
    objSelection.TypeParagraph
    objSelection.TypeText "Here is a sentence following the picture."
    objSelection.TypeParagraph
   
    objDoc.SaveAs ("C:\tstTextFiles\myFile inLine in folder")
   
End Sub
0
 

Author Closing Comment

by:Kelvin4
ID: 40458964
Thanks for very rapid guidance that helped me get started.
Kelvin
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

When creating Microsoft Word-based forms there may be a need to have a form field repeated throughout the whole document. For instance, with a company name, you may want this information repeated automatically throughout the document rather than man…
This article describes how to use the Send to Mail Recipient command. The instructions apply generally to Office 2007 and later versions, but Microsoft® Word 2013 was used for the specific steps and figures.  What is Send to Mail Recipient? Send…
The viewer will learn how to make their project stand out over others by learning how to change colors and shapes, add spaces, change directions, and add bullets to their charts.
This Micro Tutorial well show you how to find and replace special characters in Microsoft Word. This is similar to carriage returns to convert columns of values from Microsoft Excel into comma separated lists.

914 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

13 Experts available now in Live!

Get 1:1 Help Now