Inserting a few lines of text at the end of a document - vba

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

Related

Copy/paste subsequent paragraphs from two Word documents one after another (to learn a foreign language)

I have two books of the same title: one English, one Spanish. I want to combine them so I can learn Spanish. So I need a single Word document that has one paragraph in English, followed by the same paragraph in Spanish, over and over again. Below is what I have from manually copy/pasting, but I would like to automate it using a patter of (a) copy/pasting by paragraph break, or (b) copy/pasting every 350 characters (or 100 words) with a punctuation being the end point. This is what I have so far:
Sub Macro1()
Windows("3.doc - Compatibility Mode").Activate
Selection.MoveDown Unit:=wdLine, Count:=13, Extend:=wdExtend
Selection.MoveUp Unit:=wdLine, Count:=1, Extend:=wdExtend
Selection.Copy
Windows("Document2").Activate
Windows("656398.docx - Compatibility Mode").Activate
Selection.PasteAndFormat (wdFormatOriginalFormatting)
Selection.MoveDown Unit:=wdLine, Count:=23, Extend:=wdExtend
Selection.MoveUp Unit:=wdLine, Count:=7, Extend:=wdExtend
Selection.MoveDown Unit:=wdLine, Count:=3, Extend:=wdExtend
Selection.Copy
Windows("Document2").Activate
Windows("3.doc - Compatibility Mode").Activate
Selection.PasteAndFormat (wdPasteDefault)
Selection.MoveDown Unit:=wdLine, Count:=8, Extend:=wdExtend
Selection.MoveUp Unit:=wdLine, Count:=1, Extend:=wdExtend
Selection.MoveDown Unit:=wdLine, Count:=1, Extend:=wdExtend
Selection.MoveLeft Unit:=wdCharacter, Count:=2, Extend:=wdExtend
Selection.MoveRight Unit:=wdCharacter, Count:=1, Extend:=wdExtend
Selection.Copy
Windows("Document2").Activate
Windows("656398.docx - Compatibility Mode").Activate
Selection.PasteAndFormat (wdFormatOriginalFormatting)
Selection.MoveDown Unit:=wdLine, Count:=18, Extend:=wdExtend
Selection.Copy
Windows("Document2").Activate
Selection.MoveDown Unit:=wdLine, Count:=1
Selection.TypeParagraph
Selection.TypeParagraph
Selection.PasteAndFormat (wdUseDestinationStylesRecovery)
ActiveDocument.Save
End Sub
For example, provided the documents have exactly the same paragraphing:
Sub AddSecondLanguage()
Application.ScreenUpdating = False
Dim DocA As Document, DocB As Document, Rng As Range, i As Long
With Application.FileDialog(FileDialogType:=msoFileDialogFilePicker)
.Title = "Select the source document containing the primary language."
.InitialFileName = "C:\Users\" & Environ("Username") & "\Documents\"
.AllowMultiSelect = False
If .Show = -1 Then
Set DocA = Documents.Open(.SelectedItems(1), ReadOnly:=True, AddToRecentFiles:=False)
Else
MsgBox "No primary language file selected. Exiting.", vbExclamation: Exit Sub
End If
End With
With Application.FileDialog(FileDialogType:=msoFileDialogFilePicker)
.Title = "Select the source document containing the secondary language."
.InitialFileName = DocA.Path & "\"
.AllowMultiSelect = False
If .Show = -1 Then
Set DocB = Documents.Open(.SelectedItems(1), ReadOnly:=False, AddToRecentFiles:=True)
Else
MsgBox "No secondary language file selected. Exiting.", vbExclamation
DocA.Close SaveChanges:=False: Set DocA = Nothing: Exit Sub
End If
End With
With DocB
For i = .Paragraphs.Count To 1 Step -1
Set Rng = .Paragraphs(i).Range
Rng.Collapse wdCollapseStart
Rng.FormattedText = DocA.Paragraphs(i).Range.FormattedText
Next
.SaveAs2 FileName:=Split(DocA.FullName, ".doc")(0) & "-Combined.docx", FileFormat:=wdFormatXMLDocument, AddToRecentFiles:=False
End With
DocA.Close SaveChanges:=False
Set DocA = Nothing: Set DocB = Nothing
Application.ScreenUpdating = True
End Sub
The combined document will be saved in the docx format with the same name as the first document you open, with '-Combined' added to the filename.

Blank selected text in VBA Word is coming up as 2 characters

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

Pasting website links into a word document in a very specific format

I recorded a macro for a link that contains "https://www." at the beginning of it, and it works great.
I want to create one macro, regardless of whether it begins with
http://www.
https://www.
https://
http://
which pastes the link in this specific format. I am coming from Python and am unfamiliar with VBA. In python, I would do something like this:
if "http://www." in mylink:
replace("http://www.", "")
for each of the aforementioned bullets.
How would I do something similar in VBA?
Here's my current macro that removes "https://www." and formats the link as desired (underlined, size 10, in parentheses)
`Sub piclink()
'
' piclink Macro
'
'
Application.Run MacroName:="Normal.NewMacros.PS"
Selection.HomeKey Unit:=wdLine
Selection.MoveRight Unit:=wdCharacter, Count:=12, Extend:=wdExtend
Selection.TypeBackspace
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
If Selection.Font.Underline = wdUnderlineNone Then
Selection.Font.Underline = wdUnderlineSingle
Else
Selection.Font.Underline = wdUnderlineNone
End If
Selection.HomeKey Unit:=wdLine
Selection.TypeText Text:="("
Selection.HomeKey Unit:=wdLine
Selection.EndKey Unit:=wdLine
Selection.TypeText Text:=")"
Selection.HomeKey Unit:=wdLine, Extend:=wdExtend
Selection.Font.Shrink
Selection.Font.Shrink
Selection.Font.Shrink
End Sub`
I will note that "Application.Run MacroName:="Normal.NewMacros.PS"" is a simple copy and paste function.
Try:
Sub Demo()
With Selection.Range
.Paste
If InStr(.Text, "www.") > 0 Then
.Text = "(" & Split(.Text, "www.")(1) & ")"
ElseIf InStr(.Text, "://") > 0 Then
.Text = "(" & Split(.Text, "://")(1) & ")"
End If
.Font.Size = 10
.Start = .Start + 1
.End = .End - 1
.Font.Underline = True
End With
End Sub

Find a line which starts with a term and delete it

I am trying to automatically truncate a long conversation between two people which has been pasted into word.
Input
Jim , 18th November 05:23
Hi!
Sarah , 19th November 06:03
Hello there
I want to format all text from Jim blue, and all text from Sarah red. In order to do this I think I need to follow this process:
Colour every bit of text red
Find a line starting with "Jim ," and format everything after it blue until it reaches a line which starts with "Sarah ,".
Hope this helps
Public Sub ColorMeRedBlue()
Selection.HomeKey Unit:=wdStory
color_the_text = False
For Each line_in_para In ActiveDocument.Paragraphs
text_in_line = line_in_para.Range.Text
check_name = Split(text_in_line, " ")
If InStr("Jim", check_name(0)) Then
color_the_text = False
ElseIf InStr("Sarah", check_name(0)) Then
color_the_text = True
End If
If color_the_text = False Then
Selection.HomeKey Unit:=wdLine
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
Selection.Font.Color = wdColorRed
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
Selection.EndKey Unit:=wdLine
Selection.MoveDown Unit:=wdLine, Count:=1
ElseIf color_the_text = True Then
Selection.HomeKey Unit:=wdLine
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
Selection.Font.Color = wdColorBlue
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
Selection.EndKey Unit:=wdLine
Selection.MoveDown Unit:=wdLine, Count:=1
End If
Next line_in_para
End Sub

User Input Should be Replacement Text

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