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
Related
When I run the my VBA code on the following text, it works fine till it gets to the paragraph break. I try to run a test for paragraph breaks but it comes up as FALSE:
OK, so that now we're recording.
Uhm, so I spoke with Berry,
which actually your own berries
team now, right?
The output looks like this:
OK, so that now we're recording. Uhm, so I spoke with Berry, which actually your own berries team now, right
Here is my original code:
Sub OneLine()
Dim charCount As Integer
'Go to End of document and add "
Selection.EndKey Unit:=wdStory
Selection.TypeText Text:=" """
'Go to beginning of Document
Selection.GoTo what:=wdGoToSection, Which:=wdGoToFirst
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
'Count how many characters are in Selection
charCount = Len(Selection)
Set myRange = Selection
myRange.Find.Execute FindText:="""", Forward:=True
'MsgBox (myRange.Find.found)
Do While myRange.Find.found = False
If charCount > 1 Then
Selection.MoveDown Unit:=wdLine, Count:=1, Extend:=wdMove
Selection.HomeKey Unit:=wdLine, Extend:=wdMove
Selection.TypeBackspace
Selection.TypeText Text:=" "
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
charCount = Len(Selection)
Else
Selection.Delete
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
charCount = Len(Selection)
End If
Set myRange = Selection
myRange.Find.Execute FindText:="""", Forward:=True
'MsgBox (myRange.Find.found)
Loop
Selection.TypeBackspace
Selection.TypeBackspace
Selection.TypeBackspace
Selection.GoTo what:=wdGoToSection, Which:=wdGoToFirst
End Sub
I tried using Find.Text:= "^p" statement with an IF THEN but it came out as FALSE.
Thank you in advanced!
Figured it out.
i used the following lines:
With Selection.Find
.Execute FindText:=vbCrLf
.Forward = True
End With
Originally I was only looking for one thing, paragraph break or line break but not the combo of line feed and carriage return: vbCRLf. I was also using "vbCrLf" which is wrong because at that point it's looking for that string.
This is my final code:
Sub OneLine()
Dim charCount As Integer
'Go to End of document and add "
Selection.EndKey Unit:=wdStory
Selection.TypeText Text:=" """
'Go to beginning of Document
Selection.GoTo what:=wdGoToSection, Which:=wdGoToFirst
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
'Count how many characters are in Selection
charCount = Len(Selection)
Set myRange = Selection
myRange.Find.Execute FindText:=""""
'MsgBox (myRange.Find.found)
Do While myRange.Find.found = False
If charCount > 1 Then
With Selection.Find
.Execute FindText:=vbCrLf
.Forward = True
End With
If ((Selection.Find.found = True) And (charCount = 2)) Then
Selection.HomeKey Unit:=wdLine, Extend:=wdMove
Selection.TypeBackspace
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
charCount = Len(Selection)
Else
Selection.MoveDown Unit:=wdLine, Count:=1, Extend:=wdMove
Selection.HomeKey Unit:=wdLine, Extend:=wdMove
Selection.TypeBackspace
Selection.TypeText Text:=" "
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
charCount = Len(Selection)
End If
Else
Selection.Delete
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
charCount = Len(Selection)
End If
Set myRange = Selection
myRange.Find.Execute FindText:=""""
'MsgBox (myRange.Find.found)
Loop
Selection.TypeBackspace
Selection.TypeBackspace
Selection.TypeBackspace
Selection.GoTo what:=wdGoToSection, Which:=wdGoToFirst
End Sub
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 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
My title didn't seem to match questions that may have my answer, and I did find some snippets from other threads/sites to help me get this far. I'm looking for assistance tying the entire macro together. Here is what I have so far:
Sub Test()
Selection.EndKey Unit:=wdStory
Dim oPara1 As Word.Paragraph
Set oDoc = oWord.Documents.Add
Set oPara1 = oDoc.Content.Paragraphs.Add
With oPara1.Range
.ParagraphFormat.Alignment = wdAlignParagraphCenter
.InsertParagraphAfter
With .Font
.Name = "Times New Roman"
.Size = "12"
.Bold = True
End With
End With
Selection.TypeText Text:="Fosters, Inc."
Selection.MoveDown Unit:=wdLine, Count:=1, Extend:=wdExtend
Selection.TypeText Text:="www.genericwebsite.com"
Selection.MoveDown Unit:=wdLine, Count:=1, Extend:=wdExtend
Selection.MoveDown Unit:=wdLine, Count:=1, Extend:=wdExtend
'this needs to be left alignment from here on out
Selection.TypeText Text:="Block\Paragraph Format:"
Selection.MoveDown Unit:=wdLine, Count:=1, Extend:=wdExtend
Selection.TypeText Text:="Run Date:"
Selection.MoveDown Unit:=wdLine, Count:=1, Extend:=wdExtend
Selection.TypeText Text:="Picture:"
Selection.MoveDown Unit:=wdLine, Count:=1, Extend:=wdExtend
Selection.TypeText Text:="Symbol:"
Selection.MoveDown Unit:=wdLine, Count:=1, Extend:=wdExtend
Selection.TypeText Text:="Guest Book:"
End Sub
I want it to move to the end of the document and print:
Fosters, Inc.
www.genericwebsite.com
Block\Paragraph Format:
Run Date:
Picture:
Symbol:
Guest Book:
Thanks for any help - I've spent literally just an hour or so with vba in Word today.
Option Explicit
Sub Test()
Selection.EndKey Unit:=wdStory
Dim oPara1 As Word.Paragraph
Dim oDoc As Word.Document
Set oDoc = ActiveDocument
Set oPara1 = oDoc.Content.Paragraphs.Add
With oPara1.Range
.ParagraphFormat.Alignment = wdAlignParagraphCenter
.InsertParagraphAfter
With .Font
.Name = "Times New Roman"
.Size = "12"
.Bold = True
End With
End With
Selection.TypeText Text:=vbCr
Selection.TypeText Text:="Fosters, Inc." & vbCr
Selection.TypeText Text:="www.genericwebsite.com" & vbCr
oPara1.Range.ParagraphFormat.Alignment = wdAlignParagraphLeft
Selection.TypeText Text:="Block\Paragraph Format:" & vbCr
Selection.TypeText Text:="Run Date:" & vbCr
Selection.TypeText Text:="Picture:" & vbCr
Selection.TypeText Text:="Symbol:" & vbCr
Selection.TypeText Text:="Guest Book:"
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