PDA

View Full Version : Add a macro to Document



illusionati
05-09-2011, 06:11 PM
Hey,

I have reports that i export to Word and then use a macro to add an additional frame that the software doesnt allow to add.

The report has a bunch of frames that it transfers over to the document.

I need to change what looks like this "Order Number: WO-0012345" to "*WO-0012345*". It is in a frame and I am able to change the "Order Number: " to "*" by using a find replace macro.

The reports are batch printed every report page has a different order number. So i dont know how to write a macro that works for that.

I also need to change its font to a barcode after the asterisks are added.

If anyone can help it would be appreciated.

illusionati
05-09-2011, 07:27 PM
btw here is the macro i tried to use last which was unsucessful

With Selection.Find
.ClearFormatting
.Text = "Order Number: "
.Replacement.ClearFormatting
.Replacement.Text = "*"
Selection.Find.Execute Replace:=wdReplaceAll
Selection.EndKey Unit:=wdLine
Selection.InsertAfter "*"
End With

macropod
05-09-2011, 07:32 PM
Hi illusionati,

If you use a wildcard Find/Replace, you can use:
Find = Order Number: ([A-Z]{2}-[0-9]{7})
Replace = *\1*
This will find the 'Order Number: ' string, followed by any two upper-case characters and a hyphen then 7 digits. The 'Order Number: ' will be deleted and asterisks will be inserted either side of the found string.

illusionati
05-10-2011, 01:36 AM
Hi illusionati,

If you use a wildcard Find/Replace, you can use:
Find = Order Number: ([A-Z]{2}-[0-9]{7})
Replace = *\1*
This will find the 'Order Number: ' string, followed by any two upper-case characters and a hyphen then 7 digits. The 'Order Number: ' will be deleted and asterisks will be inserted either side of the found string.

Hey macropod,

Thanks for the reply, but i tried and it didnt work

this is what i did

Dim number As String


With Selection.Find
.ClearFormatting
.Text = "Order Number: ([A-Z]{2}-[0-9]{7})"
.Replacement.Text = "*\1*"
Selection.Find.Execute Replace:=wdReplaceAll
End With

Please let me know whats wrong and what I should change. Forgive my knowledge in this matter.

macropod
05-10-2011, 01:52 AM
Hi illusionati,

I don't see any indication in the snippet you posted as to whether you's using '.MatchWildcards = True'. Neither do I see any indication of whether you're searching within the frame referred to in your earlier post. Finally, I can't see the relevance of 'Dim number As String' to the snippet.

illusionati
05-10-2011, 02:41 AM
Hi illusionati,

I don't see any indication in the snippet you posted as to whether you's using '.MatchWildcards = True'. Neither do I see any indication of whether you're searching within the frame referred to in your earlier post. Finally, I can't see the relevance of 'Dim number As String' to the snippet.

Macropod,

I am completely new to this VBA thing and am completely clueless as to what is going on.

I can attach the Document for you to see what it looks like if you want.

I can program in VB6 and tried to use my knowledge in that and hence you see the number as string.

macropod
05-10-2011, 03:27 AM
Hi illusionati,

Probably overkill (because I don't really know where your data are), but try:
Sub Demo()
Application.ScreenUpdating = False
Dim RngStory As Range
For Each RngStory In ActiveDocument.StoryRanges
With RngStory.Find
.ClearFormatting
.Text = "Order Number: ([A-Z]{2}-[0-9]{7})"
.Replacement.Text = "*\1*"
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = True
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute Replace:=wdReplaceAll
End With
Next RngStory
Application.ScreenUpdating = True
End Sub

illusionati
05-10-2011, 03:50 AM
I absolutely appreciate your help, but I have no idea whats wrong.

If you can, please have a look at my data.

macropod
05-10-2011, 04:13 AM
Hi illusionati,

Your data don't have an ordinary hyphen in the "Order Number". Since it appears you're trying t turn it into a 3of9 barcode, you'll probably nead an ordinary hyphen there. Change the Find/Replace code to:

.Text = "Order Number: ([A-Z]{2})?([0-9]{7})"
.Replacement.Text = "*\1-\2*"

illusionati
05-10-2011, 04:23 AM
Wow Thanks a lot!

You are a genius!