• Microsoft Word – Replace function returns compile error: expected array

    Home » Forums » AskWoody support » Productivity software by function » Visual Basic for Applications » Microsoft Word – Replace function returns compile error: expected array

    Author
    Topic
    #479180

    Hi All,

    I have a simple piece of code in a module on my Normal template:

    Code:
    Sub SearchString()
           Dim txt As String
           Dim newtxt As String
           txt = “Designation1/Designation2”
           newtxt = Replace(txt, “/”, vbCr)
           Selection.TypeText Text:=newtxt
        
    End Sub
    

    Works beautifully. BUT, take this same code into another module on another template (AuthorDesignation has already been defined and is a simple string variable) and I get a compile error: expected array. This happens in Word 2010 and Word 2003. Try as I might, I cannot see a difference between the two.

    Code:
    If AuthorDesignation  “” Then
        
               AuthorDesignation = Replace(AuthorDesignation, “/”, vbCr)   ‘if the designation goes on to two lines
               Selection.TypeText Text:=vbCr & AuthorDesignation    
    End If
    

    I’m not trying to deal with an array at all – I just want to remove the character / from the text.

    Any help would be appreciated.

    Thank you.

    Viewing 0 reply threads
    Author
    Replies
    • #1299662

      Do you have any other Replace method/function defined in the scope of that procedure? It could be furnished through an external library (e.g., listed under References) or be a separate function in your project.

      • #1299664

        Thank you so much. There was a string variable named Replace. Once I changed this to strReplace everything worked perfectly! Pity the error message didn’t just say I had an ambiguous name or something understandable (to me anyway :o).

    Viewing 0 reply threads
    Reply To: Microsoft Word – Replace function returns compile error: expected array

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: