Solved

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

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Nice table. Huge mess. Maybe this was something you created way back before you figured out tabs or a document you received from someone else. Either way, using the spacebar to separate the columns resulted in a mess. Trying to convert text to t…
Using Word 2013, I was experiencing some incredible lag when typing.  Here's what worked for me....
This video walks the viewer through the process of creating envelopes and labels, with multiple names and addresses. Navigate to the “Start Mail Merge” button in the Mailings tab: Follow the step-by-step process until asked to find the address doc…
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.

759 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

21 Experts available now in Live!

Get 1:1 Help Now