Automatically Enter Answer on InputBox - vba

Hi How would I run a macro on excel that automatically inserts and "enters" the answer on the inputbox on AutoCad without having to manually do it myself. How would I have to modify my code below:
Sub DWG ()
Pump: ans = InputBox("1 = STD Piping" & vbCrLf & _
"2 = Omit Pump" & vbCrLf & _
"3 = SBPP", "Pump Piping")
Select Case ans
Case "1":
: Set layerObj = ThisDrawing.Layers.Add("PUMP_PIPING_" & Size)
layerObj.LayerOn = True
Case "2":
: Set layerObj = ThisDrawing.Layers.Add("OMIT_PUMP_" & Size)
layerObj.LayerOn = True
Case "3":
: Set layerObj = ThisDrawing.Layers.Add("STBP_" & Size)
layerObj.LayerOn = True
Case Else: MsgBox "Wrong Input Dude.", vbCritical, MSG: Exit Sub
End Select

You cannot "enter" a Inputbox automatically in an easy way - and even if it would be possible I think you shouldn't - your code will get a nightmare.
You could pass the "made" choice as an optional parameter to your routine and show the InputBox only if the parameter was omitted:
Sub DWG(Optional answer As String = "")
If answer = "" Then
answer = InputBox("1 = STD Piping" & vbCrLf & _
"2 = Omit Pump" & vbCrLf & _
"3 = SBPP", "Pump Piping")
End If
select case answer
....
end sub
However that will not save you from going thru all your code.

Related

Content Control not recognizing content

I was hoping someone could help me work out why the the 'F' value in my code below continues to include my error label in the ErrorMessage String when the Count value is 5?
In the document, the content control contains text just like all the other controls (which work perfectly) but this content Control text value is not being recognised in the VBA code to map error labels.
Have tried just replacing the control and checking the properties match. Debug messages suggest the the value is just being set to the default Content Control Value of "Click or Tap here to input text".
Private Sub Create_Click()
Dim oCC As ContentControl
Dim oCC2 As ContentControl
Dim Mandatory(9) As String
Dim ErrorMessage As String
Dim ErrorCount As Integer
Dim ErrorLabel(9) As String
Dim objDoc As Document
Dim strFilename As String
Dim strFileString As String
Dim Number As String
Mandatory(0) = "A"
Mandatory(1) = "B"
Mandatory(2) = "C"
Mandatory(3) = "D"
Mandatory(4) = "E"
Mandatory(5) = "F"
Mandatory(6) = "G"
Mandatory(7) = "H"
Mandatory(8) = "I"
ErrorLabel(0) = "A Label"
ErrorLabel(1) = "B Label"
ErrorLabel(2) = "C Label"
ErrorLabel(3) = "D Label"
ErrorLabel(4) = "E Label"
ErrorLabel(5) = "F Label"
ErrorLabel(6) = "G Label"
ErrorLabel(7) = "H Label"
ErrorLabel(8) = "I Label"
ErrorMessage = ""
ErrorMessage = "The following mandatory fields are missing: "
For Count = 0 To 8
Set oCC = ActiveDocument.SelectContentControlsByTitle(Mandatory(Count)).Item(1)
MsgBox (oCC.Range.Text)
If Count = 0 Then
Number = ActiveDocument.SelectContentControlsByTitle(Mandatory(Count)).Item(1).Range.Text
End If
If oCC.Range.Text = "Click or tap here to enter text." Or oCC.Range.Text = "0.00" Then
ErrorMessage = ErrorMessage & vbCrLf & vbCrLf & "- " & ErrorLabel(Count)
MsgBox (oCC.Range.Text)
ErrorCount = ErrorCount + 1
End If
Next Count
If ErrorCount > 0 Then
MsgBox (ErrorMessage)
Else
strFileString = Number
MsgBox (strFileString)
strFilename = "Some Text Here" & " - " & strFileString & ".pdf"
With ActiveDocument
NewPath = .Path & "\" & strFilename
.SaveAs2 FileName:=NewPath, FileFormat:=wdFormatPDF
.ExportAsFixedFormat OutputFileName:=strFilename, ExportFormat:=wdExportFormatPDF, _
OpenAfterExport:=True, OptimizeFor:=wdExportOptimizeForPrint, _
Range:=wdExportAllDocument, Item:=wdExportDocumentContent
End With
End If
End Sub
Check there are no other content controls with the same title in the document.
I couldn't test your code for lack of data but from your description I guess that the ErrorMessage must be reset with each loop since it will be changed when used and would naturally retain the modified version thereafter.
Except for what follows the loop, I looked closely at your code in order to understand it. Perhaps, the changes I made will be of some use to you.
Option Explicit
Private Sub Create_Click()
Dim Doc As Document
Dim Mandatory() As String
Dim ErrorMessage As String
Dim ErrorCount As Integer
Dim strFilename As String
Dim strFileString As String ' this appears identical with 'Number'
Dim Number As String
Dim Count As Integer ' loop counter
Set Doc = ActiveDocument
Mandatory = Split("A B C D E F G H I")
Number = Doc.SelectContentControlsByTitle(Mandatory(0))(1).Range.Text
For Count = 1 To UBound(Mandatory) + 1
ErrorMessage = "The following mandatory fields are missing: "
With Doc.SelectContentControlsByTitle(Mandatory(Count))(1).Range
MsgBox "Number = " & Number & vbCr & .Text
If .Text = "Click or tap here to enter text." Or _
.Text = "0.00" Then
ErrorMessage = ErrorMessage & vbCrLf & vbCrLf & "- " & Mandatory(Count) & " Label"
MsgBox (.Text)
ErrorCount = ErrorCount + 1
End If
End With
If Count = 1 Then Exit For
Next Count
If ErrorCount > 0 Then
MsgBox (ErrorMessage)
Else
strFileString = Number
MsgBox (strFileString)
strFilename = "Some Text Here" & " - " & strFileString & ".pdf"
With Doc
NewPath = .Path & "\" & strFilename
.SaveAs2 FileName:=NewPath, FileFormat:=wdFormatPDF
.ExportAsFixedFormat OutputFileName:=strFilename, _
ExportFormat:=wdExportFormatPDF, _
OpenAfterExport:=True, _
OptimizeFor:=wdExportOptimizeForPrint, _
Range:=wdExportAllDocument, _
Item:=wdExportDocumentContent
End With
End If
End Sub
You can have VBA add the useful (some would say necessary) Option Explicit to all new code modules automatically. Select Tools > Options in the VBE window and check "Require Variable Declaration" on the Editor tab.

Erase Previously selected data

I am currently working on a drawing generating code for AutoCAD, in order for the drawing to be generated certain options have to be chosen , and when a wrong option is chosen the person can be able to click on back to go and rectify the option. Here is my code below, the problem is that the code does not erase the previously selected data when "back" is clicked, for example, if I choose "SBPP" the click enter and it takes me to "Louvres" option, then I want to go back and change from "SBPP" to "STD Piping", the previously selected "SBPP" must be erased :
Sub DWG ()
Pump: ans = InputBox("1 = STD Piping" & vbCrLf & _
"2 = Omit Pump" & vbCrLf & _
"3 = SBPP", "Pump Piping")
Select Case ans
Case "1":
: Set layerObj = ThisDrawing.Layers.Add("PUMP_PIPING_" & Size)
layerObj.LayerOn = True
Case "2":
: Set layerObj = ThisDrawing.Layers.Add("OMIT_PUMP_" & Size)
layerObj.LayerOn = True
Case "3":
: Set layerObj = ThisDrawing.Layers.Add("STBP_" & Size)
layerObj.LayerOn = True
Case Else: MsgBox "Wrong Input Dude.", vbCritical, MSG: Exit Sub
End Select
'______________________________________________________________________________
'Option for Louvres
Louver: ans = InputBox("1 = STD Louvers" & vbCrLf & _
"2 = IND louvers" & vbCrLf & _
"3 = Back ", "Pump Piping")
Select Case ans
Case "1":
: Set layerObj = ThisDrawing.Layers.Add("LOUVRES_STD")
layerObj.LayerOn = True
Case "2":
: Set layerObj = ThisDrawing.Layers.Add("LOUVRES_INDUS")
layerObj.LayerOn = True
Case "3":
If ans = 3 Then
GoTo Pump
End If
Case Else: MsgBox "Wrong Input Dude.", vbCritical, MSG: Exit Sub
End Select
End Sub
How can I modify this code so that when "back" is clicked it erases previously selected data?
This is a flow logic problem. You must use cycles to accomplish your target. "Undoing" your previous options was not defined by you, I'm guessing:
Sub DWG()
Dim ans1 As String, ans2 As String, err_msg As String
Do 'This cycle will begin and repeat when [Back] is choosen
Select Case ans1 'Undo previous operation
Case 1
ThisDrawing.Layers("PUMP_PIPING_" & size).Delete
Case "2"
ThisDrawing.Layers.Add("OMIT_PUMP_" & size).Delete
Case "3"
ThisDrawing.Layers.Add("STBP_" & size).Delete
End Select
err_msg = ""
Do 'This cycle will repeat until first answer is accepted
ans1 = InputBox(err_msg & _
"1 = STD Piping" & vbCrLf & _
"2 = Omit Pump" & vbCrLf & _
"3 = SBPP", "Pump Piping")
Select Case ans1
Case "1"
Set layerObj = ThisDrawing.Layers.Add("PUMP_PIPING_" & size)
layerObj.LayerOn = True
Case "2"
Set layerObj = ThisDrawing.Layers.Add("OMIT_PUMP_" & size)
layerObj.LayerOn = True
Case "3"
Set layerObj = ThisDrawing.Layers.Add("STBP_" & size)
layerObj.LayerOn = True
Case "" '[Cancel] button
Exit Sub
Case Else
err_msg = "Wrong Input Dude." & vbCrLf & vbCrLf
ans1 = ""
End Select
Loop While ans1 = ""
err_msg = ""
Do
ans2 = InputBox(err_msg & _
"1 = STD Louvers" & vbCrLf & _
"2 = IND louvers" & vbCrLf & _
"3 = Back ", "Pump Piping")
Select Case ans2
Case "1"
Set layerObj = ThisDrawing.Layers.Add("LOUVRES_STD")
layerObj.LayerOn = True
Case "2"
Set layerObj = ThisDrawing.Layers.Add("LOUVRES_INDUS")
layerObj.LayerOn = True
Case "3"
'Do nothing, loop control
Case "" '[Cancel] button
Exit Sub
Case Else
err_msg = "Wrong Input Dude." & vbCrLf & vbCrLf
ans2 = ""
End Select
Loop While ans2 = ""
Loop While ans2 = "3"
End Sub
Also made some changes:
Handle [Cancel] button;
Merged error messages in input box, so user doesn't have to press a lot a buttons when a mistake is done;
Made a more traditional code formatting
Part II - "Ask before doing" alternative
Sub DWG()
Dim ans1, ans2
ans1 = Choose(Val(InputBox("1 = STD Piping" & vbCrLf & "2 = Omit Pump" & vbCrLf & "3 = SBPP", "Pump Piping")), "PUMP_PIPING_", "PUMP_PIPING_", "PUMP_PIPING_")
If IsNull(ans1) Then MsgBox "Wrong Input Dude.": Exit Sub
ans2 = Choose(Val(InputBox("1 = STD Louvers" & vbCrLf & "2 = IND louvers")), "LOUVRES_STD", "LOUVRES_INDUS")
If IsNull(ans2) Then MsgBox "Wrong Input Dude.": Exit Sub
ThisDrawing.Layers.Add(ans1 & Size).LayerOn = True
ThisDrawing.Layers.Add(ans2).LayerOn = True
End Sub

Returning prompt value

Hi i'm working on a code that will return a prompt value of each case to a cell ("A1") but I can't seem to get it right, for example if I choose "case 1", I want the value on cell "A1" to be "1 - FCI18-0", is there a way to do this?. Here is my code below :
Sub TestFunction()
ans = InputBox("1 = FCI18-0" & vbCrLf & _
"2 = FCI18-1" & vbCrLf & _
"3 = FCI18-2" & vbCrLf & _
"4 = FCI18-3", "Model")
Sheets("Sheet1").Range("a1").Value = InputBoxPrompt
End Sub
Please help
How about:
Sub TestFunction()
'Declare variables
Dim test(3) As String
Dim i as long
'Set array with cases. Advantage of this set up is flexibility. In a later stage you can
'make a function that reads cases from a table in a (hidden) sheet into this array.
'This way you don't need to add new cases to your code,
'but you can add them in your excel sheet instead.
test(0) = "FCI18-0"
test(1) = "FCI18-1"
test(2) = "FCI18-2"
test(3) = "FCI18-3"
'read the input given by the user. You might want to add some checks here
i = InputBox ("Which case?")
Sheets("Sheet1").Range("a1").Value = test(i-1)
End Sub
Use Select Case
Sub TestFunction()
ans = InputBox("1 = FCI18-0" & vbCrLf & _
"2 = FCI18-1" & vbCrLf & _
"3 = FCI18-2" & vbCrLf & _
"4 = FCI18-3", "Model")
Select Case ans
Case 1: Sheets("Sheet1").Range("a1").Value = "1 = FCI18-0"
Case 2: Sheets("Sheet1").Range("a1").Value = "2 = FCI18-1"
Case 3: Sheets("Sheet1").Range("a1").Value = "3 = FCI18-2"
Case 4: Sheets("Sheet1").Range("a1").Value = "4 = FCI18-3"
End Select
End Sub

How to use variables to set pivot table Function parameters with VBA?

I would like to use variables to specifying parameters of pivot table. Most of them are working correctly, but 2 of them not. The “function” parameters “xli” and “ValueFilterQuant(pos)” don’t come up with any values. I tried it write between quotation marks and quotation marks-& but nothing happened. Is there any method to set Function parameters with variables? This is the snippet of code:
For pos = 1 To UBound(ValueQuant)
If ValueQuant(pos) = "Work" Then
xli = "xlSum"
Label = "Sum of "
Else
xli = "xlCount"
Label = "Count of "
End If
ActiveSheet.PivotTables("PivotTable1").CubeFields.GetMeasure "[database].[" & ValueQuant(pos) & "]" _
, xli, "" & Label & "" & ValueQuant(pos) & ""
ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
" PivotTable1").CubeFields("[Measures].[" & Label & "" & ValueQuant(pos) & "]"), "" & Label & "" & ValueQuant(pos) & ""
With ActiveSheet.PivotTables("PivotTable1").PivotFields( _
"[Measures].[" & Label & "" & ValueQuant(pos) & "]")
.Caption = "" & ValueFilterQuantaf(pos) & "" & ValueQuant(pos) & ""
.Function = ValueFilterQuant(pos)
End With
Next
The code is incomplete. I don't know where ValueFilterQuant(pos) is coming from.
I suspect that like xli = "xlSum" it is returning a string variable, but the thing is that those parameters should not be strings. They are numbers that are derived from an XlConsolidationFunction enumeration.
So something like:
If ValueQuant(pos) = "Work" Then
xli = XlConsolidationFunction.xlSum '-4157
Label = "Sum of "
Else
xli = XlConsolidationFunction.xlCount '-4112
Label = "Count of "
End If
Same thing needs to happen for ValueFilterQuant(pos).
Note that it is more work to derive a dynamic enumeration at runtime through a string. I guess that you are reading a string value in off a worksheet or similar process.
You could set them up in a dictionary beforehand, and pick out the numbers that way.
The list of values is here: https://msdn.microsoft.com/en-us/library/office/ff837374.aspx

VBA - How do I send new line command (\n) or tab command (\t) to a textbox.textrange.text of a PowerPointS Shape

SlideNumber = 1
Set oPPTSlide = oPPTFile.Slides(SlideNumber)
For y = 1 To oPPTSlide.Shapes.Count
MsgBox oPPTSlide.Shapes(y).Name
Next
With oPPTSlide.Shapes("Title 1")
.TextFrame.TextRange.Text = _
"Operations Monthly Report\n" & _
"April " & _
"2014"
End With
This is the code I have now. The "\n" does cause the text-box I am editing to start a new line. Is it possible? The code, in its context, is working perfectly. The exact text is sent to the text-box though, not two lines of text.
There is no "\n" in Vba instead you should use VbNewLine or VbCrLf or Vblf
Replace this
SlideNumber = 1
Set oPPTSlide = oPPTFile.Slides(SlideNumber)
For y = 1 To oPPTSlide.Shapes.Count
MsgBox oPPTSlide.Shapes(y).Name
Next
With oPPTSlide.Shapes("Title 1")
.TextFrame.TextRange.Text = _
"Operations Monthly Report" & VbCrLf & _
"April " & _
"2014"
End With
I had the problem where the vbNewLine didn't work in the UserForm, but I fixed it by checking the textBox properties and making sure multi-line is true. Give that a try.