Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 11-12-2015, 08:16 AM
ChrisJ83 ChrisJ83 is offline User input box with IF statement Windows 7 64bit User input box with IF statement Office 2013
Novice
User input box with IF statement
 
Join Date: Nov 2015
Posts: 9
ChrisJ83 is on a distinguished road
Default User input box with IF statement

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
Reply With Quote
  #2  
Old 11-12-2015, 02:32 PM
macropod's Avatar
macropod macropod is offline User input box with IF statement Windows 7 64bit User input box with IF statement Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Try:
Code:
Sub PromptText()
Dim TopLineText As String
TopLineText = InputBox("Top Line Text?", "")
If Trim(TopLineText) <> "" Then Selection.InsertAfter TopLineText & vbCr
End Sub
PS: When posting code, please use the code tags, indicated by the # button on the posting menu.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 11-13-2015, 01:11 AM
ChrisJ83 ChrisJ83 is offline User input box with IF statement Windows 7 64bit User input box with IF statement Office 2013
Novice
User input box with IF statement
 
Join Date: Nov 2015
Posts: 9
ChrisJ83 is on a distinguished road
Default

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.
Reply With Quote
  #4  
Old 11-13-2015, 04:38 AM
ChrisJ83 ChrisJ83 is offline User input box with IF statement Windows 7 64bit User input box with IF statement Office 2013
Novice
User input box with IF statement
 
Join Date: Nov 2015
Posts: 9
ChrisJ83 is on a distinguished road
Default

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
Reply With Quote
  #5  
Old 11-13-2015, 05:52 AM
ChrisJ83 ChrisJ83 is offline User input box with IF statement Windows 7 64bit User input box with IF statement Office 2013
Novice
User input box with IF statement
 
Join Date: Nov 2015
Posts: 9
ChrisJ83 is on a distinguished road
Default

Managed to sort this now, thanks again!
Reply With Quote
  #6  
Old 11-13-2015, 03:04 PM
macropod's Avatar
macropod macropod is offline User input box with IF statement Windows 7 64bit User input box with IF statement Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #7  
Old 11-13-2015, 03:15 PM
ChrisJ83 ChrisJ83 is offline User input box with IF statement Windows 7 64bit User input box with IF statement Office 2013
Novice
User input box with IF statement
 
Join Date: Nov 2015
Posts: 9
ChrisJ83 is on a distinguished road
Default

Quote:
Originally Posted by macropod View Post
Why are you doing any of this via a macro, instead of using a mailmerge main document or template designed for the purpose?
To be honest I thought this was the only way

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
Reply With Quote
  #8  
Old 11-13-2015, 04:01 PM
macropod's Avatar
macropod macropod is offline User input box with IF statement Windows 7 64bit User input box with IF statement Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Quote:
Originally Posted by ChrisJ83 View Post
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 what are the mergefields for?
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #9  
Old 11-13-2015, 04:10 PM
ChrisJ83 ChrisJ83 is offline User input box with IF statement Windows 7 64bit User input box with IF statement Office 2013
Novice
User input box with IF statement
 
Join Date: Nov 2015
Posts: 9
ChrisJ83 is on a distinguished road
Default

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
Reply With Quote
  #10  
Old 11-13-2015, 06:20 PM
macropod's Avatar
macropod macropod is offline User input box with IF statement Windows 7 64bit User input box with IF statement Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
Reply



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
User input box with IF statement vba: user input named argument andrew12345 Excel Programming 2 11-18-2014 08:18 AM
User input box with IF statement 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

Other Forums: Access Forums

All times are GMT -7. The time now is 10:08 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft