Jump to content
Messy_Code_Guy

Need a sample of how to find a picture and add a hypelink in a Word document.

Recommended Posts

Messy_Code_Guy

All,

I need some help with the following:
1. Finding an image in a Word doc.  I have read the help file but I cannot figure out how to reference the image in the Word doc.
2. Adding a hyperlink to that image.
3. How would I loop the add hyperlink (text) and add hyperlink (image) to replace multiple links in a document.

I have the add image and hyperlink working with the following code:

$pic = "<PHOTO>"
    $picpath = IniRead(@ScriptDir & "\Config\Config.ini", "User Info", "Picture", 0)
    Local $oRange = _Word_DocFind($oDoc, $pic)
    _Word_DocPictureAdd($oDoc, $picpath, Default, Default, $oRange)
    _Word_DocFindReplace($oDoc, $pic, "", Default, 0, True, True)
    If @error Then
        $file1 = FileOpen("C:\Tech\Log_Files\_Error_Logs\Error_LOG.txt", 9)
        _FileWriteLog($file1, "," & @ComputerName & "," & @UserName & ",Error adding a picture to the document. " & $picpath & " " & " @error = " & @error & " @extended = " & @extended)
        FileClose($file1)
    EndIf



    $Link = "<LINKEDIN>"
    $LinkedIn = IniRead(@ScriptDir & "\Config\Config.ini", "User Info", "LinkedIn", 0)
    Local $oRange = _Word_DocFind($oDoc, $Link)
    _Word_DocLinkAdd($oDoc, $oRange, $LinkedIn, Default, "Click here to visit my LinkedIn page. " & @CRLF & $LinkedIn, "LinkedIn")
    If @error Then
        $file1 = FileOpen("C:\Tech\Log_Files\_Error_Logs\Error_LOG.txt", 9)
        _FileWriteLog($file1, "," & @ComputerName & "," & @UserName & ",Error adding a link to the document. " & $LinkedIn & " " & " @error = " & @error & " @extended = " & @extended)
        FileClose($file1)
    EndIf

I just can't figure out how to find the images in a Word doc.

Thanks for reading my post!


 

"The only thing necessary for the triumph of evil is for good men to do nothing".

Edmund Burke
 

Share this post


Link to post
Share on other sites
water

The UDF does not support working with images the way you need it.

Get the Shapes collection and search for the item you need to add a link to it.

  • Like 1

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
Messy_Code_Guy

Water-

Thanks for the reply.  I am not sure how to get the shapes collection.  Do you have a small example of the code needed?

Thanks,


 

"The only thing necessary for the triumph of evil is for good men to do nothing".

Edmund Burke
 

Share this post


Link to post
Share on other sites
water

Will post an example as soon as I return to my Office.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
Messy_Code_Guy

Thanks for the assist!  Post the code when you have time. :)


 

"The only thing necessary for the triumph of evil is for good men to do nothing".

Edmund Burke
 

Share this post


Link to post
Share on other sites
water

Something like this. There is no error handling so it might crash if you do not use InlineShapes.

#include <Word.au3>
$oWord = _Word_Create()
$oDoc = _Word_DocOpen($oWord, "C:\temp\test.docx")
; Set Picture 1 to Microsoft, Picture 2 to Google
$oDoc.Hyperlinks.Add($oDoc.InlineShapes(1), "http://www.microsoft.com")
$oDoc.Hyperlinks.Add($oDoc.InlineShapes(2), "http://www.google.com")
; Loop through the InlineShape collection and display the Hyperlinkfor each picture
For $oShape In $oDoc.InlineShapes
    ConsoleWrite($oShape.Hyperlink.Address & @CRLF)
Next

 

  • Like 1

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
Messy_Code_Guy

Thanks water!  I will give it a try.  :)


 

"The only thing necessary for the triumph of evil is for good men to do nothing".

Edmund Burke
 

Share this post


Link to post
Share on other sites
water

If it doesn't work please post a small sample document (with all sensitive information removed) so i have something to play with.

  • Like 1

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
Messy_Code_Guy

Water-

Here is a sample document that is saved as a Word .mht.  I use this doc to create sigs for Outlook in Office 365 (2013).
If you can just add a link to Google to each of the images it would be much appreciated.

Thanks for the assist and if you have an address I can send the check to.....,  :)

Default RMLO_SIG.mht


 

"The only thing necessary for the triumph of evil is for good men to do nothing".

Edmund Burke
 

Share this post


Link to post
Share on other sites
water

I'm a bit confused now.
Do you want to insert the link into a doc or a mht file type?


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
Messy_Code_Guy

Sorry,

I want to add the link to the image in a .mht if possible.  After I add all the info I need I do a save as function to turn it into an Outlook signature.

