PDA

View Full Version : [SOLVED:] Wrong number of arguments or invalid property assignments inserting a hyperlink



neg
09-13-2018, 01:52 AM
Hi all, please can someone give me a pointer, I'm self taught and usually scour forums to help me find answers to get my code to work but in this case I'm struggling. Everything I read says the code below should work but.... I'm using Excel to open word and generate a document from a series of strings stored in the excel file. One of my strings needs to have a hyperlink added. So I set the display text as disp from a value from excel.
I then generate the hyperlink string by concatenating 2 fields as hyperadd (i'ts quite long).
I then search for a bookmark "law" and select the 8 character word straight after it.
Then I try to add the hyperlink at the selected txt and get "Wrong number of arguments or invalid property assignments".

Any ideas? (bound to be something simple!)

I suspect the issue is with the .range (and it not recognising my selection from the previous row but.....)

Thanks


If fieldtopass = "X9_0CX" Then Let disp = valuetopass
If fieldtopass = "X9_0DX" Then Let hyperadd = valuetopass
If fieldtopass = "X9_0EX" Then
Let hyperadd = hyperadd & valuetopass


With wrdDoc
.Bookmarks("law").Select
.Application.Selection.MoveRight Unit:=wdCharacter, Count:=8, Extend:=wdExtend
.Application.Selection.Hyperlinks.Add Anchor:=Selection.Range, Address:=hyperadd, TextToDisplay:=disp


End With
End If

gmaxey
09-13-2018, 04:05 AM
The following works fine from within Word:


Sub Test()
Dim wrdDoc As Document
Dim hyperadd As String, disp As String
Set wrdDoc = ActiveDocument
hyperadd = "www.google.com (http://www.google.com)"
disp = "Google"
With wrdDoc
.Bookmarks("law").Select
Application.Selection.MoveRight Unit:=wdCharacter, Count:=8, Extend:=wdExtend
Application.Selection.Hyperlinks.Add Anchor:=Selection.Range, Address:=hyperadd, TextToDisplay:=disp
End With
End Sub

I suspect that you use of "Application" is referencing Excel and not Word. I don't have time to test in an Excel environment right now, but try replacing .Application with .Parent.

neg
09-17-2018, 05:21 PM
Hi Greg, yes I'm calling word from excel. I have tried .parent and it throws the same error. Thanks

gmaxey
09-17-2018, 06:19 PM
Either of the following work calling Word from Excel. The issue is you are trying to use the Excel Application (and the Excel Selection) in a Word process:


Option Explicit
Sub ScratchMacroI()
'A basic Word macro coded by Greg Maxey, http://gregmaxey.com/word_tips.html, 9/17/2018
Dim oApp As Object
Dim oDoc As Object
Dim hyperadd As String, disp As String
hyperadd = "www.google.com (http://www.google.com)"
disp = "Google"
Set oApp = CreateObject("Word.Application")
Set oDoc = oApp.Documents.Open("D:\Text.docx")
With oDoc
.Bookmarks("law").Select
.Parent.Selection.MoveRight Unit:=1, Count:=8, Extend:=1
.Parent.Selection.Hyperlinks.Add Anchor:=.Parent.Selection.Range, Address:=hyperadd, TextToDisplay:=disp
.Close -1
End With
oApp.Quit
Set oApp = Nothing
lbl_Exit:
Exit Sub
End Sub
Sub ScratchMacroII()
'A basic Word macro coded by Greg Maxey, http://gregmaxey.com/word_tips.html, 9/17/2018
Dim oApp As Object
Dim oDoc As Object
Dim hyperadd As String, disp As String
hyperadd = "www.google.com (http://www.google.com)"
disp = "Google"
Set oApp = CreateObject("Word.Application")
Set oDoc = oApp.Documents.Open("D:\Text.docx")
oDoc.Bookmarks("law").Select
oApp.Selection.MoveRight Unit:=1, Count:=8, Extend:=1
oApp.Selection.Hyperlinks.Add Anchor:=oApp.Selection.Range, Address:=hyperadd, TextToDisplay:=disp
oDoc.Close -1
oApp.Quit
Set oApp = Nothing
lbl_Exit:
Exit Sub
End Sub

neg
09-18-2018, 12:39 AM
Thanks Greg that worked, I just inserted "Add Anchor:=.Application.Selection.Range" into my code. :clap: