K
Ken Hudson
I can use VBA in Excel reasonably well. This is my first foray into word
macros and I am having trouble.
I am developing a macro that allows the user to select a text file to open.
Then I format the file to fit the width of one page. No problems there.
Next I want to insert page breaks. To do that I need to loop through each
row of the file (that's what I would do in Excel anyway) until I find the
phrase "REPORT ID:" without the quotes. When I find that phrase, I then want
to find the next string of characters following that phrase and set it equal
to a variable called "RptName."
At that point I can end the loop. (In Excel I would use an Exit For line of
code.)
I then use that variable in a search and replace function to insert a page
break.
The RptName starts one blank space after the phrase "REPORT ID:" and can
vary in length. If I were using Excel, I would use the InStr function to
isolate the variable name but don't know how to get there in word.
Feel free to amend the code below to make it more compact also, if you have
a moment.
-------------------------
Option Explicit
Dim RptName As String
Sub ImportRSDReports()
Application.ScreenUpdating = False
Dialogs(wdDialogFileOpen).Show
Selection.WholeStory
With ActiveDocument.PageSetup
.Orientation = wdOrientPortrait
.TopMargin = InchesToPoints(1)
.BottomMargin = InchesToPoints(1)
.LeftMargin = InchesToPoints(0.2)
.RightMargin = InchesToPoints(0.2)
.PageWidth = InchesToPoints(8.5)
.PageHeight = InchesToPoints(11)
End With
Selection.Font.Name = "Lucida Console"
Selection.Font.Size = 7
Selection.HomeKey
'I need to find the report name here and set it = RptName.
With WordApp.Selection.Find
.Text = RptName
.Replacement.Text = "^m" & RptName
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With
Application.ScreenUpdating = True
End Sub
macros and I am having trouble.
I am developing a macro that allows the user to select a text file to open.
Then I format the file to fit the width of one page. No problems there.
Next I want to insert page breaks. To do that I need to loop through each
row of the file (that's what I would do in Excel anyway) until I find the
phrase "REPORT ID:" without the quotes. When I find that phrase, I then want
to find the next string of characters following that phrase and set it equal
to a variable called "RptName."
At that point I can end the loop. (In Excel I would use an Exit For line of
code.)
I then use that variable in a search and replace function to insert a page
break.
The RptName starts one blank space after the phrase "REPORT ID:" and can
vary in length. If I were using Excel, I would use the InStr function to
isolate the variable name but don't know how to get there in word.
Feel free to amend the code below to make it more compact also, if you have
a moment.
-------------------------
Option Explicit
Dim RptName As String
Sub ImportRSDReports()
Application.ScreenUpdating = False
Dialogs(wdDialogFileOpen).Show
Selection.WholeStory
With ActiveDocument.PageSetup
.Orientation = wdOrientPortrait
.TopMargin = InchesToPoints(1)
.BottomMargin = InchesToPoints(1)
.LeftMargin = InchesToPoints(0.2)
.RightMargin = InchesToPoints(0.2)
.PageWidth = InchesToPoints(8.5)
.PageHeight = InchesToPoints(11)
End With
Selection.Font.Name = "Lucida Console"
Selection.Font.Size = 7
Selection.HomeKey
'I need to find the report name here and set it = RptName.
With WordApp.Selection.Find
.Text = RptName
.Replacement.Text = "^m" & RptName
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With
Application.ScreenUpdating = True
End Sub