![]() |
|
#1
|
|||
|
|||
|
Hi
I was wondering if anyone could help/point me in the right direction, I have the following macro Sub PromptText() ' ' PromptText Macro ' ' Dim TopLineText As String TopLineText = InputBox("Top Line Text?", "") Selection.TypeText Text:=TopLineText Selection.TypeParagraph End Sub Which works fine however I would like it to only add the TypeParagraph if any text is input. If the prompt is left blank I would like it to effectively do nothing (stay on the same line) as I will add additional commands to the macro once this is sorted. Any advice appreciated. Cheers Chris |
|
#2
|
||||
|
||||
|
Try:
Code:
Sub PromptText()
Dim TopLineText As String
TopLineText = InputBox("Top Line Text?", "")
If Trim(TopLineText) <> "" Then Selection.InsertAfter TopLineText & vbCr
End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
#3
|
|||
|
|||
|
Hi Paul
Thankyou! Also thanks for the tip, I thought there might be a code section but musn't have looked hard enough. Thanks again and I hope you have a great weekend, really appreciated. |
|
#4
|
|||
|
|||
|
Sorry to post again, do you know how I would incorporate this into my existing code?
I have tried the following and it prompts me to enter the text however it doesn't actually put the text in the document. It completes the rest of the macro completely though. Code:
Selection.HomeKey Unit:=wdStory
With ActiveDocument.PageSetup
.TopMargin = CentimetersToPoints(5)
.BottomMargin = CentimetersToPoints(3)
.LeftMargin = CentimetersToPoints(2.31)
.RightMargin = CentimetersToPoints(2.31)
.HeaderDistance = CentimetersToPoints(2.54)
.FooterDistance = CentimetersToPoints(0)
ActiveDocument.Tables(1).Delete
End With
Selection.Style = ActiveDocument.Styles("Normal")
Dim TopLineText As String
Dim Reference As String
TopLineText = InputBox("Top Line Text?", "")
Reference = InputBox("Reference?", "")
Selection.Font.Name = "Arial"
Selection.Font.Size = 10
If Trim(TopLineText) <> "" Then Selection.InsertAfter TopLineText & vbCr
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
"MERGEFIELD LQCASE_NAME", PreserveFormatting:=True
ActiveWindow.View.ShowFieldCodes = Not ActiveWindow.View.ShowFieldCodes
Selection.TypeParagraph
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
"MERGEFIELD ADD", PreserveFormatting:=True
ActiveWindow.View.ShowFieldCodes = Not ActiveWindow.View.ShowFieldCodes
Selection.TypeParagraph
Selection.TypeParagraph
Selection.TypeParagraph
Selection.TypeParagraph
Selection.TypeParagraph
Selection.TypeText Text:="Our Ref: "
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
"MERGEFIELD LQCASE_MAN_SEN", PreserveFormatting:=True
ActiveWindow.View.ShowFieldCodes = Not ActiveWindow.View.ShowFieldCodes
Selection.TypeText Text:="/"
Selection.TypeText Text:=Reference
Selection.TypeText Text:="/"
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
"MERGEFIELD LQCASE_CASECODE", PreserveFormatting:=True
ActiveWindow.View.ShowFieldCodes = Not ActiveWindow.View.ShowFieldCodes
Selection.TypeParagraph
Selection.TypeParagraph
Selection.TypeText Text:="Your Ref: "
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
"MERGEFIELD CREF", PreserveFormatting:=True
ActiveWindow.View.ShowFieldCodes = Not ActiveWindow.View.ShowFieldCodes
Selection.TypeParagraph
Selection.TypeParagraph
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
"DATE \@ ""dd MMMM yyyy"" ", PreserveFormatting:=True
Selection.TypeParagraph
Selection.TypeParagraph
Selection.TypeParagraph
Selection.HomeKey Unit:=wdStory
Selection.Style = ActiveDocument.Styles("Normal")
ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=1, NumColumns:= _
1, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:= _
wdAutoFitFixed
With Selection.Tables(1)
If .Style <> "Table Grid" Then
.Style = "Table Grid"
End If
.ApplyStyleHeadingRows = True
.ApplyStyleLastRow = False
.ApplyStyleFirstColumn = True
.ApplyStyleLastColumn = False
.ApplyStyleRowBands = True
.ApplyStyleColumnBands = False
End With
With Selection.Tables(1).Rows
.WrapAroundText = True
.HorizontalPosition = CentimetersToPoints(14.78)
.RelativeHorizontalPosition = wdRelativeHorizontalPositionPage
.DistanceLeft = CentimetersToPoints(0.32)
.DistanceRight = CentimetersToPoints(0.32)
.VerticalPosition = CentimetersToPoints(-0.85)
.RelativeVerticalPosition = wdRelativeVerticalPositionParagraph
.DistanceTop = CentimetersToPoints(0)
.DistanceBottom = CentimetersToPoints(0)
.AllowOverlap = True
End With
With Selection.Tables(1)
.TopPadding = CentimetersToPoints(0)
.BottomPadding = CentimetersToPoints(0)
.LeftPadding = CentimetersToPoints(0.19)
.RightPadding = CentimetersToPoints(0.19)
.Spacing = 0
.AllowPageBreaks = True
.AllowAutoFit = True
End With
Selection.Tables(1).Rows.HeightRule = wdRowHeightAtLeast
Selection.Tables(1).Rows.Height = CentimetersToPoints(2.55)
Selection.Tables(1).Columns(1).PreferredWidthType = wdPreferredWidthPoints
Selection.Tables(1).Columns(1).PreferredWidth = CentimetersToPoints(5.13)
Selection.Range.Cells(1).PreferredWidthType = wdPreferredWidthPoints
Selection.Range.Cells(1).PreferredWidth = CentimetersToPoints(5.13)
Selection.Borders(wdBorderTop).LineStyle = wdLineStyleNone
Selection.Borders(wdBorderLeft).LineStyle = wdLineStyleNone
Selection.Borders(wdBorderBottom).LineStyle = wdLineStyleNone
Selection.Borders(wdBorderRight).LineStyle = wdLineStyleNone
Selection.Borders(wdBorderDiagonalDown).LineStyle = wdLineStyleNone
Selection.Borders(wdBorderDiagonalUp).LineStyle = wdLineStyleNone
Selection.Font.Name = "Arial"
Selection.Font.Size = 10
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
"MERGEFIELD GENERAL_ADD", PreserveFormatting:=True
ActiveWindow.View.ShowFieldCodes = Not ActiveWindow.View.ShowFieldCodes
Selection.TypeParagraph
Selection.TypeParagraph
Selection.Font.Bold = wdToggle
Selection.TypeText Text:="T: "
Selection.Font.Bold = wdToggle
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
"MERGEFIELD GENERAL_TEL1", PreserveFormatting:=True
ActiveWindow.View.ShowFieldCodes = Not ActiveWindow.View.ShowFieldCodes
Selection.TypeParagraph
Selection.Font.Bold = wdToggle
Selection.TypeText Text:="F: "
Selection.Font.Bold = wdToggle
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
"MERGEFIELD GENERAL_TEL2", PreserveFormatting:=True
ActiveWindow.View.ShowFieldCodes = Not ActiveWindow.View.ShowFieldCodes
End Sub
|
|
#5
|
|||
|
|||
|
Managed to sort this now, thanks again!
|
|
#6
|
||||
|
||||
|
Why are you doing any of this via a macro, instead of using a mailmerge main document or template designed for the purpose?
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
#7
|
|||
|
|||
|
Quote:
We receive documents every month and then we have to remove the the generic layout at the top (in a table), adjust the margins and then add our letterhead format. So instead of doing it manually to be honest I thought this was the only way. Can this be done in a simpler method? Thanks again for your input Also the first line is different for some documents if that's makes a difference |
|
#8
|
||||
|
||||
|
So what are the mergefields for?
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
#9
|
|||
|
|||
|
The merge field are specific to our software so they automatically bring through the correct information. The documents are generic some have to add the merge fields
|
|
#10
|
||||
|
||||
|
In that case, try:
Code:
Sub Demo()
Application.ScreenUpdating = False
Dim strTTxt As String, strRef As String, Rng As Range, Fld As Field, Tbl As Table
strTTxt = Trim(InputBox("Top Line Text?", ""))
strRef = Trim(InputBox("Reference?", ""))
With ActiveDocument
.ActiveWindow.View.ShowFieldCodes = False
With .PageSetup
.TopMargin = CentimetersToPoints(5)
.BottomMargin = CentimetersToPoints(3)
.LeftMargin = CentimetersToPoints(2.31)
.RightMargin = CentimetersToPoints(2.31)
.HeaderDistance = CentimetersToPoints(2.54)
.FooterDistance = CentimetersToPoints(0)
End With
With .Styles(wdStyleNormal)
With .Font
.Name = "Arial"
.Size = 10
End With
With .ParagraphFormat
.SpaceAfter = 0
.SpaceBefore = 0
.LineSpacingRule = wdLineSpaceSingle
End With
End With
If .Tables.Count > 0 Then .Tables(1).Delete
Set Rng = .Range
With Rng
.Style = wdStyleNormal
.Collapse wdCollapseStart
Set Tbl = .Tables.Add(Range:=Rng, NumRows:=1, NumColumns:=1, _
DefaultTableBehavior:=wdWord9TableBehavior)
With Tbl
.Borders.Enable = False
.TopPadding = CentimetersToPoints(0)
.BottomPadding = CentimetersToPoints(0)
.LeftPadding = CentimetersToPoints(0.19)
.RightPadding = CentimetersToPoints(0.19)
.Spacing = 0
.AllowPageBreaks = True
.PreferredWidthType = wdPreferredWidthPoints
.PreferredWidth = CentimetersToPoints(5.13)
With .Rows
.HeightRule = wdRowHeightAtLeast
.Height = CentimetersToPoints(2.55)
.WrapAroundText = True
.HorizontalPosition = CentimetersToPoints(14.78)
.RelativeHorizontalPosition = wdRelativeHorizontalPositionPage
.DistanceLeft = CentimetersToPoints(0.32)
.DistanceRight = CentimetersToPoints(0.32)
.VerticalPosition = CentimetersToPoints(-0.85)
.RelativeVerticalPosition = wdRelativeVerticalPositionParagraph
.DistanceTop = CentimetersToPoints(0)
.DistanceBottom = CentimetersToPoints(0)
.AllowOverlap = True
End With
End With
.End = Tbl.Range.End
.Collapse wdCollapseEnd
If strTTxt <> "" Then .InsertAfter strTTxt & vbCr
.Collapse wdCollapseEnd
Set Fld = .Fields.Add(Range:=Rng, Type:=wdFieldEmpty, _
Text:="MERGEFIELD LQCASE_NAME", PreserveFormatting:=False)
.End = Fld.Result.End + 1
.InsertAfter vbCr
.Collapse wdCollapseEnd
Set Fld = .Fields.Add(Range:=Rng, Type:=wdFieldEmpty, _
Text:="MERGEFIELD ADD", PreserveFormatting:=False)
.End = Fld.Result.End + 1
.InsertAfter vbCr & vbCr & vbCr & vbCr & vbCr & "Our Ref: "
.Collapse wdCollapseEnd
Set Fld = .Fields.Add(Range:=Rng, Type:=wdFieldEmpty, _
Text:="MERGEFIELD LQCASE_MAN_SEN", PreserveFormatting:=False)
.End = Fld.Result.End + 1
.InsertAfter "/" & strRef & "/"
.Collapse wdCollapseEnd
Set Fld = .Fields.Add(Range:=Rng, Type:=wdFieldEmpty, _
Text:="MERGEFIELD LQCASE_CASECODE", PreserveFormatting:=False)
.End = Fld.Result.End + 1
.InsertAfter vbCr & vbCr & vbCr & "Your Ref: "
.Collapse wdCollapseEnd
Set Fld = .Fields.Add(Range:=Rng, Type:=wdFieldEmpty, _
Text:="MERGEFIELD CREF", PreserveFormatting:=False)
.End = Fld.Result.End + 1
.InsertAfter vbCr & vbCr
.Collapse wdCollapseEnd
Set Fld = .Fields.Add(Range:=Rng, Type:=wdFieldEmpty, _
Text:="DATE \@ ""dd MMMM yyyy""", PreserveFormatting:=False)
.End = Fld.Result.End + 1
.InsertAfter vbCr & vbCr & vbCr
.Collapse wdCollapseEnd
End With
Set Rng = Tbl.Cell(1, 1).Range
With Rng
.End = .End - 1
Set Fld = .Fields.Add(Range:=Rng, Type:=wdFieldEmpty, _
Text:="MERGEFIELD GENERAL_ADD", PreserveFormatting:=False)
.End = Fld.Result.End + 1
.InsertAfter vbCr & vbCr
.Collapse wdCollapseEnd
.Text = "T:"
.Style = "Strong"
.Collapse wdCollapseEnd
.Text = " "
.Font.Reset
.Collapse wdCollapseEnd
Set Fld = .Fields.Add(Range:=Rng, Type:=wdFieldEmpty, _
Text:="MERGEFIELD GENERAL_TEL1", PreserveFormatting:=False)
.End = Fld.Result.End + 1
.InsertAfter vbCr
.Collapse wdCollapseEnd
.Text = "F:"
.Style = "Strong"
.Collapse wdCollapseEnd
.Text = " "
.Font.Reset
.Collapse wdCollapseEnd
Set Fld = .Fields.Add(Range:=Rng, Type:=wdFieldEmpty, _
Text:="MERGEFIELD GENERAL_TEL2", PreserveFormatting:=False)
End With
End With
Set Fld = Nothing: Set Tbl = Nothing: Set Rng = Nothing
Application.ScreenUpdating = True
End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Variable arrays from user input | SeattleITguy | Excel Programming | 1 | 01-29-2015 09:19 AM |
| Taking input from InputBox from user | SeattleITguy | Excel Programming | 1 | 01-28-2015 09:05 AM |
vba: user input named argument
|
andrew12345 | Excel Programming | 2 | 11-18-2014 08:18 AM |
User input to a variable on the document
|
dsm1995gst | Word VBA | 1 | 09-03-2013 03:43 PM |
| Look up an array based on user input | johnsmb | Excel | 2 | 01-07-2011 01:12 PM |