Solved

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

Posted on 2014-11-21
3
204 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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

Introduction This tutorial provides instructions on how to properly format your Word document using the inbuilt tools provided. The benefits of using these tools means your documents are more accessible and easily portable to other applications an…
I would like to show you some basics you can do with Mailings in MS Word. It´s quite handy feature you can use for creating envelopes, labels, personalized letters etc. First question could be what is this feature good for? Mailing can really he…
Learn how to create and modify your own paragraph styles in Microsoft Word. This can be helpful when wanting to make consistently referenced styles throughout a document or template.
Office 365 is currently available in five editions. Three of them are for business use: Office 365 Business Essentials, Office 365 Business, and Office 365 Business Premium. Two of them are for home/personal use: Office 365 Home and Office 365 Perso…

778 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