PDA

View Full Version : Ignore new object variable



m_court15
08-27-2012, 05:38 AM
I have a userform where a user selects a word file and then it populates the userform. I recently had to add an object variable: oVars("wear").Value.

My problem is the old saved word files do not have this object, so when the old word files are opened in my userform, it errors out. Is there anyway to check for this variable and then ignore it if it is not present?


here is my code for opoening the word file in my userform:

Option Explicit
Private oVars As Variables

Private Sub cmdAddSave_Click()
'POPULATES DOCUMENT WITH VARIABLES
Set oVars = ActiveDocument.Variables
'oVars("DieType").Value = "pultrusion hv die"
oVars("DieNo").Value = Me.txtDieNo
oVars("RunDate").Value = Me.txtRunDate
oVars("DieInternalSuraface").Value = Me.txtDieInternalSuraface
oVars("InternalDamage").Value = Me.txtInternalDamage
oVars("InternalChrome").Value = Me.txtInternalChrome
oVars("InternalSurface").Value = Me.txtInternalSurface
oVars("AFace").Value = Me.txtAFace
oVars("ADamage").Value = Me.txtADamage
oVars("BFace").Value = Me.txtBFace
oVars("BDamage").Value = Me.txtBDamage
oVars("BatchNo").Value = Me.txtBatchNo

oVars("ACT1").Value = Me.LabelACT1
oVars("AChrome").Value = Me.txtAChrome
oVars("ACT2").Value = Me.LabelACT2
oVars("txtACT2").Value = Me.txtACT2
oVars("ACT3").Value = Me.LabelACT3
oVars("txtACT3").Value = Me.txtACT3
oVars("SF4").Value = Me.txtSF4
oVars("ASF1").Value = Me.LabelASF1
oVars("ASurface").Value = Me.txtASurface
oVars("ASF2").Value = Me.LabelASF2
oVars("AP6").Value = Me.txtAp6

oVars("BCT1").Value = Me.LabelBCT1
oVars("BChrome").Value = Me.txtBChrome
oVars("BCT2").Value = Me.LabelBCT2
oVars("txtBCT2").Value = Me.txtBCT2
oVars("BCT3").Value = Me.LabelBCT3
oVars("txtBCT3").Value = Me.txtBCT3
oVars("BSF1").Value = Me.LabelBSF1
oVars("BSurface").Value = Me.txtBSurface
oVars("BSF2").Value = Me.LabelBSF2
oVars("AP9").Value = Me.txtAP9

oVars("SF5").Value = Me.txtSF5
oVars("SF6").Value = Me.txtSF6
oVars("CT4").Value = Me.txtCT4
oVars("CT5").Value = Me.txtCT5
oVars("CT6").Value = Me.txtCT6
oVars("AP1").Value = Me.txtAP1
oVars("AP2").Value = Me.txtAP2
oVars("AP3").Value = Me.txtAP3
oVars("AP4").Value = Me.txtAP4
oVars("AP7").Value = Me.txtAP7
oVars("AP10").Value = Me.txtAP10
oVars("AP11").Value = Me.txtAP11
oVars("AP12").Value = Me.txtAP12
oVars("AP13").Value = Me.txtAP13
oVars("AP14").Value = Me.txtAP14
oVars("AP15").Value = Me.txtAP15
oVars("DieCondition").Value = Me.txtDieCondition
oVars("Description").Value = Me.txtDescription
oVars("Size").Value = Me.txtSize
oVars("DieShape").Value = Me.txtDieShape
oVars("FTran").Value = Me.txtFTran
oVars("name").Value = Me.txtName
oVars("weawr").Value = Me.txtWear

