poyhouston.blogg.se

Create a macro to insert file name in word
Create a macro to insert file name in word






  1. #Create a macro to insert file name in word how to#
  2. #Create a macro to insert file name in word pdf#
  3. #Create a macro to insert file name in word code#

#Create a macro to insert file name in word how to#

How to create a list of comments from a worksheet programmatically

#Create a macro to insert file name in word pdf#

How to save specific multiple worksheets to a pdf file programmatically Title:=": " & f.Multiply numbers in each row with remaining rows in cell range (UDF) Selection.InsertCaption Label:="Figure", TitleAutoText:="", _ Scale_factor = (max_width / myNewPic.Width) * 100 'loop through all the files in the target folder Here's an example that also resizes pictures that are over a defined widthSub BulkInsertPictures() You might want to consider batch processing all the files in a folder. So, considering there are over 1000 files to insert, won't it be very time consuming to have to select each file with the dialog? It would be useful to set a object variable for the picture, which isn't quite as straight-forward when you use a dialog compared with when you use the Shapes or InlineShapes Add method. You can size the picture using it's Height and Width properties, or ScaleHeight and ScaleWidth. TitleAutoText:="", Title:=": " & picName, _Įnd SubAlso, in this example, I've checked the return value from the dialog to make sure it wasn't cancelled to avoid errors Selection.InsertCaption Label:="Figure", _

create a macro to insert file name in word

PicName = Right(picPath, Len(picPath) - InStrRev(picPath, "\")) PicName = Trim(picName)Here's an alternative using wdDialogInsertPicture - it returns the whole path so you need to get the filename for the caption from thatSub InsertPicture2()īClicked =. With the FileOpen dialog using Display, after the End With line, add'replace the extra quotes with spaces I wondered why you didn't use the InsertPicture dialog - it somehow seems appropriate - either way, you need to do a bit of string manipulation to sort things out. (Display doesn't excecute the dialog action, like Show does)

create a macro to insert file name in word

This is caused, quite frankly, by buggy Office dialogs that insert quotes around strings with spaces in when using Display mode. Notice the extra quotes, which are illegal characters in filenames. With a filename with spaces, you get ""filename.jpg"" With a filename with no spaces, you get "filename.jpg"

#Create a macro to insert file name in word code#

When you want to continue the code excecution, press F5 (or F8 to step line, by line) the code will pause at that point and you can look at the variable values.

create a macro to insert file name in word

Now put your cursor in the End With line at press F9 - this will set a breakpoint. In the VBE, select View>Locals - this will show the Locals window that displays a list of all the local variables while the routine is running. To find out whats going on, you look at the value of your string variable in each case. This is caused by illegal characters in the file name - but spaces are perfectly valid. I am no word expert by any means but I think you will have to rename your pictures so that they don't have any spaces.maybe someone will come along and correct me.I have the same problem with your code.Īs far as getting the caption below the picture, try adding a carriage return between your insert picture and insert caption code. I have taken the liberty of enclosing your code in vba tags.just select your code when posting and hit the "Wrap VBA tags button" The code 'Position:=wdCaptionPositionBelow' positions the caption at the bottom right of the picture but I would prefer it to be underneath it.ĭoes anyone have any ideas or suggestions? Any help would be much appreciated. If I try to insert a picture with a filename that contains spaces then I get a run-time error '5152'.Ģ. Selection.InsertCaption Label:="Figure", TitleAutoText:="", Title:=": " & picName, _ġ. PicPath = CurDir 'picPath holds the picture path By surfing the net to find example macros and piecing together the relevant bits, here's what I've come up with so far: I am a VBA novice trying to write code to insert a picture and the filename as a caption.








Create a macro to insert file name in word