Need help in creating macro
-
Hi Guys. this is Jun. one of @Joyfano member i would like to open a new topic about creating macro in ms word.
I am currently creating a macro in ms word but i'm having a really hard time. this is what i wanted to do:
i want to create a macro that can search a specific word or text lets say in document1.doc then store the result in an array and after that i will search again in another document lets say document2.doc using the stored result in my first search and if i find the word it will go back to document1.doc to copy the whole paragraph and paste it in document2.doc but take note that it will paste in specific location.
my code is working fine for only the first paragraph my problem is i want to do that in another paragraph. but that paragraph have different word or text.here's my code:
Sub Mosaic1()
'Dim MyAr() As String Dim i As Long i = 0 Selection.Find.ClearFormatting With Selection.Find .Text = "\webfiles*?.pdf" .Replacement.Text = "" .Forward = True .Wrap = wdFindContinue .Format = False .MatchCase = False .MatchWholeWord = False .MatchAllWordForms = False .MatchSoundsLike = False .MatchWildcards = True End With Selection.Find.Execute ReDim Preserve MyAr(i) MyAr(i) = Selection i = i + 1 Windows( _ "Document2.doc" _ ).Activate Selection.Find.ClearFormatting With Selection.Find .Text = "(i)" .Replacement.Text = "" .Forward = True .Wrap = wdFindContinue .Format = False .MatchCase = False .MatchWholeWord = False .MatchAllWordForms = False .MatchSoundsLike = False .MatchWildcards = True End With Selection.Find.Execute Selection.MoveRight Unit:=wdCharacter, Count:=21 Selection.MoveDown Unit:=wdLine, Count:=1 Selection.MoveRight Unit:=wdCharacter, Count:=29 Windows("Document1").Activate Selection.MoveLeft Unit:=wdCharacter, Count:=1 Selection.MoveDown Unit:=wdLine, Count:=1 Selection.MoveUp Unit:=wdLine, Count:=2, Extend:=wdExtend Selection.Cut Windows( _ "Document2.doc" _ ).Activate Selection.PasteAndFormat wdPasteDefault Selection.TypeParagraph Selection.MoveDown Unit:=wdLine, Count:=1 Selection.MoveRight Unit:=wdCharacter, Count:=4 Windows("Document1").Activate Selection.MoveDown Unit:=wdLine, Count:=1
End Sub
-
@junrustia What will you be storing to your array? The found text? The location or index of the text to find?
-
I want to store the found text in the array. but it seem's that i put a wrong code because it only find's letter "i"
-
It has been SOO long since I worked with VBA (or whateve they call it now) that I cannot even follow this easily.
-
@junrustia said:
I want to store the found text in the array. but it seem's that i put a wrong code because it only find's letter "i"
That's why I asked :D. Check again your code.
-
@JaredBusch Here in the Philippines, there are still lots of big company that uses VBA.
-
@Pol said:
@JaredBusch Here in the Philippines, there are still lots of big company that uses VBA.
VBA continues to be used because it is the language of MS Office apps. If you need to automate your office suite, it is the only choice.
-
@JaredBusch said:
It has been SOO long since I worked with VBA (or whateve they call it now) that I cannot even follow this easily.
@scottalanmiller said:
@Pol said:
@JaredBusch Here in the Philippines, there are still lots of big company that uses VBA.
VBA continues to be used because it is the language of MS Office apps. If you need to automate your office suite, it is the only choice.
Yes...my office is FULL of Excel sheets and many with VBA and I don't ever see us going away from it. While kind of a messy language, it is perfect for small businesses. And another reason I am getting my MOS Master and hitting the VBA books. They want me to be a programmer and a "know it all" in Office in addition to my System Admin duties.
-
@garak0410 only perfect for small business because it is all that there is for office automation on Ms Office. VBA is a horrible language for small businesses to be saddled with, though.
LibreOffice uses BASIC, JavaScript, Python or BeanShell. So you can choose.
-
@scottalanmiller said:
@garak0410 only perfect for small business because it is all that there is for office automation on Ms Office. VBA is a horrible language for small businesses to be saddled with, though.
LibreOffice uses BASIC, JavaScript, Python or BeanShell. So you can choose.
If, God willing, I am in this job for years to come, I plan on sharpening my skills in other languages in hopes we can get off of VBA...but for now, its a way of life.
-
@junrustia So I had a chance to look at this again today. Your overall premise is not simple. Where is document1.doc coming from? how is the data being placed in it put there?
You stated that you want to use word 1 in document 1 as the search for document 2. then to select an entire section of document 1 and paste it into document 2 where word 1 was found in document 2.
You need to have some intelligent organization to document 1 if you plan on being able to do this. Document 1 is your "control" document and its structure will have to be known in order to use it as a base to loop through.
Right now your code is not really doing any selecting based on objects (words). you are simply using the find then using the selection.move stuff that looks like it was part of a recorded macro.
Your entire subroutine here is also needing to be in some type of loop for it to execute more than once. I do not see any looping mechanism. That is why it only executes once.
Finally. you are telling document 2 to specifically search for i. You need to change .Text = "(i)" to .Text = MyAr(i) or maybe to .Text = "(" & MyAr(i) & ")"
-
Hi @JaredBusch you absolutely understand what i'm trying to do but I already done with this project.
but anyway thank you for your reply i really appreciate it. maybe a few weeks later i will ask again for your help. hahaha