'LEAVES FIELD BLANK IF NOTHING IS ENTERED
If Me.txtAP15 = "" Then
oVars("AP15").Value = " "
Else
oVars("AP15").Value = Me.txtAP15
End If
If Me.txtAP14 = "" Then
oVars("AP14").Value = " "
Else
oVars("AP14").Value = Me.txtAP14
End If
If Me.txtAP13 = "" Then
oVars("AP13").Value = " "
Else
oVars("AP13").Value = Me.txtAP13
End If
If Me.txtAP12 = "" Then
oVars("AP12").Value = " "
Else
oVars("AP12").Value = Me.txtAP12
End If
If Me.txtAP11 = "" Then
oVars("AP11").Value = " "
Else
oVars("AP11").Value = Me.txtAP11
End If
If Me.txtAP10 = "" Then
oVars("AP10").Value = " "
Else
oVars("AP10").Value = Me.txtAP10
End If
If Me.txtAP9 = "" Then
oVars("AP9").Value = " "
Else
oVars("AP9").Value = Me.txtAP9
End If
If Me.txtAP7 = "" Then
oVars("AP7").Value = " "
Else
oVars("AP7").Value = Me.txtAP7
End If
If Me.txtAp6 = "" Then
oVars("AP6").Value = " "
Else
oVars("AP6").Value = Me.txtAp6
End If
If Me.txtAP4 = "" Then
oVars("AP4").Value = " "
Else
oVars("AP4").Value = Me.txtAP4
End If
If Me.txtAP3 = "" Then
oVars("AP3").Value = " "
Else
oVars("AP3").Value = Me.txtAP3
End If
If Me.txtAP2 = "" Then
oVars("AP2").Value = " "
Else
oVars("AP2").Value = Me.txtAP2
End If
If Me.txtAP1 = "" Then
oVars("AP1").Value = " "
Else
oVars("AP1").Value = Me.txtAP1
End If
If Me.txtDieNo = "" Then
oVars("DieNo").Value = " "
Else
oVars("Dieno").Value = Me.txtDieNo
End If
If Me.txtRunDate = "" Then
oVars("RunDate").Value = " "
Else
oVars("RunDate").Value = Me.txtRunDate
End If
If Me.txtDieInternalSuraface = "" Then
oVars("DieInternalSuraface").Value = " "
Else
oVars("DieInternalSuraface").Value = Me.txtDieInternalSuraface
End If
If Me.txtInternalDamage = "" Then
oVars("InternalDamage").Value = " "
Else
oVars("InternalDamage").Value = Me.txtInternalDamage
End If
If Me.txtInternalChrome = "" Then
oVars("InternalChrome").Value = " "
Else
oVars("InternalChrome").Value = Me.txtInternalChrome
End If
If Me.txtInternalSurface = "" Then
oVars("InternalSurface").Value = " "
Else
oVars("InternalSurface").Value = Me.txtInternalSurface
End If
If Me.txtAFace = "" Then
oVars("AFace").Value = " "
Else
oVars("AFace").Value = Me.txtAFace
End If
If Me.txtADamage = "" Then
oVars("ADamage").Value = " "
Else
oVars("ADamage").Value = Me.txtADamage
End If
If Me.txtAChrome = "" Then
oVars("AChrome").Value = " "
Else
oVars("AChrome").Value = Me.txtAChrome
End If
If Me.txtASurface = "" Then
oVars("ASurface").Value = " "
Else
oVars("ASurface").Value = Me.txtASurface
End If
If Me.txtBFace = "" Then
oVars("BFace").Value = " "
Else
oVars("BFace").Value = Me.txtBFace
End If
If Me.txtBDamage = "" Then
oVars("BDamage").Value = " "
Else
oVars("BDamage").Value = Me.txtBDamage
End If
If Me.txtBChrome = "" Then
oVars("BChrome").Value = " "
Else
oVars("BChrome").Value = Me.txtBChrome
End If
If Me.txtBSurface = "" Then
oVars("BSurface").Value = " "
Else
oVars("BSurface").Value = Me.txtBSurface
End If
If Me.txtBatchNo = "" Then
oVars("BatchNo").Value = " "
Else
oVars("BatchNo").Value = Me.txtBatchNo
End If
If Me.txtCT6 = "" Then
oVars("CT6").Value = " "
Else
oVars("CT6").Value = Me.txtCT6
End If
If Me.txtCT5 = "" Then
oVars("CT5").Value = " "
Else
oVars("CT5").Value = Me.txtCT5
End If
If Me.txtCT4 = "" Then
oVars("CT4").Value = " "
Else
oVars("CT4").Value = Me.txtCT4
End If
If Me.txtSF6 = "" Then
oVars("SF6").Value = " "
Else
oVars("SF6").Value = Me.txtSF6
End If
If Me.txtSF5 = "" Then
oVars("SF5").Value = " "
Else
oVars("SF5").Value = Me.txtSF5
End If
If Me.txtSF4 = "" Then
oVars("SF4").Value = " "
Else
oVars("SF4").Value = Me.txtSF4
End If
If Me.txtDieCondition = "" Then
oVars("DieCondition").Value = " "
Else
oVars("DieCondition").Value = Me.txtDieCondition
End If
If Me.txtDescription = "" Then
oVars("description").Value = " "
Else
oVars("Description").Value = Me.txtDescription
End If
If Me.txtSize = "" Then
oVars("Size").Value = " "
Else
oVars("Size").Value = Me.txtSize
End If
If Me.txtFTran = "" Then
oVars("FTran").Value = " "
Else
oVars("FTran").Value = Me.txtFTran
End If
If Me.txtName = "" Then
oVars("name").Value = " "
Else
oVars("name").Value = Me.txtName
End If
If Me.txtWear = "" Then
oVars("wear").Value = " "
Else
oVars("wear").Value = Me.txtWear
End If
ActiveDocument.Fields.Update
Set oVars = Nothing
Unload Me
'SAVE FUNCTION
On Error Resume Next
Dim strFileName As String
Dim strdir As String
Dim strNewFile As String
'save pdf with name that is diplayed in textbox1
strdir = "\\hfgpmain\engineering\pultrusion\tooling\inspection (file://\\hfgpmain\engineering\pultrusion\tooling\inspection) documents\view only\die\"
strFileName = Me.TextBox1 & ".pdf"
strNewFile = strdir & strFileName
ActiveDocument.SaveAs strNewFile, FileFormat:=wdFormatPDF
ActiveDocument.Close savechanges:=True
MainMenu.Show
End Sub
Private Sub cmdBack_Click()
'GOES BACK TO MAIN MENU
Unload Me
ActiveDocument.Close savechanges:=False
MainMenu.Show
End Sub
Private Sub cmdClose_Click()
'CLOSES FORM WITH OUT SAVING
Unload Me
ActiveDocument.Close savechanges:=False
End Sub
Private Sub UserForm_Initialize()
'OPENS OPEN DIALOG BOX
Dim fd As FileDialog
Dim ffs As FileDialogFilters
Set fd = Application.FileDialog(msoFileDialogOpen)
With fd
Set ffs = .Filters
With ffs
.Clear
.Add "wdFormatXMLDocument", "*.docx"
End With
.InitialFileName = "\\hfgpmain\engineering\pultrusion\tooling\inspection (file://\\hfgpmain\engineering\pultrusion\tooling\inspection) documents\edit\die\HV\"
If .Show = 0 Then ActiveDocument.Close savechanges:=False 'user pressed the cancel button
'Else
.Execute
'End If
End With
Set fd = Nothing
'populates userform from word document
Set oVars = ActiveDocument.Variables
Me.labelDie = oVars("DieType").Value
Me.txtDieNo = oVars("DieNo").Value
Me.txtRunDate = oVars("RunDate").Value
Me.txtDieInternalSuraface = oVars("DieInternalSuraface").Value
Me.txtInternalDamage = oVars("InternalDamage").Value
Me.txtInternalChrome = oVars("InternalChrome").Value
Me.txtInternalSurface = oVars("InternalSurface").Value
Me.txtAFace = oVars("AFace").Value
Me.txtADamage = oVars("ADamage").Value
Me.txtAChrome = oVars("AChrome").Value
Me.txtASurface = oVars("ASurface").Value
Me.txtBFace = oVars("BFace").Value
Me.txtBDamage = oVars("BDamage").Value
Me.txtBChrome = oVars("BChrome").Value
Me.txtBSurface = oVars("BSurface").Value
Me.txtBatchNo = oVars("BatchNo").Value

