Ticker Tape
10-08-2014, 02:53 AM
Hello,
This question refers to VBA in Word 2007 running under Windows XP Pro. SP3.
My problem is simple. The script below does everything I need to achieve image insertion formatted to my needs, except:-
It types the full file name into each cell of the table: I need only the file name itself, without path or extension.
At this point, if you can help please do so. If you can tell me the modification needed, and where it fits into the script to achieve the objective, I would be very grateful.
The rest of this note, after the script, is background describing how I got to this point. When I help people, I find it interesting / useful to understand what the person is trying to do, and why.
Sub Autopics()
Dim fd As FileDialog
Dim oTbl As Table
Dim oILS As InlineShape
Dim vrtSelectedItem As Variant
If Documents.Count = 0 Then
If MsgBox("No document open!" & vbCr & vbCr & _
"Do you wish to create a new document to hold the images?", _
vbYesNo, "Insert Images") = vbYes Then
Documents.Add
Else
Exit Sub
End If
End If
'Add a 1 row 2 column table to take the images
Set oTbl = Selection.Tables.Add(Selection.Range, 1, 2)
With oTbl.Borders
.InsideColor = wdColorAutomatic
.InsideLineStyle = wdLineStyleSingle
.OutsideColor = wdColorAutomatic
.OutsideLineStyle = wdLineStyleSingle
End With
oTbl.AutoFitBehavior (wdAutoFitFixed)
Set fd = Application.FileDialog(msoFileDialogFilePicker)
With fd
.Title = "Select image files and click OK"
.Filters.Add "Images", "*.gif; *.jpg; *.jpeg; *.bmp; *.tif; *.png"
.FilterIndex = 2
If .Show = -1 Then
For Each vrtSelectedItem In .SelectedItems
With Selection
Set oILS = .InlineShapes.AddPicture(FileName:= _
vrtSelectedItem, LinkToFile:=False, SaveWithDocument:=True, _
Range:=Selection.Range)
.InsertAfter vbCrLf & vbCrLf
.Collapse 0
.MoveLeft Unit:=wdCharacter, Count:=1
.Text = vrtSelectedItem
.MoveRight wdCell, 1
End With
Next vrtSelectedItem
Else
End If
End With
If Len(oTbl.Rows.Last.Cells(1).Range) = 2 Then oTbl.Rows.Last.Delete
Set fd = Nothing
' Stop rows breaking
Selection.Tables(1).Select
Selection.Rows.AllowBreakAcrossPages = 0
' Font size and colour'
Selection.Tables(1).Select
Selection.Font.Color = wdColorAutomatic
Selection.Font.Bold = 0
Selection.Font.Size = 9
Selection.ParagraphFormat.SpaceAfter = 0 'or the value you prefer
End Sub
Background
I am a UK based inventory clerk. For years I have been inserting images of property interiors and their defects into the reports I produce for clients (Inventories, Check In, Check Out etc.). Methodology: Insert pictures one at a time and then type descriptive text. This is time consuming and can be troublesome when selecting images which were captured out of sequence during the inspection.
So, I trawled the web and found some VBA code which provided the basis of my script (if it’s yours, and you still recognise it after my editing, thank you very much). The script has been much ‘tweaked’ by trial and error to achieve my particular needs and now meets those needs to perfection except for the name problem (although I have no idea whether it still stands up as a logical, efficient script). I have searched extensively and found numerous pieces of code that achieve the desired file name output, but have been unable to get any of them to work within my script. I apologise for including the full script but, after my own efforts, I don’t know where, or how, it needs modifying. (Did I explain? Prior to this, a macro was something I recorded with keystrokes, I am a VBA virgin!). It’s clear to me that I’m not going to achieve my aim without a lot more time spent understanding VBA, hence my plea for help.
BTW
Whilst looking for file name code to complete my macro, I came across a Word Add-In, written by Greg Maxey. For a short time I was prepared to kick myself for spending so much time putting my own solution together. Unfortunately, Greg’s Add-In doesn’t meet my needs in respect of a couple of key details:-
My principal client requires each image to be in a cell with borders and its descriptive text to be in the same cell, above the image. In all other respects Greg’s Add-In is perfect. I’m afraid I don’t have the knowledge to know just how clever his work is, but every time I run my macro I will be thinking of his image selection and ordering screen. For what it’s worth, that feature is, to me, fantastic.
Thanks for taking the time to read this note.
Phil
This question refers to VBA in Word 2007 running under Windows XP Pro. SP3.
My problem is simple. The script below does everything I need to achieve image insertion formatted to my needs, except:-
It types the full file name into each cell of the table: I need only the file name itself, without path or extension.
At this point, if you can help please do so. If you can tell me the modification needed, and where it fits into the script to achieve the objective, I would be very grateful.
The rest of this note, after the script, is background describing how I got to this point. When I help people, I find it interesting / useful to understand what the person is trying to do, and why.
Sub Autopics()
Dim fd As FileDialog
Dim oTbl As Table
Dim oILS As InlineShape
Dim vrtSelectedItem As Variant
If Documents.Count = 0 Then
If MsgBox("No document open!" & vbCr & vbCr & _
"Do you wish to create a new document to hold the images?", _
vbYesNo, "Insert Images") = vbYes Then
Documents.Add
Else
Exit Sub
End If
End If
'Add a 1 row 2 column table to take the images
Set oTbl = Selection.Tables.Add(Selection.Range, 1, 2)
With oTbl.Borders
.InsideColor = wdColorAutomatic
.InsideLineStyle = wdLineStyleSingle
.OutsideColor = wdColorAutomatic
.OutsideLineStyle = wdLineStyleSingle
End With
oTbl.AutoFitBehavior (wdAutoFitFixed)
Set fd = Application.FileDialog(msoFileDialogFilePicker)
With fd
.Title = "Select image files and click OK"
.Filters.Add "Images", "*.gif; *.jpg; *.jpeg; *.bmp; *.tif; *.png"
.FilterIndex = 2
If .Show = -1 Then
For Each vrtSelectedItem In .SelectedItems
With Selection
Set oILS = .InlineShapes.AddPicture(FileName:= _
vrtSelectedItem, LinkToFile:=False, SaveWithDocument:=True, _
Range:=Selection.Range)
.InsertAfter vbCrLf & vbCrLf
.Collapse 0
.MoveLeft Unit:=wdCharacter, Count:=1
.Text = vrtSelectedItem
.MoveRight wdCell, 1
End With
Next vrtSelectedItem
Else
End If
End With
If Len(oTbl.Rows.Last.Cells(1).Range) = 2 Then oTbl.Rows.Last.Delete
Set fd = Nothing
' Stop rows breaking
Selection.Tables(1).Select
Selection.Rows.AllowBreakAcrossPages = 0
' Font size and colour'
Selection.Tables(1).Select
Selection.Font.Color = wdColorAutomatic
Selection.Font.Bold = 0
Selection.Font.Size = 9
Selection.ParagraphFormat.SpaceAfter = 0 'or the value you prefer
End Sub
Background
I am a UK based inventory clerk. For years I have been inserting images of property interiors and their defects into the reports I produce for clients (Inventories, Check In, Check Out etc.). Methodology: Insert pictures one at a time and then type descriptive text. This is time consuming and can be troublesome when selecting images which were captured out of sequence during the inspection.
So, I trawled the web and found some VBA code which provided the basis of my script (if it’s yours, and you still recognise it after my editing, thank you very much). The script has been much ‘tweaked’ by trial and error to achieve my particular needs and now meets those needs to perfection except for the name problem (although I have no idea whether it still stands up as a logical, efficient script). I have searched extensively and found numerous pieces of code that achieve the desired file name output, but have been unable to get any of them to work within my script. I apologise for including the full script but, after my own efforts, I don’t know where, or how, it needs modifying. (Did I explain? Prior to this, a macro was something I recorded with keystrokes, I am a VBA virgin!). It’s clear to me that I’m not going to achieve my aim without a lot more time spent understanding VBA, hence my plea for help.
BTW
Whilst looking for file name code to complete my macro, I came across a Word Add-In, written by Greg Maxey. For a short time I was prepared to kick myself for spending so much time putting my own solution together. Unfortunately, Greg’s Add-In doesn’t meet my needs in respect of a couple of key details:-
My principal client requires each image to be in a cell with borders and its descriptive text to be in the same cell, above the image. In all other respects Greg’s Add-In is perfect. I’m afraid I don’t have the knowledge to know just how clever his work is, but every time I run my macro I will be thinking of his image selection and ordering screen. For what it’s worth, that feature is, to me, fantastic.
Thanks for taking the time to read this note.
Phil