Results 1 to 11 of 11
  1. #1
    New Lounger
    Join Date
    Feb 2002
    Location
    Oz
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Extending FindText into text boxes in Word macros (Office 2000)

    Hi everyone

    I'm new to the lounge and fairly new to VBA.

    I'm writing a versioning macro for Word that uses highlight of different colours to mark the different versions.

    This is now working pretty well except that I have a graphic in a text box that needs to change according to the version and the macro does not find it to change it.

    What's really annoying is that if I manually search for it in Word, the find dialog finds the stuff in the text box so it's only when find is used within the macro that it fails to find my graphics - aarrgggghhhh!

    Is there any way to force find to look in text boxes from a macro?

  2. #2
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: Extending FindText into text boxes in Word macros (Office 2000)

    M.,

    This came up recently either here or in the Word forum, as well as in some earlier threads - do a search on "TextFrame" to find earlier threads on this.
    The code you'd need would probably be something like this:

    <pre>Public Sub FindTextFrames()
    Dim aShape As Shape
    For Each aShape In ActiveDocument.Shapes
    If aShape.TextFrame.HasText Then
    With aShape.TextFrame.TextRange.Find
    'put find/replace code here
    End With
    End If
    Next 'aShape
    End Sub
    </pre>

    Gary

  3. #3
    New Lounger
    Join Date
    Feb 2002
    Location
    Oz
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Extending FindText into text boxes in Word macros (Office 2000)

    Thanks for trying Gary but I have tried your suggestion and also all the other variations in the previous thread on this topic and I cannot get it to find my graphics (I presumed I needed to turn field codes on to get it to see text in the text box but still no joy).

    Would anyone out there be willing to look at my entire macro and check it over for complete beginners gaffs and also see if there is something that I am missing on my finding problem (I've been looking at this for so long now I can't bear to look at the screen anymore!). It's not hugely long and I can cut down and send the files to play with too if required.

    Any help will save my sanity!

  4. #4
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: Extending FindText into text boxes in Word macros (Office 2000)

    If you can post a cutdown version of the file (with the full macros) here, that would help. Just be sure to remove any incriminating evidence before posting it! <img src=/S/grin.gif border=0 alt=grin width=15 height=15>

  5. #5
    New Lounger
    Join Date
    Feb 2002
    Location
    Oz
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Extending FindText into text boxes in Word macros (Office 2000)

    Thanks for looking at this.

    Here is the doc I am having the graphic problem with - front page, QPm or QPe logo.

    Looks like I can only upload one file at a time so I have put examples of all the version stuff in this doc. Let me know if having the other files would make life easier for you and I will upload cut down versions of them one at a time.

    Gee it doesn't take much to blow Word over the 100k!
    Attached Files Attached Files

  6. #6
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: Extending FindText into text boxes in Word macros (Office 2000)

    Melanie,

    Couldn't take more than a quick look at your macro tonight, but have a couple of questions already:

    Does the "real-live" document contain just the one text box (as in the sample document), or does it contain more than one? - the code you've got for dealing with the textboxes is quite complicated and is designed to deal with possible grouped textboxes (I remember writing part of it for Ed C. last year I think). Anyway, a lot of that complexity can be avoided here if (a) there are no grouped text boxes in your document and/or ([img]/forums/images/smilies/cool.gif[/img] there is only the one text box - much simpler code could be used.

    Are there some kind of field codes in the textbox that you are trying to show? - I see that you are switching field codes to show, but it's not clear why.

    Gary

  7. #7
    New Lounger
    Join Date
    Feb 2002
    Location
    Oz
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Extending FindText into text boxes in Word macros (Office 2000)

    Hi Gary

    At the moment, there is only the one text box in one of the documents. I don't think there will be any more but I would prefer not to limit the macro in case I do find a need for adding another one.

    As for grouping, it is very unlikely that I will group graphics in these docs so that stuff can be taken out (I had tried various non-group, cut down code; but reached breaking point after putting in the entire code from your previous go - and so this was what you got in my macro).

    I show the field codes in the vain hope that the reason my graphics are not being found is that they are graphics in a text box and that if I show codes instead (the graphics are linked), the find would see them as text and deal with them properly - as you can see this has not worked.

    Another approach I tried was to have highlighted paragraph markers to which separate text boxes were anchored and this worked if I wanted to delete the graphics but not when I hid them - oh I love the consistency in Word/VBA, *not*.

    I'm sure we'll get this working in time for Office.NET to scrap VBA <img src=/S/hairout.gif border=0 alt=hairout width=31 height=23>

    -Mel

  8. #8
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: Extending FindText into text boxes in Word macros (Office 2000)

    Hi again,

    Okay I'm finally starting to follow what you're doing here (I've never worked with these types of fields):
    The text box only contains a field - probably "IncludePicture" - to bring in the linked graphic (am guessing as the sample document has nothing in the textbox). The Find/Replace needs to replace something in the filepath specified in the field, so that a different picture will be linked (hope I've got that right(?)

    I think the key to what is going wrong in the code, is the way you are updating the fields - currently you're selecting the entire document, and updating the fields in the selection - this is leaving out the textbox 'cause the textbox doesn't get selected when you do a Select All.

    I'm being lazy here and not setting up a test for this, but it seems like it should work if you:

    Keep your line of code to show field codes.
    Replace all of that complicated text box code with something like the code I posted earlier in this thread.
    Run the Find/Replace code in the place indicated in that code snippet.
    Hide field codes.
    While the text in the textbox is still selected, update fields.
    Then deal with the rest of the document content; it may be necessary to reshow field codes etc.

    Gary

  9. #9
    New Lounger
    Join Date
    Feb 2002
    Location
    Oz
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Extending FindText into text boxes in Word macros (Office 2000)

    Nearly right!

    Yes there is an IncludePicture field in there but I don't actually replace the text in the path - that is an option I did consider and I'm sure there was a good reason why I chose not to do it that way - I just can't think what it was now.

    What I am trying to do is hide one graphic or the other, which, as they occupy the same space, should work - and again, if I do it manually in Word works fine.

    I will go with changing the text in the field the way you suggest and hopefully that should do the trick (if the reason why I discarded this option rears its ugly head again I'll let you know what it was).

    Other than that, is the basic structure reasonably elegant? I would rather knock any bad programming practices on the head early rather than have to change bad habits later.

    -Mel

  10. #10
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: Extending FindText into text boxes in Word macros (Office 2000)

    Hi again,

    I'm still a bit in the dark - the sample document posted contains neither a field nor graphics so I'm just guessing - in the live document, does the textbox contain an IncludePicture field that's just there doing nothing, and additionally the textbox contains two different graphics, one on top of the other? Or are the graphics not in the textbox nor the document, but just linked in via the IncludePicture field?

    The best way to flip the graphics will likely depend on the specific way you've got them set up; but without knowing that it's kind of hard to know what to do.

    If you click on my name on this post, you'll see my private e-mail address. If you don't mind, could you send me a sample of the document in its real-live state? (I'll understand if you can't of course.) I can have a look and then will report back here what I see.

    As far as programming practices, just some minor things to suggest:

    Always use the "Option Explicit" statement at the top of your code modules; this forces you to explicitly declare every variable - so for example:

    Dim qpmDocument As Document
    Dim docFound As Boolean
    etc...

    Where possible use range methods rather than selection methods.
    For example you can replace

    Selection.WholeStory
    Selection.Fields.Update

    with

    ActiveDocument.Range.Fields.Update

    The only other thing worth mentioning is that you are looping through the Documents collection three or four times - is it necessary to do this or can some of this be combined into fewer loops?

    Actually one more: Might it be possible to use DocProperty fields, in all the places in the document content where the QPm and QPe are to appear? - that way, all you'd need do is just change the custom document property and update the fields (i.e. wouldn't need to do all those find/replace loops throughout the documents to replace the text) - this would make the whole effort much simpler.

    Gary

  11. #11
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: Addendum - why not templates?

    Just an addendum to the previous note: don't know if this is applicable in your situation, but it seems like it would be more elegant to have these documents as templates.

    It seems pretty strenuous to have a set of documents, which you are continually flipping from "e" documents "m" documents and vice-versa as needed.

    Instead, have a set of templates for the needed documents, which can accommodate both "e" and "m" documents. This can be done via a simple userform which shows when a new document is created based on the template. Depending upon which one is chosen, all that needs to be done via code is to set the custom document property as needed, and updated all the docproperty fields in the document (as well as swap that pesky graphic).

    Seems like a simpler structure...

Posting Permissions

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