_Word_DocSaveAs($oDoc, $sDocument, $wdFormatHTML)
_Word_DocSaveAs($oDoc, $sDocument, $wdFormatRTF)
_Word_DocSaveAs($oDoc, $sDocument, $wdFormatTEXT)

Thanks for the help!

Edited by Messy_Code_Guy

 

"The only thing necessary for the triumph of evil is for good men to do nothing".

Edmund Burke
 

Share this post


Link to post
Share on other sites
water

You know that my OutlookEX UDF contains a function to create signatures? Please check _OL_MailSignatureCreate.au3 example script to get an idea how Word and Outlook can be combined to create a signature. Then use _OL_MailSignatureSet to set the signature.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
Messy_Code_Guy

Water-
Very cool UDF by the way.  I have been using Word to create the template and add the info to the template using the Word UDF.  The only function I am missing is the find a picture and add a hyperlink to that picture.  I do have a working sample of the Outlook UDF Test that creates a signature and it works well.  Quick question! What language do you use to program all of UDF's in Autoit?

Thanks,


 

"The only thing necessary for the triumph of evil is for good men to do nothing".

Edmund Burke
 

Share this post


Link to post
Share on other sites
water

The UDFs are written in AutoIt as well.

Can you please post the code to create the template? It should be easy to add the links.

  • Like 1

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
Messy_Code_Guy

Water-
Here is the code I have to fill out the Word .mht file.  I do not have the search and add link function for the 11 images in the document. I am also adding the modified word document that has updated text place holders for the ini file. I have also added the .au3 for the template creation code. The code is a little sloppy but it works for me.

Thanks again for your help.

Default RMLO_SIG.mht

Word_Test2.au3


 

"The only thing necessary for the triumph of evil is for good men to do nothing".

Edmund Burke
 

Share this post


Link to post
Share on other sites
water

Maybe I need another cup of coffee but why do you need to work with a mht file type?
According to Wikipedia this is a web archive format: https://en.wikipedia.org/wiki/MHTML

I will try to provide an example based on a true Word docx document.

 

Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
Messy_Code_Guy

I use .mht files so I can embed the images and keep everything in one nice and tidy file.  I know it is not the best choice but it works. Thanks for the assistance and a .docx will work fine.:)


 

"The only thing necessary for the triumph of evil is for good men to do nothing".

Edmund Burke
 

Share this post


Link to post
Share on other sites
water

Save your .mht file as .docx and give this example script a try:

#include <Word.au3>
$oWord = _Word_Create()
$oDoc = _Word_DocOpen($oWord, "C:\temp\Default RMLO_SIG.docx")
$FBName = "http://www.facebook.com"
; Set Picture 1 to Facebook
_Word_DocLinkAdd($oDoc, $oDoc.InlineShapes(1).Range, $FBName, Default, "Click here to visit my facebook page. " & @CRLF & $FBName, "FB")
; Loop through the InlineShape collection and display the Hyperlink for each picture
For $oShape In $oDoc.InlineShapes
    ConsoleWrite($oShape.Hyperlink.Address & @CRLF)
Next

 

  • Like 1

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
Messy_Code_Guy

Water-

You ROCK!  Thanks for the help and the kick A$$ UDF's you have written.  I only have one question.  How do you figure out which image is what number when using inlineShapes?

Thanks again!


 

"The only thing necessary for the triumph of evil is for good men to do nothing".

Edmund Burke
 

Share this post


Link to post
Share on other sites
water

