For each... next help needed

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Blckbx
    New Member
    • Oct 2006
    • 21

    For each... next help needed

    For Each variant In ThisDocument
    [statements]
    Next variant

    for the above code i need VBA to scan all of the document and find each " ' " but "thisdocume nt" doesn't work neither does "wholestory " anyone got any suggestions?

    cheers Blacbox
  • willakawill
    Top Contributor
    • Oct 2006
    • 1646

    #2
    Originally posted by Blckbx
    For Each variant In ThisDocument
    [statements]
    Next variant

    for the above code i need VBA to scan all of the document and find each " ' " but "thisdocume nt" doesn't work neither does "wholestory " anyone got any suggestions?

    cheers Blacbox
    Hi, perhaps you could use InStr(ThisDocum ent, " ' ")

    Comment

    • Killer42
      Recognized Expert Expert
      • Oct 2006
      • 8429

      #3
      Originally posted by willakawill
      Hi, perhaps you could use InStr(ThisDocum ent, " ' ")
      Given the nature of the For Each... statement, I think that would be Instr(variant, " ' ").

      Comment

      • Blckbx
        New Member
        • Oct 2006
        • 21

        #4
        thanks but when i use

        x = InStr(1, ThisDocument, "'")

        firstly if i leave out the 1 it takes "this document" as the start and secondly instr returns a value of 0 why? there definitely are ' in the text.

        any suggestions? cheeers

        blackbox

        Comment

        • Killer42
          Recognized Expert Expert
          • Oct 2006
          • 8429

          #5
          Could you paste in here the whole section of code?

          Comment

          • Blckbx
            New Member
            • Oct 2006
            • 21

            #6
            x = 1
            With Selection
            .GoTo what:=wdGoToPag e
            .MoveRight unit:=wdCharact er, Count:=NumChars Before
            .TypeText Text:=Chr(146)
            x = InStr(1, ThisDocument, "'")
            .MoveRight unit:=wdCharact er, Count:=x
            UserForm1.ListB ox3.AddItem (Selection.Text )
            Do
            x = InStr(x, ThisDocument, "'")
            .MoveRight unit:=wdCharact er, Count:=x
            UserForm1.ListB ox3.AddItem (Selection.Text )
            Loop Until n = NumCharsBefore
            End With

            that's what i've got so far straight off the page

            Comment

            • Blckbx
              New Member
              • Oct 2006
              • 21

              #7
              to be honest i might just elave it as i managed to re-write the code so it works using if then statements, roughly the same lenght just takes abit longer. the real reason i came on here again is a question about variants, if i want a to paste the entire contents of a variant how would i go about doing that?

              Comment

              • Killer42
                Recognized Expert Expert
                • Oct 2006
                • 8429

                #8
                Originally posted by Blckbx
                to be honest i might just elave it as i managed to re-write the code so it works using if then statements, roughly the same lenght just takes abit longer. the real reason i came on here again is a question about variants, if i want a to paste the entire contents of a variant how would i go about doing that?
                Getting outside my area of expertise there, but I imagine it would depend on the contents (type) of the variant and where you want to paste it.

                Comment

                • Killer42
                  Recognized Expert Expert
                  • Oct 2006
                  • 8429

                  #9
                  Maybe it's just me, but when I run a macro and check, ThisDocument refers to the Normal.Dot template. Perhaps you have the same problem?

                  Comment

                  • Blckbx
                    New Member
                    • Oct 2006
                    • 21

                    #10
                    have sorted the variant problem just used do...loop

                    when i refer to a thisdocument it refers to the actual document as the code is written in the project rather than the normal.

                    i could go on asking problems all night if you want to stay and help?

                    Comment

                    • Killer42
                      Recognized Expert Expert
                      • Oct 2006
                      • 8429

                      #11
                      Originally posted by Blckbx
                      have sorted the variant problem just used do...loop

                      when i refer to a thisdocument it refers to the actual document as the code is written in the project rather than the normal.

                      i could go on asking problems all night if you want to stay and help?
                      I was actually editing my earlier reply, but TheScripts told me I had run out of time (apparently you only have 5 minutes after posting to edit). Here's what I was about to post...


                      Perhaps ActiveDocument or ActiveDocument. Content would work? After interrupting macro execution I went to the immediate window and printed some things. Here's the result.
                      Code:
                      ? thisdocument
                      [b]Normal.dot[/b]
                      ? activedocument
                      [b]Document1[/b]
                      ? activedocument.Content
                      [b]Ds gkjsdklhgsdlgj dgj haksl haslkj fglaksf akslfasfsadf[/b]
                      This garbage string is the text I had typed into the test document, of course.

                      So, it looks as though your code is (or was) actually scanning the name of either the document or the template, depending on your setup.

                      Comment

                      • Killer42
                        Recognized Expert Expert
                        • Oct 2006
                        • 8429

                        #12
                        Oh, and sorry, I don't have time to stick around. It's not night here, and I'm at work. :)

                        Comment

                        • Blckbx
                          New Member
                          • Oct 2006
                          • 21

                          #13
                          instr still returns a value of "0" it's probably something to do with the text i'm using.

                          also do you happen to know / suggest code for counting the frequency of each letter in a document? i have managed to find some but it's rather long, if not could you tell me how to only find and replace in a certain part of the document?

                          Comment

                          • Killer42
                            Recognized Expert Expert
                            • Oct 2006
                            • 8429

                            #14
                            Originally posted by Blckbx
                            instr still returns a value of "0" it's probably something to do with the text i'm using.

                            also do you happen to know / suggest code for counting the frequency of each letter in a document? i have managed to find some but it's rather long, if not could you tell me how to only find and replace in a certain part of the document?
                            I'm really curious about the Instr - could you paste the latest version of the statement here? It's usually pointless when people just describe code, as it's the fine details which make the difference, and they're always left out.

                            As for counting a letter, I know the Find function can do that - to demonstrate, do a Find in Word, check the "Highlight all items found..." checkbox, and hit the Find All button. As well as highlighting (selecting) all of the occurrences found, it tells you the count in the dialogue box. So it should be possible to retrieve the same information in code. I don't know how though, sorry. (Be careful afterward, as the highlighted items are in fact selected, just as though you dragged over them.)

                            As for replacing in a certain section, I expect it's just a matter of selecting the text then using the usual replace function/method, but on the Selection object. There are bound to be others in the forum who can provide better detail on this.

                            Comment

                            • Blckbx
                              New Member
                              • Oct 2006
                              • 21

                              #15
                              have managed to get it all to work, i made a silly mistake with the instr, it wouldnt let me search for chr(146) and i wouldn't let me use ' either so i used " ' " (without the spaces) so then it searched for exactly what i aske dit to, so used a var insteaed. small question: when searchnig through a certain part of a document it says it's finished and would i like to search the rest? the answer is always no but unfortunately i have to tell it this 104 times, is there anyway of automating it?

                              cheers Blackbox

                              Comment

                              Working...