Consulting

Results 1 to 5 of 5

Thread: Wrong number of arguments or invalid property assignments inserting a hyperlink

  1. #1
    VBAX Newbie
    Joined
    Sep 2018
    Posts
    4
    Location

    Question Wrong number of arguments or invalid property assignments inserting a hyperlink

    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

  2. #2
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,335
    Location
    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"
      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.
    Greg

    Visit my website: http://gregmaxey.com

  3. #3
    VBAX Newbie
    Joined
    Sep 2018
    Posts
    4
    Location
    Hi Greg, yes I'm calling word from excel. I have tried .parent and it throws the same error. Thanks

  4. #4
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,335
    Location
    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"
      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"
      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
    Greg

    Visit my website: http://gregmaxey.com

  5. #5
    VBAX Newbie
    Joined
    Sep 2018
    Posts
    4
    Location
    Thanks Greg that worked, I just inserted "Add Anchor:=.Application.Selection.Range" into my code.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •