I want to replace every occurrence of the string "#PAGEBREAK# with an actual pagebreak. This is what I came up with:
Sub InsertPageBreak()
Application.ScreenUpdating = False
With ActiveDocument.Range
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "#PAGEBREAK#"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
.InsertBreak Type:=wdPageBreak
.Execute
End With
End With
Application.ScreenUpdating = True
End Sub
What actually happens: The string "#PAGEBREAK#" is exchanged for an empty string. The .Find works as intended but I get the error message:
Method or Object not found
on the
.InsertBreak Type:= wdPageBreak
What methods could be used here in which way?
This will work For you:
Sub InsertPageBreak()
ActiveDocument.Range.Select
With Selection.Find
.Text = "#PAGEBREAK#"
.Execute
End With
If Selection.Find.Found Then
Selection.GoTo What:=wdGoToBookmark, Name:="\Page"
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.MoveLeft Unit:=wdCharacter, Count:=1
Selection.InsertBreak Type:=wdPageBreak
End If
End Sub
If you want to Replace all of the "#PAGEBREAK#", use this below code:
Sub InsertPageBreak()
ActiveDocument.Range.Select
Do
With Selection.Find
.Text = "#PAGEBREAK#"
.Execute
End With
If Selection.Find.Found Then
Selection.GoTo What:=wdGoToBookmark, Name:="\Page"
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.MoveLeft Unit:=wdCharacter, Count:=1
Selection.InsertBreak Type:=wdPageBreak
Else: Exit Sub
End If
Loop
End Sub
Related
I need to add a blank page after all pages containing a specifc word like "S U M M A R Y ".
Sub SelFind()
Dim oRng As Range
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = "S U M M A R Y "
Selection.GoTo What:=wdGoToBookmark, Name:="\Page"
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.MoveLeft Unit:=wdCharacter, Count:=1
Selection.InsertBreak Type:=wdPageBreak
End With
lbl_Exit:
Exit Sub
End Sub
This is doing it for a single page. How can I loop through all the pages.
A Simple Do Loop will work for you:
Sub SelFind()
ActiveDocument.Range.Select
Do
With Selection.Find
.Text = "S U M M A R Y "
.Execute
End With
If Selection.Find.Found Then
Selection.GoTo What:=wdGoToBookmark, Name:="\Page"
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.MoveLeft Unit:=wdCharacter, Count:=1
Selection.InsertBreak Type:=wdPageBreak
Else: GoTo nxt
End If
Loop
nxt:
ActiveDocument.Range.Select
Do
With Selection.Find
.Text = "R O Y A L T Y "
.Execute
End With
If Selection.Find.Found Then
Dim Rnddg As Integer
Rnddg = Selection.Information(wdActiveEndPageNumber)
If Rnddg Mod 2 > 0 Then
Selection.GoTo What:=wdGoToBookmark, Name:="\Section"
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.MoveLeft Unit:=wdCharacter, Count:=1
Selection.InsertBreak Type:=wdPageBreak
End If
Else: Exit Sub
End If
Loop
End Sub
You might try something based on:
Sub Demo()
Application.ScreenUpdating = False
Dim Rng As Range
With ActiveDocument.Range
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "S U M M A R Y "
.Replacement.Text = ""
.Forward = True
.Format = False
.Wrap = wdFindStop
.Execute
End With
Do While .Find.Found
Set Rng = .Duplicate
Set Rng = Rng.GoTo(What:=wdGoToPage, Name:=.Information(wdActiveEndPageNumber))
Set Rng = Rng.GoTo(What:=wdGoToBookmark, Name:="\Page")
Rng.Paragraphs.Last.Range.InsertAfter Chr(12) & Chr(12)
.Collapse wdCollapseEnd
.Find.Execute
Loop
End With
Application.ScreenUpdating = True
End Sub
I have a problem with my code. It does not recognize the Empty Cell.
The condition is "If the cell is empty then repeat, else change the font of the text in the cell above the current cell"
Sub lastprice()
Dim price As String
Do
Selection.Find.ClearFormatting
With Selection.Find.Font
.Size = 11
.Bold = True
End With
With Selection.Find
.Text = "last price ("
.Forward = True
.Wrap = wdFindStop
.Format = True
End With
Selection.Find.Execute
If Selection.Find.Found = False Then
Exit Do
Else
Selection.MoveRight Unit:=wdCell
price = Trim(Selection.Text)
If price <> "" Then ' I think there is some problem
Selection.MoveLeft Unit:=wdCell, Count:=2
Selection.Font.Name = "Cambria"
Selection.MoveDown Unit:=wdLine, Count:=1
Else
End If
End If
Loop
End Sub
For more details check
It should elaborate more by second image of table with conditions
I took the code in the debugger, which you should have done, and inspected what price was. It turns out it contains some kind of cell marker with character codes 13, 7. So an empty cell still in Word contains something. Change your if statement to the following:
If mid(price,1,1) <> Chr(13) Then
After Edited: #paul ogilvie
If mid(price,1,1) = Chr(13) Then
works for me, I change the logic <> to =
Here is working code
Sub lastprice()
Dim price As String
Do
Selection.Find.ClearFormatting
With Selection.Find.Font
.Size = 11
.Bold = True
End With
With Selection.Find
.Text = "last price ("
.Forward = True
.Wrap = wdFindStop
.Format = True
End With
Selection.Find.Execute
If Selection.Find.Found = False Then
Exit Do
Else
Selection.MoveRight Unit:=wdCell
Selection.MoveRight Unit:=wdCharacter, Count:=1, Extend:=wdExtend
price = Trim(Selection.Text)
If Mid(price, 1, 1) = Chr(13) Then
Selection.MoveLeft Unit:=wdCell, Count:=2
Selection.Font.Name = "Cambria"
Selection.MoveDown Unit:=wdLine, Count:=1
Else
Selection.MoveRight Unit:=wdCharacter, Count:=1
End If
End If
Loop
End Sub
How do I find a phrase, go to the beginning of the line the phrase is in, insert a page break, then execute the macro again.
I have attempted the following but it will not go to the following value of "Agent Name" upon subsequent execution.
Sub mFI()
' ' mFI Macro ' '
Selection.MoveRight Unit:=wdCharacter, Count:=1
With Selection
.Find
.ClearFormatting
.Forward = True
.MatchWholeWord = True
.MatchCase = False
.Execute FindText:="Agent Name"
End With
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.MoveDown Unit:=wdLine, Count:=1
Selection.InsertBreak Type:=wdPageBreak, Count:=1
End Sub
assuming code you provided is correct, This will help you.
Sub mFI()
' ' mFI Macro ' '
Selection.MoveRight Unit:=wdCharacter, Count:=1
With Selection
.Find
.ClearFormatting
.Forward = True
.MatchWholeWord = True
.MatchCase = False
.Execute FindText:="Agent Name"
End With
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.MoveDown Unit:=wdLine, Count:=1
Selection.InsertBreak Type:=wdPageBreak, Count:=1
With Selection
.Find
.ClearFormatting
.Forward = True
.MatchWholeWord = True
.MatchCase = False
.Execute FindText:="Agent Name"
End With
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.MoveDown Unit:=wdLine, Count:=1
End Sub
IF you want to insert page break to all the occurrences of agent name you could try.
Sub Demo()
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "Agent Name"
.Replacement.Text = "^m^&"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub
I have recorded a macro in Word 2007 that finds a word, moves the cursor two lines up, inserts three '***', then highlights the line. It works on the first instance of the found word. I am struggling to get it to repeat throughout the document with all instances of the word I want it to find.
This is the output from my recorded macro. I need the actions to be repeated for each instance of "B,".
Sub HighlightNewItems()
'
' HighlightNewItems Macro
'
'
Selection.Find.ClearFormatting
With Selection.Find
.Text = "B,"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = True
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.MoveUp Unit:=wdLine, Count:=2
Selection.MoveLeft Unit:=wdWord, Count:=1
Selection.TypeText Text:="***"
Selection.TypeParagraph
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
Options.DefaultHighlightColorIndex = wdRed
Selection.Range.HighlightColorIndex = wdRed
Selection.MoveRight Unit:=wdCharacter, Count:=1
End Sub
Try putting the following construct within your With.Selection.Find
Do While .Execute
'(logic that you want to apply after finding string)
Loop
In your case, your code would look like
Sub HighlightNewItems()
'
' HighlightNewItems Macro
'
'
Selection.Find.ClearFormatting
With Selection.Find
.Text = "B,"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = True
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
Do While .Execute
Selection.MoveUp Unit:=wdLine, Count:=2
Selection.MoveLeft Unit:=wdWord, Count:=1
Selection.TypeText Text:="***"
Selection.TypeParagraph
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
Options.DefaultHighlightColorIndex = wdRed
Selection.Range.HighlightColorIndex = wdRed
Selection.MoveRight Unit:=wdCharacter, Count:=1
Loop
End With
End Sub
I am working on a macro, it has 3 problems with it. Probably more depending on how experience a programmer you are. I am trying to get my code to do this 1 thing right now.
At the bottom of the code, I am trying to make the input box work so that what the user's input will be the replacement text for 'Assigned BK Specialist:'
With ActiveDocument.Content.Find
.Text = "Assigned BK Specialist:"
.Replacement.Text = InputBox("Type in BK specialist's name.", "Mark Scott")
End With
Sub Combined_Code()
'Condense Version
Dim iRet As Integer
Dim strPrompt As String
Dim strTitle As String
Dim BkSpecName As String
For i = 1 To 2
With Selection.Find
.Text = "date received"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindAsk
.Format = False
.MatchCase = False
.MatchWholeWord = False
End With
Selection.Find.Execute
Selection.Paste
Selection.MoveDown Unit:=wdLine, Count:=10
Next i
Selection.GoTo What:=wdGoToPage, Which:=wdGoToNext, Name:="3"
Selection.Find.Replacement.ClearFormatting
' Insert your name for the file.
With Selection.Find
.Text = "By:"
End With
Selection.Find.Execute
Selection.TypeText Text:="By: Robert Birch"
With Selection.Find
.Text = "Date Assigned:"
End With
' Insert current date on 3rd page.
Selection.Find.Execute
Selection.TypeText Text:="Date Assigned: "
Selection.InsertDateTime DateTimeFormat:="M/d/yyyy", InsertAsField:=False, _
DateLanguage:=wdEnglishUS, CalendarType:=wdCalendarWestern, _
InsertAsFullWidth:=False
'Prompt
strPrompt = "What chapter is the MFR for?, NO = 13, Yes = 7"
'Dialog's Title
strTitle = "What Chapter Are You Working On?"
'Display MessageBox
iRet = MsgBox(strPrompt, vbYesNoCancel, strTitle, Yes = "Option1", No = "Option2")
'Check pressed button
If iRet = vbNo Then
MsgBox "Running Ch.13!"
' Added code for ch 13
Selection.GoTo What:=wdGoToPage, Which:=wdGoToNext, Name:="1"
Selection.MoveDown Unit:=wdLine, Count:=9
Selection.MoveRight Unit:=wdCell, Count:=2
For q = 1 To 7
Selection.TypeText Text:="x"
Selection.MoveDown Unit:=wdLine, Count:=1
Next q
Selection.GoTo What:=wdGoToPage, Which:=wdGoToNext, Name:="2"
Selection.MoveDown Unit:=wdLine, Count:=10
Selection.MoveRight Unit:=wdCell, Count:=2
For w = 1 To 9
Selection.TypeText Text:="x"
Selection.MoveDown Unit:=wdLine, Count:=1
Next w
Else
MsgBox "Running Ch.7!"
' Added coded for Ch.7
Selection.GoTo What:=wdGoToPage, Which:=wdGoToNext, Name:="1"
Selection.MoveDown Unit:=wdLine, Count:=9
Selection.MoveRight Unit:=wdCell, Count:=2
For b = 1 To 7
Selection.TypeText Text:="x"
Selection.MoveDown Unit:=wdLine, Count:=1
'modded
Next b
Selection.MoveDown Unit:=wdLine, Count:=10
For j = 1 To 3
Selection.TypeText Text:="x"
Selection.MoveDown Unit:=wdLine, Count:=1
Next j
Selection.TypeText Text:="n/a"
Selection.MoveDown Unit:=wdLine, Count:=1
Selection.TypeText Text:="n/a"
For o = 1 To 4
Selection.MoveDown Unit:=wdLine, Count:=1
Selection.TypeText Text:="x"
Next o
End If
Selection.GoTo What:=wdGoToPage, Which:=wdGoToNext, Name:="3"
Selection.MoveDown Unit:=wdLine, Count:=9
Selection.MoveRight Unit:=wdCell, Count:=2
Selection.TypeText Text:="x"
Selection.MoveDown Unit:=wdLine, Count:=2
Selection.TypeText Text:="x"
'End part of program requires user input for BK spec's name
With ActiveDocument.Content.Find
.Text = "Assigned BK Specialist:"
.Replacement.Text = InputBox("Type in BK specialist's name.", "Mark Scott")
End With
Try it like this:
With ActiveDocument.Content.Find
.Text = "Assigned BK Specialist:"
.Replacement.Text = InputBox("Type in BK specialist's name.", "Mark Scott")
.Execute Replace:=wdReplaceAll, Forward:=True, Wrap:=wdFindContinue '<----- add that
End With