Jump to content

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


Recommended Posts

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
 

Link to comment
Share on other sites

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.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

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

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

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

 

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

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

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

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
 

Link to comment
Share on other sites

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 (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

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
 

Link to comment
Share on other sites

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 (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

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
 

Link to comment
Share on other sites

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.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

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
 

Link to comment
Share on other sites

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 (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

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
 

Link to comment
Share on other sites

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

 

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

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
 

Link to comment
Share on other sites

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 (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
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
 Share

×
×
  • Create New...