Go Premium for a chance to win a PS4. Enter to Win


I need to add a feature onto an existing Word macro

Posted on 2011-03-11
Medium Priority
Last Modified: 2012-05-11
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.
Question by:photoman11
  • 3
  • 3
LVL 15

Assisted Solution

roylong earned 400 total points
ID: 35115591
have you ever thought about investing some time and money into a proper desktop publishing solution with some workflow automation plug-ins?

I'm thinking something like Quark Xpress or Adobe InDesign with some automation would give you much better and quicker results - and it's designed from the ground up for that kind of workflow.

Author Comment

ID: 35118563
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.
LVL 76

Expert Comment

ID: 35134254
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"
        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$()
    End If
End Sub

Open in new window

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 35134578

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
LVL 76

Accepted Solution

GrahamSkan earned 1600 total points
ID: 35136371
If you need to use VB code, you will have to learn something about how to use it. You already know where a VBA macro goes and how to start it.

This site is intended to be one that gives tips -  not to do all your development . It is arguable that I exceeded that remit with the original macro, but I couldn't find an example that demonstrated the methods that you needed. The macro is now there for anyone else who needs to do anything like that.

Yes the macro is a replacement.

As it stands, Line 14 sets the active document that is already open in the application as the one to work on.  

The commented out part shows how it could be used to open a particular file, thus:

Set Doc = Documents.Open("C:\MyDocs\MyFile.doc") 'Open the document

If you are always working on the same document, you can safely put the path in place of the bit within the quotes. If that file is already open, it will still work.

Line 15 hard-codes the number of the page that you want to accept the data.

Using page numbers for navigation in Word is generally problematic. Page numbers can change, but this will be OK if the earlier parts of the document are fixed.


Author Comment

ID: 35139673
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!
LVL 76

Expert Comment

ID: 35229977
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.

Featured Post

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

When asking a question in a forum or creating documentation, screenshots are vital tools that can convey a lot more information and save you and your reader a lot of time
In a use case, a user needs to close an opened report by simply pressing the Escape (Esc) key. This can be done by adding macro code in Report_KeyPress or Report_KeyDown event.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

783 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