Link to home
Start Free TrialLog in
Avatar of photoman11
photoman11Flag for United States of America

asked on

I need to add a feature onto an existing Word macro

I use Microsoft Outlook 2007 in a Windows 7 operating environment.

I am attaching a Word Macro that an expert exchange member was kind enough to develop in order to accomplish what I wanted to do. Generally, I create 80 page Word documents, the first 10 pages are comprised of text and a few images; while the last 5 pages consist of text. All the pages in between consist of images.

The Word Macro which was designed, allows me to designate a folder, usually containing hundreds of images, and the macro inserts them into successive blank pages in a 3 row by 2 column format. Since I'm using this more and more, the issues I am running into now involve the increasing difficulty of simply doing a cut-and-paste of the 50-60 pages of images from the resulting new Word document and insert it into my primary document with the text.

I've enclosed the macro in hopes that there was a way that I could instruct it to start loading the images in a specific existing document, starting at a particular page number; rather than start a blank new document every time it does it.

Thank you very much for your help and pity taken on a non-coding macro neophyte.
Word-macro.txt
SOLUTION
Avatar of roylong
roylong
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of photoman11

ASKER

Actually I have, however I am dealing with so many applications already (over 60 at last count), the thought of taking on anything more complicated than tic-tac-toe, is met with high blood pressure.

So this point, I'm just hoping for a slight revision to the Word Macro.
Avatar of GrahamSkan
roylong has a point. However, if you want to add VBA to your skills, it's good to have a starting point.

Here is some modified code
Sub InsertPicturesInTables()
    Dim Doc As Word.Document
    Dim tbl As Word.Table
    Dim strFolder As String
    Dim strFileName As String
    Dim c As Integer
    Dim r As Integer
    Dim ilsh As InlineShape
    Dim iPage As Integer
    
    Rem this is a whole-line comment (REM is short for remark)
    'and so is this. To append a comment following code on a line you must use the apostrophe and not REM
    strFolder = "F:\Images for Word Macro" 'edit this to match your folder
    Set Doc = ActiveDocument 'Documents.Open("C:\MyDocs\MyFile.doc") 'Open the document
    iPage = 10 'say
    
    If iPage > Doc.Range.Information(wdNumberOfPagesInDocument) Then
        MsgBox "Page " & iPage & " doesn't exist"
    Else
        Doc.Select
        Selection.GoTo wdGoToPage, wdGoToAbsolute, iPage
        
        Set tbl = Doc.Tables.Add(Doc.Bookmarks("\Page").Range, 3, 2) 'create a 3-row, 2-column table
        tbl.Rows.HeightRule = wdRowHeightExactly
        tbl.Rows.Height = CentimetersToPoints(7)
        tbl.Range.Cells.VerticalAlignment = wdCellAlignVerticalCenter 'centre cell content vertically
        tbl.Range.Paragraphs.Alignment = wdAlignParagraphCenter 'inline shapes are treated like text.
        strFileName = Dir$(strFolder & "\*.jpg") ' find the first file in the folder
        c = 0
        r = 1
        Do Until strFileName = ""
            c = c + 1 'c is used to set the column number
            If c = 3 Then
                r = r + 1
                If r = 4 Then
                    Doc.Bookmarks("\EndOfDoc").Range.InsertBreak wdPageBreak
                    Set tbl = Doc.Tables.Add(Doc.Bookmarks("\EndOfDoc").Range, 3, 2) 'create a 3-row, 2-column table
                    tbl.Rows.HeightRule = wdRowHeightExactly
                    tbl.Rows.Height = CentimetersToPoints(7)
                    tbl.Range.Cells.VerticalAlignment = wdCellAlignVerticalCenter 'centre cell content vertically
                    tbl.Range.Paragraphs.Alignment = wdAlignParagraphCenter 'inline shapes are treated like text.
    
                    r = 1
                End If
                c = 1
            End If
            Debug.Print r, c, strFileName
            Set ilsh = Doc.InlineShapes.AddPicture(strFolder & "\" & strFileName, False, True, tbl.Cell(r, c).Range)
            strFileName = Dir$()
        Loop
    End If
End Sub

Open in new window

GrahamSkan,

I appreciate your point as well as Roy's. Truth be told, I never understood programming although I tried 3 times and it is far too frustrating and overwhelming at this point to even consider it. I try to stay away from it like the plague, but unfortunately I am not always successful.

Since I don't understand it, it's hard for me to even ask semi-intelligent questions about it. So I've decided to take a different tact and plead complete ignorance, and list a few questions that you might consider answering:

All of these are based on the assumption that the above 52 lines of code are meant to replace my current macro and to accomplish everything it did, PLUS to insert the images into an existing document rather than start a new document...

1. I am guessing that line #14 has something to do with identifying which document, the images are supposed to be inserted into, and where in the document (page number). I am sure if I take a guess on how to change this, to match my drive and directory structures, I will mess it up completely.

Instead, how about if I just tell you that the document that I want to run the macro for, will be open on my desktop at this path: C:\Users\Dinny\Desktop\[name of document.docx]

Therefore, can that information be inserted into the proper place, in the code?

2. Can you tell me where in the code I identify the page number where I want the images to start being inserted into?

I guess that is about it. I place the images in a folder ("F:\Images for Word Macro") and all I am trying to figure out now is how to tell the macro where the document is located that I want the images loaded into.

And, should the document be open or not.

Thanks for your help and understanding area
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I understand you going above and beyond. And, please realize that I am very appreciative and do not expect development work to be done. Actually, I wanted to know if the current code was in such a condition that I could use it as is or I needed to offer it as a starting point to somebody who I can pay to finish it.

I did want to come across as a total idiot, offering to pay someone to finish something that already was finished. Keeping in the spirit of Expert Exchange, what is your opinion on accessing resources who could perform visual basic tasks like this?

Within the expert pool on this site? I have used oDesk before for a variety of tasks, but not Visual Basic. Or, would you recommend a different source to assist in this manner?

Thanks again for everything!
Sorry. I missed the questions in your final comment.

Some of the experts here do offer paid help with projects that are too large to be considered as questions. Those that do will indicate this and will have a contact address in their profiles.

Personally I don't know anything about oDesk, but I think that this is the right place for VB and VBA questions.