Jump to content

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


Recommended Posts

You could set the AlternativeText when creating the Word document and check that in your script:

#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 some information for each picture
For $oShape In $oDoc.InlineShapes
    ConsoleWrite($oShape.Name & " - " & $oShape.AlternativeText & " - " & $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-

I am getting this error:

"C:\Temp\Word_Test3.au3" (35) : ==> The requested action with this object has failed.:
ConsoleWrite($oShape.Name & " - " & $oShape.AlternativeText & " - " & $oShape.Hyperlink.Address & @CRLF)
ConsoleWrite($oShape^ ERROR

Here is the code I was using.

#include <MsgBoxConstants.au3>
#include <Word.au3>
#include <File.au3>



Local $oWord = _Word_Create(True, True)
If @error Then
    $file1 = FileOpen(@ScriptDir & "\Error_LOG.txt", 9)
    _FileWriteLog($file1, "," & @ComputerName & "," & @UserName & ",Error creating a new Word application object." & " @error = " & @error & " @extended = " & @extended)
    FileClose($file1)
EndIf

Local $sDocument = @ScriptDir & '\Default RMLO_SIG.mht'
Local $oDoc = _Word_DocOpen($oWord, $sDocument, Default, Default, False)
If @error Then
    $file1 = FileOpen(@ScriptDir & "\Error_LOG.txt", 9)
    _FileWriteLog($file1, "," & @ComputerName & "," & @UserName & ",Error opening " & $sDocument & " @error = " & @error & " @extended = " & @extended)
    FileClose($file1)
EndIf
$Google = "http://www.google.com"
; Set Picture 1 to Facebook
_Word_DocLinkAdd($oDoc, $oDoc.InlineShapes(2).Range, $Google, Default, "Click here to visit Google.com. " & @CRLF & $Google, "G")
If @error Then
    $file1 = FileOpen(@ScriptDir & "\Error_LOG.txt", 9)
    _FileWriteLog($file1, "," & @ComputerName & "," & @UserName & ",Error setting link in image " & $Google & " @error = " & @error & " @extended = " & @extended)
    FileClose($file1)
EndIf

$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 some information for each picture
For $oShape In $oDoc.InlineShapes
    ConsoleWrite($oShape.Name & " - " & $oShape.AlternativeText & " - " & $oShape.Hyperlink.Address & @CRLF)
    If @error Then
    $file1 = FileOpen(@ScriptDir & "\Error_LOG.txt", 9)
    _FileWriteLog($file1, "," & @ComputerName & "," & @UserName & ",Error setting link in image " & $FBName & " @error = " & @error & " @extended = " & @extended)
    FileClose($file1)
EndIf
Next
If @error Then
    $file1 = FileOpen(@ScriptDir & "\Error_LOG.txt", 9)
    _FileWriteLog($file1, "," & @ComputerName & "," & @UserName & ",Error setting link in image " & $FBName & " @error = " & @error & " @extended = " & @extended)
    FileClose($file1)
EndIf

$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")
For $oShape In $oDoc.InlineShapes
    ConsoleWrite($oShape.Name & " - " & $oShape.AlternativeText & " - " & $oShape.Hyperlink.Address & @CRLF)
    If @error Then
    $file1 = FileOpen(@ScriptDir & "\Error_LOG.txt", 9)
    _FileWriteLog($file1, "," & @ComputerName & "," & @UserName & ",Error setting link in image " & $FBName & " @error = " & @error & " @extended = " & @extended)
    FileClose($file1)
EndIf
Next

If @error Then
    $file1 = FileOpen(@ScriptDir & "\Error_LOG.txt", 9)
    _FileWriteLog($file1, "," & @ComputerName & "," & @UserName & ",Error setting link in image " & $FBName & " @error = " & @error & " @extended = " & @extended)
    FileClose($file1)
EndIf

$Google = "http://www.google.com"
; Set Picture 1 to Facebook
_Word_DocLinkAdd($oDoc, $oDoc.InlineShapes(2).Range, $Google, Default, "Click here to visit Google.com. " & @CRLF & $Google, "G")
If @error Then
    $file1 = FileOpen(@ScriptDir & "\Error_LOG.txt", 9)
    _FileWriteLog($file1, "," & @ComputerName & "," & @UserName & ",Error setting link in image " & $Google & " @error = " & @error & " @extended = " & @extended)
    FileClose($file1)
EndIf

Thanks for the help!

 

"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

Which version of AutoIt do you run?

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

With AutoIt 3.3.14.x you need a COM error handler. If none is defined every COM error crashes your script.

Global $__goError = ObjEvent("AutoIt.Error", "__Excel_COMErrFunc") ; At the top of your script

Function __Excel_COMErrFunc comes with the Excel UDF.
For details see ObjEvent in the help file.

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...