Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Have Macro That Works, Needs Tweaking for Image Insert Size

Status
Not open for further replies.

alex20850

Programmer
Mar 28, 2003
83
0
0
US
To use the following macro, I create a table with two columns and some rows and place the cursor in the right hand column.

While I have very little experience with macros, I am happy with the following macro as is EXCEPT for the size of image that is inserted into the right hand cells.

I am just looking for a way to adjust the column width and row height. While I suspect I will end up with 4 x 4 inches, I may want to change it.
============================================
Code:
Sub AddPix()
    'Declare a variable as a FileDialog object.
    Dim fd As FileDialog

    'Create a FileDialog object as a File Picker dialog box.
    Set fd = Application.FileDialog(msoFileDialogFilePicker)

    'Declare a variable to contain the path
    'of each selected item. Even though the path is a String,
    'the variable must be a Variant because For Each...Next
    'routines only work with Variants and Objects.
    Dim vrtSelectedItem As Variant

    'Use a With...End With block to reference the FileDialog object.
    With fd
    'Add a filter that includes GIF and JPEG images and make it the second item in the list.
        .Filters.Add "Images", "*.gif; *.jpg; *.jpeg"

        'Sets the initial file filter to number 2.
        .FilterIndex = 2

    'Use the Show method to display the File Picker dialog box and return the user's action.
        'If the user presses the action button...
        If .Show = -1 Then

            'Step through each string in the FileDialogSelectedItems collection.
            For Each vrtSelectedItem In .SelectedItems

            'vrtSelectedItem is a String that contains the path of each selected item.

                 Selection.InlineShapes.AddPicture FileName:= _
                  vrtSelectedItem _
                , LinkToFile:=False, SaveWithDocument:=True
                Selection.MoveRight Unit:=wdCell
                Selection.MoveRight Unit:=wdCell
            Next vrtSelectedItem
        'If the user presses Cancel...
        Else
        End If
    End With

    'Set the object variable to Nothing.
    Set fd = Nothing

End Sub
 
Inserted pictures are normally resized according to constraints imposed by the container - the table cell in this case. I'm not quite sure what you are asking. Is the table cell a fixed size, and the picture resized according to it, or is the table cell automatically resizing to accomodate the inserted picture?

Enjoy,
Tony

------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.

I'm working (slowly) on my own website
 
It is automatically adjusting.
I looked at column size to restrict the size, but I didn't see a way to do it. That would be very useful to know.
Thanks,
Alex
 
1. you should probably test to make sure the Selection is, in fact, in a table. You state you are placing the cursor (Selection) in the table column, you - if so - you would not get an error. However, if the code is executed with the cursor (Selection) NOT in a table it would crash on a run-time error. So, best practice is to make sure the Selection is in a table.

2. As Tony has mentioned, the image is constrained by the container holding it. So I will reiterate his question: what exactly are you trying to do. To turn OFF AutoFit, you can change that property of the table with:
Code:
.AllowAutoFit = False
It would be best to use that with a table object, but you could do it with Selection.Tables(1).

3. You can resize the image by using a InlineShape object, rather than Selection.InlineShape. Truncated code:
Code:
Dim oInlineShape As InlineShape

'   other stuff......


      For Each vrtSelectedItem In .SelectedItems
         'vrtSelectedItem is a String that contains the path
         'of each selected item.
         [b]Set oInlineShape[/b] = Selection.InlineShapes _
            .AddPicture(FileName:= _
              vrtSelectedItem _
              , LinkToFile:=False, SaveWithDocument:=True)
        [b] With oInlineShape
            .Height = 100
            .Width = 160
         End With[/b]

'  other stuff
But it is hard to say exactly until we know what - exactly - you are trying to do. The code above explicitly sets each image as 100 X 160 points - the default quality.

NOTE: the table was already set with .AllowAutoFit = False

BTW: nice use of lots of comments in your code.

"A little piece of heaven
without that awkward dying part."

advertisment for Reese's Peanut Butter Cups (a chocolate/peanut butter confection)

Gerry
 
I haven't figured out how to add an attachment to these messages.

First, my background.
I am a technical writer with no macro experience so I appreciate your patience. I don't remember where I found this throughly commented macro on one website or another.

Next, what I am trying to do.
As a technical writer and the at-desk support for Excel, etc. I frequently get asked questions. I want to be able to put together a quick list of instructions for steps by various users. I use SnagIt to capture a series of screenshots for the various steps in the process.

I want to be able to plug in the screenshots in the right hand column of a two column table with the macro moving to the next line and to the right hand column again. The left hand column will be used for instructions.

As for testing if I am in a table, that's not really necessary since I will be only one using this macro for now. If there is someone else later, I can just tell them to create a table with two columns.

That's all. Your help is greatly appreciated.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top