Results 1 to 3 of 3
  1. #1
    New Lounger
    Join Date
    Sep 2008
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Doc VBA Issues (2003 SP3)

    I'm trying to do a couple of things that seem pretty simple, but my search of Word Help and the Internet haven't turned up anything helpful:

    - Search and replace text in a string. I'm saving a [filename].in file as [filename].in.htm, and I want to remove the ".in" part. I've tried working with the Instr() function, but haven't gotten far enough.
    - Save that same file to the location of the original file. For some reason, it's saving it to the last Save As location that I used. I thought there would be some kind of Activedocument.CurrentDirectory property, but I can't find anything.

    Thanks,
    Bob

  2. #2
    Silver Lounger
    Join Date
    Jul 2001
    Location
    Ottawa, Ontario, Canada
    Posts
    1,609
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Doc VBA Issues (2003 SP3)

    Hi Bob
    The following example demonstrates how to search and replace text in a string.<div style="width: 100%; background-color: #FFFFFF;"><code><font color=black><font color=blue>Option</font color=blue> <font color=blue>Explicit</font color=blue>

    <font color=blue>Sub</font color=blue> SearchAndReplace()
    <font color=blue>Dim</font color=blue> strOrig <font color=blue>As</font color=blue> <font color=blue>String</font color=blue> <font color=448800>'<font color=blue>String</font color=blue> being searched</font color=448800>
    <font color=blue>Dim</font color=blue> strSrch <font color=blue>As</font color=blue> <font color=blue>String</font color=blue> <font color=448800>' <font color=blue>String</font color=blue> being replaced</font color=448800>
    <font color=blue>Dim</font color=blue> strRep <font color=blue>As</font color=blue> <font color=blue>String</font color=blue> <font color=448800>' Replacement string</font color=448800>
    <font color=blue>Dim</font color=blue> locn <font color=blue>As</font color=blue> <font color=blue>Long</font color=blue> 'Location of the start of the string being replaced
    <font color=blue>Dim</font color=blue> strPre <font color=blue>As</font color=blue> <font color=blue>String</font color=blue> <font color=448800>'String before that to be replaced</font color=448800>
    <font color=blue>Dim</font color=blue> strPost <font color=blue>As</font color=blue> String <font color=448800>' String after that to be replaced</font color=448800>
    <font color=blue>Dim</font color=blue> strFinal <font color=blue>As</font color=blue> String <font color=448800>'Resultant string</font color=448800>

    strOrig = "Myfilename.in.doc"
    strSrch = ".in"
    strRep = "OUT"
    locn = InStr(1, strOrig, strSrch)
    strPre = Left(strOrig, locn - 1)
    strPost = Right(strOrig, Len(strOrig) - locn - Len(strRep) + 1)
    strFinal = strPre & strRep & strPost
    <font color=blue>End</font color=blue> <font color=blue>Sub</font color=blue>
    </font color=black></code></div hiblock>
    Regards
    Don

  3. #3
    Silver Lounger
    Join Date
    Jul 2001
    Location
    Ottawa, Ontario, Canada
    Posts
    1,609
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Doc VBA Issues (2003 SP3)

    The property you want is ActiveDocument.FullName or ActiveDocument.Path.
    Regards
    Don

Posting Permissions

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