Me.LabelACT1 = oVars("ACT1").Value
Me.LabelACT2 = oVars("ACT2").Value
Me.txtACT2 = oVars("txtACT2").Value
Me.LabelACT3 = oVars("ACT3").Value
Me.txtACT3 = oVars("txtACT3").Value
Me.LabelASF1 = oVars("ASF1").Value
Me.LabelASF2 = oVars("ASF2").Value
Me.LabelBCT1 = oVars("BCT1").Value
Me.LabelBCT2 = oVars("BCT2").Value
Me.txtBCT2 = oVars("txtBCT2").Value
Me.LabelBCT3 = oVars("BCT3").Value
Me.txtBCT3 = oVars("txtBCT3").Value
Me.LabelBSF1 = oVars("BSF1").Value
Me.LabelBSF2 = oVars("BSF2").Value

Me.txtSF4 = oVars("SF4").Value
Me.txtSF5 = oVars("SF5").Value
Me.txtSF6 = oVars("SF6").Value
Me.txtCT4 = oVars("CT4").Value
Me.txtCT5 = oVars("CT5").Value
Me.txtCT6 = oVars("CT6").Value
Me.txtAP1 = oVars("AP1").Value
Me.txtAP2 = oVars("AP2").Value
Me.txtAP3 = oVars("AP3").Value
Me.txtAP4 = oVars("AP4").Value
Me.txtAp6 = oVars("AP6").Value
Me.txtAP7 = oVars("AP7").Value
Me.txtAP9 = oVars("AP9").Value
Me.txtAP10 = oVars("AP10").Value
Me.txtAP11 = oVars("AP11").Value
Me.txtAP12 = oVars("AP12").Value
Me.txtAP13 = oVars("AP13").Value
Me.txtAP14 = oVars("AP14").Value
Me.txtAP15 = oVars("AP15").Value
Me.labelDie = oVars("DieType").Value
Me.txtDieCondition = oVars("DieCondition").Value
Me.txtDescription = oVars("Description").Value
Me.txtSize = oVars("Size").Value
Me.txtDateNew = oVars("DateNew").Value
Me.txtDieShape = oVars("DieShape").Value
Me.txtFTran = oVars("FTran").Value
Me.txtName = oVars("name").Value
Me.txtWear = oVars("wear").Value

