Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 05-02-2023, 11:31 AM
MartinGM MartinGM is offline Change code now that Alt Text has changed Windows 11 Change code now that Alt Text has changed Office 2021
Advanced Beginner
Change code now that Alt Text has changed
 
Join Date: May 2023
Location: England
Posts: 66
MartinGM is on a distinguished road
Default Change code now that Alt Text has changed

I have just migrated to Office 365 and Alt Text functionality has changed, so I need to change a simple line of VBA . . . but I cannot see how to do this.

I used to insert an image using the Insert Picture dialog triggered by VBA, and capture the filename which automatically came across as Alt Text, as follows . . .

1. Set the dialog to the required image folder, display the list of images, then select the correct one manually . . .



Dim PicturePath As String
Dim FileName as String

'Save user's default image insertion path
PicturePath = Options.DefaultFilePath(Path:=wdPicturesPath)

'Change path to directory of the pictures
Options.DefaultFilePath(Path:=wdPicturesPath) = "C:\Users\Public\Pictures\ . . . ."

'Display the dialog
With Dialogs(wdDialogInsertPicture)
If .Show = False Then Exit Sub
End With

'Before quitting, restore the original pictures' path
Options.DefaultFilePath(Path:=wdPicturesPath) = PicturePath

ActiveDocument.InlineShapes(ActiveDocument.InlineS hapes.Count).Select

2. So now the correct image is inserted and selected.

3. Now for the one line of code that no longer works . . .

FileName = Selection.InlineShapes(1).AlternativeText 'Extract the Alternative Text

Unfortunately (for me) Microsoft has changed the AlternativeText function entirely and this field no longer comes into the document automatically as it used to. My question is how can I capture the filename of the image which I have just chosen and inserted ?

FYI I used to go on to extract the final section of the filename (less the file extension) and use that as the image's title in the Word document I am editing. This saved me a great deal of time and avoided transcription errors.

Thanks in advance for any advice.

Martin
Reply With Quote
  #2  
Old 05-02-2023, 06:01 PM
Guessed's Avatar
Guessed Guessed is offline Change code now that Alt Text has changed Windows 10 Change code now that Alt Text has changed Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,932
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

.AlternativeText still works but perhaps your logic in working out which graphic to apply it to is flawed.

ActiveDocument.InlineShapes(ActiveDocument.InlineS hapes.Count).Select
will select the LAST graphic in the file, which may or may not be the graphic you just inserted - depending on where you put your cursor.

Instead, you can harvest the path to the picture via the dialog without inserting it, and then insert the picture while defining it. You then have the right shape to assign the text to.
Code:
Sub TagMyNewPicture()
  Dim sPath As String, aInShape As InlineShape
  With Dialogs(wdDialogInsertPicture)
    .Display
    sPath = .Name
    Debug.Print sPath
  End With
  Set aInShape = Selection.InlineShapes.AddPicture(FileName:=sPath, LinkToFile:=False, SaveWithDocument:=True)
  With aInShape
    .AlternativeText = sPath
    .Title = "Look Mum, no hands"
  End With
End Sub
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #3  
Old 05-05-2023, 05:03 AM
MartinGM MartinGM is offline Change code now that Alt Text has changed Windows 11 Change code now that Alt Text has changed Office 2021
Advanced Beginner
Change code now that Alt Text has changed
 
Join Date: May 2023
Location: England
Posts: 66
MartinGM is on a distinguished road
Default Thanks

Thank you very much - I understand (which is a miracle).
Reply With Quote
  #4  
Old 05-11-2023, 10:06 AM
MartinGM MartinGM is offline Change code now that Alt Text has changed Windows 11 Change code now that Alt Text has changed Office 2021
Advanced Beginner
Change code now that Alt Text has changed
 
Join Date: May 2023
Location: England
Posts: 66
MartinGM is on a distinguished road
Default

Correction - that isn't what I wanted to do - I am not finding it easy to describe, sorry about that.

This is what I am trying to do, all in VBA

1. I set the path of the folder from which I want to insert the picture. this works fine
2. I open the dialog, pointing at the folder. this works fine
3. I click on the picture I want to insert, and it is inserted. this works fine
4. This is the bit that doesn't work any more - I want to capture the filename of the picture I have just inserted. I used to use the AltText property which contained the filename, but that is no longer imported with the picture.

How can I find the filename of the picture I have clicked and inserted ?

Thanks
Reply With Quote
  #5  
Old 05-12-2023, 04:34 AM
MartinGM MartinGM is offline Change code now that Alt Text has changed Windows 11 Change code now that Alt Text has changed Office 2021
Advanced Beginner
Change code now that Alt Text has changed
 
Join Date: May 2023
Location: England
Posts: 66
MartinGM is on a distinguished road
Default

SOLVED

OK, coming at this from a different direction, here is how I can insert a picture AND capture its filename. It is much simpler than the AltText method I used to use.

In passing, I go on to use the filename with the extension stripped off, as the picture caption in my document.

Sub InsertThumbnail()

Dim objFSO As New FileSystemObject
Dim FileName As String 'The filename of the inserted picture
Dim FullPath As String 'The fliepath of the picture to be inserted

Dim ThumbnailsFolder As String 'The folder containing the picture to be inserted

'Insert the picture, capturing the file name in the process

ThumbnailsFolder = "C:\<My Location>"

With Application.FileDialog(msoFileDialogOpen) 'Open the Thumbails folder

.InitialFileName = ThumbnailsFolder 'Set where the dialog opens
.Title = "Choose a file to insert" 'Title for the dialog box
.AllowMultiSelect = False
If .Show <> -1 Then Exit Sub
FileName = objFSO.GetFileName(.SelectedItems(1)) 'Capture the filename
FullPath = ThumbnailsFolder & FileName 'Set the full path of the picture, for later insertion

End With

Selection.InlineShapes.AddPicture FileName:=FullPath, LinkToFile:=False, _
SaveWithDocument:=True 'Insert the picture

End Sub

Last edited by MartinGM; 05-12-2023 at 09:41 AM.
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Change code now that Alt Text has changed AutoOpen code to change date code to date text Legal Learning Center Word VBA 6 02-22-2020 02:24 PM
Change code now that Alt Text has changed Default opening changed from 2010 to 2016 - How to change it back on Win10? stimu Excel 5 09-28-2017 11:45 AM
Change code now that Alt Text has changed VBA Code to search for field codes with certain text before the Field code and to change style welcometocandyland Word VBA 4 02-08-2017 06:53 PM
Change code now that Alt Text has changed Code that will run when a cells value is changed. DonJohns1 Excel Programming 2 04-15-2015 10:48 AM
Need to delete 'field code changed' in my reference list leb Word 4 05-04-2013 12:25 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 01:39 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft