PDA

View Full Version : Insert multiple tif files in document



clhare
10-05-2010, 09:20 AM
I need to set up a macro that will allow me to select multiple image files from a folder and then insert all the selected images into the active Word document. I tried to figure it out and came up with the following using pieces from other code I found, but it doesn't work. I get a compile error that says "expected array". I'm not at all familiar with arrays, so I don't know what I did wrong or how to fix it.

Sub OpenDialog()
Dim strFilename As String
Dim strPath As String
strPath = "C:\My Images\*.tif"
' Bring up File Open dialog box and show only the .tif files in selected folder
With Dialogs(wdDialogFileOpen)
strFilename = .Name = strPath
.Show
End With
' Exit on Cancel
If Not IsArray(strFilename) Then
MsgBox "No file was selected."
Exit Sub
End If
' Open Files
For i = LBound(strFilename) To UBound(strFilename)
Selection.InlineShapes.AddPicture strFilename:=strPath, _
LinkToFile:=False, SaveWithDocument:=True
Next i
End Sub

Any help would be greatly appreciated!

fumei
10-05-2010, 10:18 AM
You are asking it to give values from the lowest value of an array

LBound(strFilename)
to the upper value of an array
UBound(strFilename)

Except strFilename is NOT an array. It is neither declared as an array, nor given array values.

Please post code that you got this from. I have my doubts about the syntax of both
strPath = "C:\My Images\*.tif"

' and

strFilename = .Name = strPath

clhare
10-05-2010, 02:35 PM
Totally my error. I see that I haven't got an array set up correctly and my other line had an error in it. strfilename was supposed to be just "Filename".

Here's the code I found and was trying to use as a starting point:


Sub OpenMultipleFiles()
Dim Filter As String, Title As String, msg As String
Dim i As Integer, FilterIndex As Integer
Dim Filename As Variant
' File filters
Filter = "Excel (http://www.tek-tips.com/faqs.cfm?fid=4114#) Files (*.xls),*.xls," & _
"Text Files (*.txt),*.txt," & _
"All Files (*.*),*.*"
' Default filter to *.*
FilterIndex = 3
' Set Dialog Caption
Title = "Select File(s) to Open"
' Select Start Drive & Path
ChDrive ("E")
ChDir ("E:\Chapters\chap14")
With Application
' Set File Name Array to selected Files (allow multiple)
Filename = .GetOpenFilename(Filter, FilterIndex, Title, , True)
' Reset Start Drive/Path
ChDrive (Left(.DefaultFilePath, 1))
ChDir (.DefaultFilePath)
End With
' Exit on Cancel
If Not IsArray(Filename) Then
MsgBox "No file was selected."
Exit Sub
End If
' Open Files
For i = LBound(Filename) To UBound(Filename)
msg = msg & Filename(i) & vbCrLf ' This can be removed
Workbooks.Open Filename(i)
Next i
MsgBox msg, vbInformation, "Files Opened"' This can be removed
End Sub


I'm not sure what to do to fix it.

clhare
10-06-2010, 04:50 AM
I found different code and that seems to work (though sometimes I get some memory error--not sure why as other times it works). Here's what I've got now:

Sub AddMuliplePictures()
' 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)
' Create a new document for the pictures to be inserted into
Documents.Add DocumentType:=wdNewBlankDocument
' Declare a variable to contain the path of each selected item
' NOTE: 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 and TIF images and make it the second item in the list
.Filters.Add "Images", "*.gif; *.jpg; *.jpeg; *.tif"
' 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
Next vrtSelectedItem
' If the user presses Cancel...
Else
' Do nothing
End If
End With
' Reset the object variable to Nothing
Set fd = Nothing
' Goto to top of document
Selection.HomeKey Unit:=wdStory
End Sub


Does this code look ok? Is there a way to set the starting folder for the dialog box?

Thanks!

clhare
10-07-2010, 04:31 AM
I've figured out how to preset the starting folder in the dialog box by using ChangeFileOpenDirectory. But the problem I'm still having is that after awhile I'll start getting a 5112 runtime error that says "There is not enough memory or disk space to complete the operation." If I close Word and reopen it, the macro then works fine again. What would be causing this error and how do I fix it?

clhare
10-07-2010, 08:30 AM
Ok, me again....

I think the error I'm getting is because one of the tif files has multiple images in it. When I insert the tif into the document, only the first image is shown. Is there a workaround for that so I would get all the images in the tif file?

fumei
10-07-2010, 11:01 AM
Huh? Multiple images?

clhare
10-07-2010, 01:59 PM
That what I said! I guess you can somehow scan a document and save it as a .tif file even if it has multiple pages. If I double click on a .tif file, I get a viewer window that allows me to scroll to the next image in the file (if it has more than one. I didn't know that was possible. I have no clue how to get it imported into the Word document where you can see each image in it. PDFs would be better, but unfortunately not all users will have Adobe, so they chose the .tif route.

fumei
10-07-2010, 02:04 PM
Never heard of such a thing. Never seen one. I am at a complete loss here.