'If txtCTF1 = "N/A" Then txtCTF1.Enabled = False

'display file name in textbox1 without extension
'Me.TextBox1 = Mid(ActiveDocument.name, 1, Len(ActiveDocument.name) - 5)
'Me.TextBox1 = Split(ActiveDocument.name, ".")(0)
Me.TextBox1 = Left(ActiveDocument.name, InStrRev(ActiveDocument.name, ".") - 1)

'ADDS PICTURE TO USERFORM DEPENDING ON DIE TYPE
If Me.labelDie = "U Shape Die Inspection form" Then
Image1.Picture = LoadPicture("\\hfgpmain\engineering\pultrusion\tooling\inspection (file://\\hfgpmain\engineering\pultrusion\tooling\inspection) documents\images\U Shape.jpg")
End If
If Me.labelDie = "Square Die Inspection form" Then
Image1.Picture = LoadPicture("\\hfgpmain\engineering\pultrusion\tooling\inspection (file://\\hfgpmain\engineering\pultrusion\tooling\inspection) documents\images\SquareDie.jpg")
End If
If Me.labelDie = "Round Die Inspection form" Then
Image1.Picture = LoadPicture("\\hfgpmain\engineering\pultrusion\tooling\inspection (file://\\hfgpmain\engineering\pultrusion\tooling\inspection) documents\images\Round.jpg")
End If
If Me.labelDie = "C Channel Die Inspection form" Then
Image1.Picture = LoadPicture("\\hfgpmain\engineering\pultrusion\tooling\inspection (file://\\hfgpmain\engineering\pultrusion\tooling\inspection) documents\images\Cchannel.jpg")
End If
If Me.labelDie = "HV Die Inspection form" Then
Image1.Picture = LoadPicture("\\hfgpmain\engineering\pultrusion\tooling\inspection (file://\\hfgpmain\engineering\pultrusion\tooling\inspection) documents\images\HV.jpg")
End If
ActiveDocument.Fields.Update
Application.ScreenUpdating = True
Application.Visible = False
End Sub

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
' Prevents use of the Close button
If CloseMode = vbFormControlMenu Then
MsgBox "Clicking the Close button does not work."
Cancel = True
End If
End Sub

gmaxey
08-27-2012, 08:24 AM
Use some error handling:

