Solved

I need to add a feature onto an existing Word macro

Posted on 2011-03-11
9
300 Views
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.
Word-macro.txt
0
Comment
Question by:photoman11
  • 3
  • 3
9 Comments
 
LVL 15

Assisted Solution

by:roylong
roylong earned 100 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.
0
 

Author Comment

by:photoman11
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.
0
 
LVL 76

Expert Comment

by:GrahamSkan
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"
    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

0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:photoman11
ID: 35134578
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
0
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 400 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.


0
 

Author Comment

by:photoman11
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!
0
 
LVL 76

Expert Comment

by:GrahamSkan
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.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
In this article we discuss how to recover the missing Outlook 2011 for Mac data like Emails and Contacts manually.
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
An overview on how to enroll an hourly employee into the employee database and how to give them access into the clock in terminal.

746 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