M
MAB
I'm trying to copy and paste 3 lines of text from one document to another
every time I find the word 'location' in a large (800 page) text file.
Code below works for the first 2 occurences (out of a possible 130+) then
stops.
' assumes original .txt file has been copied to clipboard before macro is run
Dim iPgs As Integer
' create source word doc
With ActiveDocument.PageSetup
.LineNumbering.Active = False
.Orientation = wdOrientLandscape
.TopMargin = CentimetersToPoints(3.17)
.BottomMargin = CentimetersToPoints(3.17)
.LeftMargin = CentimetersToPoints(2.54)
.RightMargin = CentimetersToPoints(2.54)
.Gutter = CentimetersToPoints(0)
.HeaderDistance = CentimetersToPoints(1.25)
.FooterDistance = CentimetersToPoints(1.25)
.PageWidth = CentimetersToPoints(29.7)
.PageHeight = CentimetersToPoints(21)
.FirstPageTray = wdPrinterDefaultBin
.OtherPagesTray = wdPrinterDefaultBin
.SectionStart = wdSectionNewPage
.OddAndEvenPagesHeaderFooter = False
.DifferentFirstPageHeaderFooter = False
.VerticalAlignment = wdAlignVerticalTop
.SuppressEndnotes = False
.MirrorMargins = False
.TwoPagesOnOne = False
.GutterPos = wdGutterPosLeft
End With
With Options
.CheckSpellingAsYouType = False
.CheckGrammarAsYouType = False
.SuggestSpellingCorrections = False
.SuggestFromMainDictionaryOnly = False
.CheckGrammarWithSpelling = False
.ShowReadabilityStatistics = False
.IgnoreUppercase = False
.IgnoreMixedDigits = True
.IgnoreInternetAndFileAddresses = True
.AllowCombinedAuxiliaryForms = True
.EnableMisusedWordsDictionary = True
.AllowCompoundNounProcessing = True
.UseGermanSpellingReform = True
End With
Selection.Font.Name = "LinePrinter"
Selection.Font.Size = 8.5
' paste .txt file into word source doc and get rid of blank first page
Selection.Paste
Selection.HomeKey Unit:=wdStory
Selection.Delete Unit:=wdCharacter, Count:=1
Selection.Delete Unit:=wdCharacter, Count:=1
' create target word doc
Documents.Add DocumentType:=wdNewBlankDocument
With ActiveDocument.PageSetup
.LineNumbering.Active = False
.Orientation = wdOrientLandscape
.TopMargin = CentimetersToPoints(3.17)
.BottomMargin = CentimetersToPoints(3.17)
.LeftMargin = CentimetersToPoints(2.54)
.RightMargin = CentimetersToPoints(2.54)
.Gutter = CentimetersToPoints(0)
.HeaderDistance = CentimetersToPoints(1.25)
.FooterDistance = CentimetersToPoints(1.25)
.PageWidth = CentimetersToPoints(29.7)
.PageHeight = CentimetersToPoints(21)
.FirstPageTray = wdPrinterDefaultBin
.OtherPagesTray = wdPrinterDefaultBin
.SectionStart = wdSectionNewPage
.OddAndEvenPagesHeaderFooter = False
.DifferentFirstPageHeaderFooter = False
.VerticalAlignment = wdAlignVerticalTop
.SuppressEndnotes = False
.MirrorMargins = False
.TwoPagesOnOne = False
.GutterPos = wdGutterPosLeft
End With
With Options
.CheckSpellingAsYouType = False
.CheckGrammarAsYouType = False
.SuggestSpellingCorrections = False
.SuggestFromMainDictionaryOnly = False
.CheckGrammarWithSpelling = False
.ShowReadabilityStatistics = False
.IgnoreUppercase = False
.IgnoreMixedDigits = True
.IgnoreInternetAndFileAddresses = True
.AllowCombinedAuxiliaryForms = True
.EnableMisusedWordsDictionary = True
.AllowCompoundNounProcessing = True
.UseGermanSpellingReform = True
End With
Selection.Font.Name = "LinePrinter"
Selection.Font.Size = 8.5
' go back to Source doc and copy report header to target doc
Windows(1).Activate
Selection.MoveDown Unit:=wdLine, Count:=15, Extend:=wdExtend
Selection.Copy
Windows(2).Activate
Selection.Paste
' go back to Source doc
Windows(1).Activate
Selection.MoveUp Unit:=wdLine, Count:=1
' Main Loop: Find word "Location", select 3 lines, copy and paste to Target
document
For iPgs = 1 To ActiveDocument.BuiltInDocumentProperties.Item("number of
pages")
Selection.Find.ClearFormatting
With Selection.Find
.Text = "location"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.MoveDown Unit:=wdLine, Count:=3, Extend:=wdExtend
Selection.Copy
Windows(2).Activate
Selection.Paste
Windows(1).Activate
Selection.MoveUp Unit:=wdLine, Count:=1
Selection.MoveDown Unit:=wdLine, Count:=2
Next
End Sub
every time I find the word 'location' in a large (800 page) text file.
Code below works for the first 2 occurences (out of a possible 130+) then
stops.
' assumes original .txt file has been copied to clipboard before macro is run
Dim iPgs As Integer
' create source word doc
With ActiveDocument.PageSetup
.LineNumbering.Active = False
.Orientation = wdOrientLandscape
.TopMargin = CentimetersToPoints(3.17)
.BottomMargin = CentimetersToPoints(3.17)
.LeftMargin = CentimetersToPoints(2.54)
.RightMargin = CentimetersToPoints(2.54)
.Gutter = CentimetersToPoints(0)
.HeaderDistance = CentimetersToPoints(1.25)
.FooterDistance = CentimetersToPoints(1.25)
.PageWidth = CentimetersToPoints(29.7)
.PageHeight = CentimetersToPoints(21)
.FirstPageTray = wdPrinterDefaultBin
.OtherPagesTray = wdPrinterDefaultBin
.SectionStart = wdSectionNewPage
.OddAndEvenPagesHeaderFooter = False
.DifferentFirstPageHeaderFooter = False
.VerticalAlignment = wdAlignVerticalTop
.SuppressEndnotes = False
.MirrorMargins = False
.TwoPagesOnOne = False
.GutterPos = wdGutterPosLeft
End With
With Options
.CheckSpellingAsYouType = False
.CheckGrammarAsYouType = False
.SuggestSpellingCorrections = False
.SuggestFromMainDictionaryOnly = False
.CheckGrammarWithSpelling = False
.ShowReadabilityStatistics = False
.IgnoreUppercase = False
.IgnoreMixedDigits = True
.IgnoreInternetAndFileAddresses = True
.AllowCombinedAuxiliaryForms = True
.EnableMisusedWordsDictionary = True
.AllowCompoundNounProcessing = True
.UseGermanSpellingReform = True
End With
Selection.Font.Name = "LinePrinter"
Selection.Font.Size = 8.5
' paste .txt file into word source doc and get rid of blank first page
Selection.Paste
Selection.HomeKey Unit:=wdStory
Selection.Delete Unit:=wdCharacter, Count:=1
Selection.Delete Unit:=wdCharacter, Count:=1
' create target word doc
Documents.Add DocumentType:=wdNewBlankDocument
With ActiveDocument.PageSetup
.LineNumbering.Active = False
.Orientation = wdOrientLandscape
.TopMargin = CentimetersToPoints(3.17)
.BottomMargin = CentimetersToPoints(3.17)
.LeftMargin = CentimetersToPoints(2.54)
.RightMargin = CentimetersToPoints(2.54)
.Gutter = CentimetersToPoints(0)
.HeaderDistance = CentimetersToPoints(1.25)
.FooterDistance = CentimetersToPoints(1.25)
.PageWidth = CentimetersToPoints(29.7)
.PageHeight = CentimetersToPoints(21)
.FirstPageTray = wdPrinterDefaultBin
.OtherPagesTray = wdPrinterDefaultBin
.SectionStart = wdSectionNewPage
.OddAndEvenPagesHeaderFooter = False
.DifferentFirstPageHeaderFooter = False
.VerticalAlignment = wdAlignVerticalTop
.SuppressEndnotes = False
.MirrorMargins = False
.TwoPagesOnOne = False
.GutterPos = wdGutterPosLeft
End With
With Options
.CheckSpellingAsYouType = False
.CheckGrammarAsYouType = False
.SuggestSpellingCorrections = False
.SuggestFromMainDictionaryOnly = False
.CheckGrammarWithSpelling = False
.ShowReadabilityStatistics = False
.IgnoreUppercase = False
.IgnoreMixedDigits = True
.IgnoreInternetAndFileAddresses = True
.AllowCombinedAuxiliaryForms = True
.EnableMisusedWordsDictionary = True
.AllowCompoundNounProcessing = True
.UseGermanSpellingReform = True
End With
Selection.Font.Name = "LinePrinter"
Selection.Font.Size = 8.5
' go back to Source doc and copy report header to target doc
Windows(1).Activate
Selection.MoveDown Unit:=wdLine, Count:=15, Extend:=wdExtend
Selection.Copy
Windows(2).Activate
Selection.Paste
' go back to Source doc
Windows(1).Activate
Selection.MoveUp Unit:=wdLine, Count:=1
' Main Loop: Find word "Location", select 3 lines, copy and paste to Target
document
For iPgs = 1 To ActiveDocument.BuiltInDocumentProperties.Item("number of
pages")
Selection.Find.ClearFormatting
With Selection.Find
.Text = "location"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.MoveDown Unit:=wdLine, Count:=3, Extend:=wdExtend
Selection.Copy
Windows(2).Activate
Selection.Paste
Windows(1).Activate
Selection.MoveUp Unit:=wdLine, Count:=1
Selection.MoveDown Unit:=wdLine, Count:=2
Next
End Sub