Sub ScratchMacro()
'A quick macro scratch pad created by Greg Maxey
Dim oVars As Variables
Set oVars = ActiveDocument.Variables
On Error Resume Next
Debug.Print oVars("IDontExist").Value
On Error GoTo Err_Handler1
Debug.Print oVars("IDontExist").Value
On Error GoTo Err_Handler2
Err_ReEntry:
Debug.Print oVars("IDontExist").Value & " but I do not and I'm empty"
Exit Sub
Err_Handler1:
Resume Next
Err_Handler2:
oVars("IDontExist").Value = " "
Resume Err_ReEntry
End Sub

Paul_Hossler
08-27-2012, 12:59 PM
I usually do something like this



Option Explicit
Sub test()
Dim iIndex As Long
Dim oVars As Variables

ActiveDocument.Variables.Add "Yes", "123123123"
Set oVars = ActiveDocument.Variables

iIndex = -1
On Error Resume Next
iIndex = oVars("Yes")
On Error GoTo 0

If iIndex <> -1 Then MsgBox "Found"


iIndex = -1
On Error Resume Next
iIndex = oVars("No")
On Error GoTo 0

If iIndex <> -1 Then MsgBox "Found"

MsgBox "done"
End Sub


Paul

gmaxey
08-27-2012, 01:29 PM
If I wanted to see if a variable exists or not, I would probably use a function:

Option Explicit
Sub Test()
MsgBox DoesVariableExist(ActiveDocument.Variables("Test"))
End Sub
Function DoesVariableExist(ByRef oVar As Variable) As Boolean
Dim vValue As Variant
On Error Resume Next
vValue = oVar.Value
If Err.Number <> 0 Then
DoesVariableExist = False
Else
DoesVariableExist = True
End If
End Function

m_court15
08-29-2012, 08:21 AM
Hmm.. I have tried all the code, and none of it has worked. Any ideas what I'm doing wrong? I'm pretty new to coding. This error did not start occuring until I added another box to the userform with this code:"Me.txtWear = oVars("wear").Value". So some of the previous word documents that were saved do not have this variable. The old documents that I'm trying to open are the ones giving me this error. The new documents that have been saved with this variable are giving me no problems. Here is the error I'm getting....

Frosty
08-29-2012, 10:36 AM
When you load this userform, you should probably do the following:
1. Check to see if the variable exists in the document
2. If it doesn't, create it with an empty value (or a default value of your choice).

The way to apply this is to perform this action in the initialize routine of your userform... instead of where you get the document variables with the line:
Set oVars = ActiveDocument.Variables
use this line instead
Set oVars = fGetDocVariables

and then put the following code either in your userform or a code module (your preference)

'return the variables of the passed document,
'uses active document if no document passed
Public Function fGetDocVariables(Optional oDoc As Document) As Variables
If oDoc Is Nothing Then
Set oDoc = ActiveDocument
End If

'any document variables you need to check for, do that here
If fDoesVariableExist("wear") = False Then
oDoc.Variables.Add "wear", " "
End If

'and return the variables collection to the function
Set fGetDocVariables = oDoc.Variables
End Function
Public Function fDoesVariableExist(sVarName As String, Optional oDoc As Document) As Boolean
Dim vValue As Variant

If oDoc Is Nothing Then
Set oDoc = ActiveDocument
End If
'dummy test, to see what the value is
On Error Resume Next
vValue = oDoc.Variables(sVarName).Value
'if no error, it exists
If Err.Number = 0 Then
fDoesVariableExist = True
End If
End Function

Frosty
08-29-2012, 10:41 AM
Note: I editted the code above, since I changed Greg's function to allow a string parameter, rather than an actual object variable parameter.

Frosty
08-29-2012, 10:45 AM
And also, since you can't create a variable with no value (an empty string), I'm not sure what you're populating in your userform. You repeat the setting of a text box to the value of the variable... but in this case the value of the variable will be " " which may or may not be the desired output.

In general, if I'm using a lot of document variables-- I try to make sure they are there with a value of " " rather than do a lot of tests to see if they are there at all, but you could also wrap any of these values just by using the function directly, a la...


If fDoesVariableExist("wear") Then
Me.txtWear = oVars("wear").Value
End If