View Full Version : Hey Guys
Aeneon
09-21-2012, 02:52 AM
Hey Guys,
I startet 2 weeks ago with programming but on Python...
Now i want to create a Makro on word (VBA) and i have some trouble with it.
I want a Makro wich is looking in the Document about keywords like /startrequirement and /endrequirement copy that and put into a new document.
But it should not create everytime a new document everything have to be in one document.
I have here a 70 Site document and i just want the most important sentence.
Now my task is just set on different places those keywords and let the macro do the rest.
My idea was something "find keyword position a" and "find keyword Position b" and select everything in between and copy that to the document without deleting the previous stuff on that.
Hope you can help me cause iīm a total newbie the only thing ive done jet was manipulate the code from kingsinger with the SplittingNote but my conclusion is splitting notes with 2 parameters is not possible :dunno
Ive tryed to find the keywords and make them "///" but it change the keywords and i dont like that because i dont know if its start or ends on this point :banghead:
Frosty
09-21-2012, 12:08 PM
Have you looked at wild card searching?
You should be able to do a wildcard search which does this... something along the lines of...
Can you play around with this macro (and explore the use of the native Find) to see if there is something that helps?
From there, you can record a macro that works.
Looping through a bunch of documents, using that find and copying the results to paste into a single document is fairly trivial.
But getting the right search parameters is something you will have to work on...
Sub SimpleWildcardFind()
Selection.Find.ClearFormatting
With Selection.Find
.text = "Keyword position a*Keyword position b"
.Replacement.text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
Selection.Find.Execute
End Sub
Aeneon
09-24-2012, 02:17 AM
this wildcard searching could be the solution i am looking for.
I will try to use this as far as I can.
Ill show you the complete code when iīm Finished sry that iīm answer this late but I dont have internet acces at home for the moment so i had to wait until work :thumb
Aeneon
09-25-2012, 02:47 AM
Hey i have some problems >.< first one:
my task changed again and i have to copy every note in a new document...
so i tried the code from you you postet on kingsinger with the split note.
So far i have no idea how to implement the wildcard inside wanna see the try of a total newbie wich feels so emberassed ?
i need some tips what i can do and what not >.< :help:help:(
Sub copystuff()
wildcardsearch "beginreq", "endreq", "notes"
End Sub
Public Sub wildcardsearch(strbegin As String, strend As String, strfilename As String)
Dim docNew As Document
Dim i As Long
Dim colNotes As Collection
Dim Search As String
Set colNotes = fGetCollectionofRanges(ActiveDocument, strbegin, strend)
If MsgBox("This will split the document into " & _
colNotes.Count & _
" sections. Do you wish to proceed?", vbYesNo) = vbNo Then
Exit Sub
End If
For i = 1 To colNotes.Count
Set docNew = Documents.Adde
colNotes(i).Copy
docNew.Content.Paste
docNew.SaveAs Filename:=ThisDocument.Path & "\" & strfilename & Format(i, "000"), FileFormat:=wdFormatDocument
docNew.Close SaveChanges:=True
Next
End Sub
Function fGetCollectionofRanges(oDoc As Document, strbegin As String, strend As String) As Collection
Dim colreturn As Collection
Dim rngsearch As Range
Dim rngfound As Range
Set colreturn = New Collection
Set rngsearch = oDoc.Content
Set rngfound = rngsearch.Duplicate
Do
With rngsearch.Find
.Text = "strbegin * strend"
.Execute
If .Found Then
rngfound.End = rngsearch.Start
colreturn.Add rngfound.Duplicate
rngsearch.Collapse wdCollapseEnd
rngfound.Start = rngsearch.Start
rngsearch.End = oDoc.Content.End
Else
Exit Do
End If
End With
Loop Until rngsearch.Start >= ActiveDocument.Content.End
Set fGetCollectionofRanges = colreturn
End Function
Frosty
09-25-2012, 04:12 AM
That's not a bad first try. 3 things I can see.
1. Documents.add instead of documents.Adde
2. .text = strbegin & " * " & strend
(You need the variable not to be inside of quotes)
3. .MatchWildcards = True
(Your search isn't a wildcard search yet)
Are you using option explicit? Are you trying to step through the code? This is a good first attempt, but you have to identify where it is failing. What is it not doing?
Aeneon
09-25-2012, 06:21 AM
oh i didnīt see the adde :O
With rngsearch.Find
.Text = "strbegin" * "strend"
.MatchWildcards = True ' ist this the right position ?
.Execute
my problem is it donīt find anything between strbegin and strend :/
if i go step by step it holds here and tell me runtime error '13' and Iīm sure they are both string so I dont know why >.<
Frosty
09-25-2012, 06:28 AM
Why are you putting quotes around your variables? Re-read my post please. You don't put quotes around variables, or you are setting up to look for the variable name, not the variable value.
Aeneon
09-26-2012, 12:30 AM
Ok worked so far xD
I didnīt know i have to use the & in between, i was wondering about and thought you just want that i have pay attention on the quotes :O
i thought its a function and it would work with >keypos. a * keypos. b < so why the & ? have they be connected
Aeneon
09-26-2012, 12:42 AM
Thank You a lot i would have never been able to find wildcard :O
and i do know that i have the & more than i expect :D
here the complete code if somebody want
Sub copystuff()
wildcardsearch "/beginreq", "/endreq", "notes"
End Sub
Public Sub wildcardsearch(strbegin As String, strend As String, strfilename As String)
Dim docNew As Document
Dim i As Long
Dim colNotes As Collection
Dim Search As String
Set colNotes = fGetCollectionofRanges(ActiveDocument, strbegin, strend)
If MsgBox("This will split the document into " & _
colNotes.Count & _
" sections. Do you wish to proceed?", vbYesNo) = vbNo Then
Exit Sub
End If
For i = 1 To colNotes.Count
Set docNew = Documents.Add
colNotes(i).Copy
docNew.Content.Paste
docNew.SaveAs Filename:=ThisDocument.Path & "\" & strfilename & Format(i, "000"), FileFormat:=wdFormatDocument
docNew.Close SaveChanges:=True
Next
End Sub
Function fGetCollectionofRanges(oDoc As Document, strbegin As String, strend As String) As Collection
Dim colreturn As Collection
Dim rngsearch As Range
Dim rngfound As Range
Set colreturn = New Collection
Set rngsearch = oDoc.Content
Set rngfound = rngsearch.Duplicate
Do
With rngsearch.Find
.MatchWildcards = True
.Text = strbegin & "*" & strend
.Execute
If .Found Then
rngfound.End = rngsearch.Start
colreturn.Add rngfound.Duplicate
rngsearch.Collapse wdCollapseEnd
rngfound.Start = rngsearch.Start
rngsearch.End = oDoc.Content.End
Else
Exit Do
End If
End With
Loop Until rngsearch.Start >= ActiveDocument.Content.End
Set fGetCollectionofRanges = colreturn
End Function
' thanks for your help Frosty :)
Frosty
09-26-2012, 09:08 AM
Sure thing, Aeneon.
Come back and ask more questions when you need. If I may make a suggestion-- try to give a better subject line than "Hey Guys" -- it would be better to define your question in the subject of your first post... something like "Select everything between keywords" would have been a good title for this post.
Aeneon
09-28-2012, 12:42 AM
Ok i was splitted if I sould introduce me first or ask first about my problem so i thought maybe both ?
:D
Iīll not introduce me in a subject line anymore for sure :)
Aeneon
10-17-2012, 05:32 AM
:O
i canīt understand that.
Ive returned to that macro and it wonīt work like last time maybe i didnīt noticed this but the programm donīt copy the stuff i need :O
i need just the stuff between /beginreq and /endreq but it copy everything until the first /startreq and then i donīt whats its copying >.<
hope somebody can help >.<
Aeneon
10-18-2012, 07:38 AM
@Frosty
.text = key 1 "*" key 2
my programm copy everything bevor and after the stuff between key 1 and key 2 so its the oposite of that i wantet :O
i saw something about wildcardīs with key1"?"key2 but it doesnīt work either so do you got a idea what i should do ?
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.