Will need to check tomorrow. Maybe we can set an unique identifier when creating the Word document.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Similar Content

    • Xandy
      By Xandy
      Special thanks: AdmiralAlkex, Melba23, MrCrearoR, Dragon Warrior 3, SDL
      MapIt is a tile world editor.  MapIt was built around the concept of reversing Dragon Warrior map images.  MapIt can take image input and produce a tile and world array.  
      Changing and replacing tile / world data is easy.  B/c tile world editor.

      CTRL+R in image above to signal replace tile action and I use "G" to Get the tile under mouse.
      A full list of hotkeys can be assigned in the: Help Menu\Hotkeys
       
      MapParser is a C++ project that scans images for unique tiles. 
      MapIt can be downloaded without MapParser. MapParser can be toggled off in the Scan_Image dialog. Without MapParser, MapIt will use the Scan_Tiles() function written in AutoIt ; which is 100 * slower Idk. If MapParser.exe will not run for you:      Installing Visual C++ Redistributable for Visual Studio 2015 should fix it: https://www.microsoft.com/en-us/download/details.aspx?id=48145   
      You can start with example world and tiles.
      Example world was made following these steps:
      Started with a tile map image of DragonWarrior3 town of: Reeve From MapIt World Menu \ New \ Scan_Image dialog, I set the area to exclude the key legend to the far right of image. After scanning the map image to world and tile array.  I removed a few of the map artifacts.  More work could be done on this world; removing unwanted tiles, but it is fine for now. I saved my world to disk.  This creates folder: Worldname: Containing folder of Tiles and a Worldname.txt. Using The Gimp, I edited some tiles to have a transparent color: Stairs, Trees, Desk Tables, Chest-of-drawers, Chairs, Signs, Doors, Beds. I changed the world layers to 2: World Menu \ Properties (*I recently changed this path) F9 Finds all selected tile on current layer and changes to a new selected tile on new layer. I used F9 to change all Trees on layer: 0 to Trees on layer: 1. Then I used F9 to change all Trees on layer: 0 to Grass on layer: 0  
      In this video you can see how I used the Tile Menu \ Replace From Disk option to remap tile images to my custom tiles.  Conveniently my tiles already have a transparent pixel.
      See video for how that was done:
      To use the example world:
      First unzip the world save file: http://songersoft.com/programming/mapit/worlds/Reeve_Swapped.zip From the World Menu: choose \Load Navigate to the Reeve_Swapped.txt located in the extracted zip. Or you can scan any image.
      The map images I used are here: http://www.realmofdarkness.net/dq/games/nes/dw3/maps/world
      For download, videos, and example of created world file data; please visit the MapIt webpage: http://songersoft.com/programming/mapit/mapit_about.phtml
    • lavascript
      By lavascript
      I have a Word document containing a 9-column table where row 1 is the column headers. My goal is to read the table into a 2d array, remove some rows, update some fields, and add a few rows to the end. The resulting array will likely be a different length. Next, I want to write the data back into the table. If it's easier, I can write the data to a new document from a template containing the same table header with a blank 2nd row.
      Here's my early attempt:
      Local $oWord = _Word_Create() Local $oDoc = _Word_DocOpen($oWord, $sFile) Local $aData = _Word_DocTableRead($oDoc, 1) $aData[3][5] = "Something else" Local $oRange = _Word_DocRangeSet($oDoc, 0) $oRange = _Word_DocRangeSet($oDoc, $oRange, $wdCell, 9) _Word_DocTableWrite($oRange,$aData) This, unfortunately, writes the entire array into the first cell of row 2. What am I doing wrong?
       
    • Subz
      By Subz
      Backstory:
      Our Microsoft Office Templates shared folder was changed from a DFS share to an Isilon share. example:
      Old Server: \\Domain.com\Office\Templates
      New Server: \\Templates.domain.com\Office\Templates
      The team making the changes overlooked that several hundred thousand documents, had been attached to the old template documents.  So when you open a document which has been attached, it will take a couple of minutes to open, while it tries to locate the old server path.  I've been asked to come in and fix it, so after several hours found that the data is being held in document.zip\word\_rels\settings.xml.rels, I now need to replace the old server path with the new server path.  I didn't want to use dom as that would take too long and found a tool wtc https://github.com/NeosIT/wtc which  works perfectly, takes about 8 minutes to scan a single directory with 4000 documents and fix them.  The problem is the documents are all held on sharepoint and they want to retain the file timestamp, which is easy enough, but they also don't want to keep the "Modified By" apparently they don't like seeing all the documents appearing as "Modified by: Subz"  Anyone know of way to retain the "Modified By" info,
    • FrancescoDiMuro
      By FrancescoDiMuro
      Good evening everyone
      I am working with Word UDF ( thanks @water! ), and, especially, with the function _Word_DocFindReplace().
      The replace does work everywhere in the document, but, it does not work in Headers or Footers.
      Am I missing something or am I forced to use the code below?
      I have already looked in the Help file ( about _Word_DocFindReplace() ), but there are no mentions about replace text in Headers/Footers.
      Sub FindAndReplaceFirstStoryOfEachType() Dim rngStory As Range For Each rngStory In ActiveDocument.StoryRanges With rngStory.Find .Text = "find text" .Replacement.Text = "I'm found .Wrap = wdFindContinue .Execute Replace:=wdReplaceAll End With Next rngStory End Sub Thanks everyone in advance


      Best Regards.
    • Atoxis
      By Atoxis
      Howdy, I've gone through a lot of au3 forums, and I once had a working Imagesearch script that I got from here.  However, and i'm just totally not sure how but my imagesearch scripts aren't working anymore.
      I'm not new to au3 but i'm not the most experienced with it's syntax/commands.

      Anyways, I've looked over the big threads involving imagesearch.

      Does anyone have a working Imagesearch x64 for win10 that is currently working as of the date with the post.

      Dll's and what not is fine, just when I tell the script to run, I want to be able to find the image on the screen!
      Can't find a working copy so if anyone has one please send it my way lol.

      I've taken all the imagesearch downloads and what not and have played with them but I can't get any of them working on my end, despite others saying they're working.
      Thanks.
×