Replace() -- Syntax Error

Posted on 2005-04-26
Last Modified: 2010-05-02
Why doesnt':
--Dim currname As String
--currname = ActiveDocument.FullName
--replace(currname," ","%20",1,-1,1)
work? (at least in Word 2003's implementation of VBA)? I get, "Compile Error. Expected =" I think I am faithfully following the syntax described in the Word/VB help file (see end of this posting), but obviously I am not if I get an error message. My goal is to create a string variable equal to the complete filename but with any spaces replaced with "%20" (minus the quotes). The result will be a click-able hyperlink when pasted into an email.


Replace Function
  Description -- Returns a string in which a specified substring has been replaced with another substring a specified number of times.
  Syntax -- Replace(expression, find, replace[, start[, count[, compare]]])
   -- expression = String expression containing substring to replace.
   -- find = Substring being searched for.
   -- replace = Replacement substring.
   -- start (Optional) =  Position within expression where substring search is to begin. If omitted, 1 is assumed.
   -- count (Optional) =  Number of substring substitutions to perform. If omitted, the default value is –1, which means make all possible substitutions.
   -- compare (Optional) =  Numeric value indicating the kind of comparison to use when evaluating substrings where:
    == vbUseCompareOption –1 Performs a comparison using the setting of the Option Compare statement.
    == vbBinaryCompare 0 Performs a binary comparison.
    == vbTextCompare 1 Performs a textual comparison.
    == vbDatabaseCompare 2 Microsoft Access only. Performs a comparison based on information in your database.

Question by:GrayStrickland
    LVL 15

    Accepted Solution


    I think it is just that you need to return the replace to something like this

    Dim currname As String
    currname = ActiveDocument.FullName
    currname = replace(currname," ","%20",1,-1,1)

    the replace function returns the new string back to the currname variable



    LVL 35

    Assisted Solution

    by:[ fanpages ]
    This works for me...

    ? Replace(ActiveDocument.FullName, " ", "%20")

    What do you see that is unexpected?


    LVL 15

    Expert Comment

    As well you can also just use:

    Dim currname As String
    currname = replace(ActiveDocument.FullName," ","%20",1,-1,1)

    Saves a line of code which never hurts :)


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Suggested Solutions

    Introduction While answering a recent question ( in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
    Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
    As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
    Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    7 Experts available now in Live!

    Get 1:1 Help Now