By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,594 Members | 2,062 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,594 IT Pros & Developers. It's quick & easy.

For each... next help needed

P: 21
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 "thisdocument" doesn't work neither does "wholestory" anyone got any suggestions?

cheers Blacbox
Oct 29 '06 #1
Share this Question
Share on Google+
14 Replies


100+
P: 1,646
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 "thisdocument" doesn't work neither does "wholestory" anyone got any suggestions?

cheers Blacbox
Hi, perhaps you could use InStr(ThisDocument, " ' ")
Oct 29 '06 #2

Expert 5K+
P: 8,434
Hi, perhaps you could use InStr(ThisDocument, " ' ")
Given the nature of the For Each... statement, I think that would be Instr(variant, " ' ").
Oct 29 '06 #3

P: 21
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
Oct 30 '06 #4

Expert 5K+
P: 8,434
Could you paste in here the whole section of code?
Oct 30 '06 #5

P: 21
x = 1
With Selection
.GoTo what:=wdGoToPage
.MoveRight unit:=wdCharacter, Count:=NumCharsBefore
.TypeText Text:=Chr(146)
x = InStr(1, ThisDocument, "'")
.MoveRight unit:=wdCharacter, Count:=x
UserForm1.ListBox3.AddItem (Selection.Text)
Do
x = InStr(x, ThisDocument, "'")
.MoveRight unit:=wdCharacter, Count:=x
UserForm1.ListBox3.AddItem (Selection.Text)
Loop Until n = NumCharsBefore
End With

that's what i've got so far straight off the page
Oct 30 '06 #6

P: 21
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?
Oct 30 '06 #7

Expert 5K+
P: 8,434
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.
Oct 30 '06 #8

Expert 5K+
P: 8,434
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?
Oct 30 '06 #9

P: 21
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?
Oct 30 '06 #10

Expert 5K+
P: 8,434
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.
Expand|Select|Wrap|Line Numbers
  1. ? thisdocument
  2. Normal.dot
  3. ? activedocument
  4. Document1
  5. ? activedocument.Content
  6. Ds gkjsdklhgsdlgj dgj haksl haslkj fglaksf akslfasfsadf
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.
Oct 30 '06 #11

Expert 5K+
P: 8,434
Oh, and sorry, I don't have time to stick around. It's not night here, and I'm at work. :)
Oct 30 '06 #12

P: 21
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?
Oct 30 '06 #13

Expert 5K+
P: 8,434
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.
Oct 30 '06 #14

P: 21
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
Nov 1 '06 #15

Post your reply

Sign in to post your reply or Sign up for a free account.