clhare
01-15-2007, 11:00 AM
I am getting an error, but I can't figure out what's causing it. Can someone help?
I have the following global variables set up in a module called "Variables":
Option Explicit
' Declare variables to be used in more than one procedure
Public strClientName As String
Public strBCName As String
Public strOldFilename1 As String
Public strOldFilename2 As String
Public strNewFilename_Short1 As String
Public strNewFilename_Short2 As String
Public strNewFilename_Short3 As String
Public strLocation As String
Public strVersion As String
Public objWord As Word.Application
Public objDoc As Word.Document
I'm running a user form (frmQMCSO) that has the following code in the OK button:
Private Sub cmdOK_Click()
On Error GoTo errhandle
' Hide form until it's done
frmQMCSO.Hide
' Turn screen updating off
Application.ScreenUpdating = False
If opt3x.Value = True Then strVersion = "3x4x"
If opt4x.Value = True Then strVersion = "4x"
strClientName = txtCompanyName.Value
strBCName = txtBCName.Value
strLocation = txtLocation.Value
' Update one document at a time
'For Each doc In Documents
For Each objDoc In Application.Documents
objDoc.Activate
' Make sure document is unlocked
If ActiveDocument.ProtectionType = wdAllowOnlyFormFields Then
ActiveDocument.Unprotect
End If
' Delete bookmarked instructions
If ActiveDocument.Bookmarks.Exists("Instructions") = True Then
ActiveDocument.Bookmarks("Instructions").Select
Selection.Delete
End If
' Replace variables with user's text
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "[Premier Company] Benefits Center"
.Replacement.Text = strBCName
.Forward = True
.Wrap = wdFindContinue
End With
Selection.Find.Execute Replace:=wdReplaceAll
With Selection.Find
.Text = "[Premier Company]"
.Replacement.Text = strClientName
.Forward = True
.Wrap = wdFindContinue
End With
Selection.Find.Execute Replace:=wdReplaceAll
' Remove QM and version from hidden text filename
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "QM "
.Font.Hidden = True
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
End With
Selection.Find.Execute Replace:=wdReplaceAll
With Selection.Find
.Text = " (3x4x)"
.Font.Hidden = True
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
End With
Selection.Find.Execute Replace:=wdReplaceAll
With Selection.Find
.Text = " (4x)"
.Font.Hidden = True
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
End With
Selection.Find.Execute Replace:=wdReplaceAll
' Assign values to variables
strOldFilename1 = ActiveDocument.Name
If strVersion = "3x4x" Then
' Take old filename minus last 11 characters (which would be ' (3x4x).dot')
strNewFilename_Short1 = Left(strOldFilename1, Len(strOldFilename1) - 11)
' Take revised filename minus first 3 characters (which would be ' QM')
strNewFilename_Short2 = Right(strNewFilename_Short1, Len(strNewFilename_Short1) - 3)
ElseIf strVersion = "4x" Then
' Take old filename minus last 9 characters (which would be ' (4x).dot')
strNewFilename_Short1 = Left(strOldFilename1, Len(strOldFilename1) - 9)
' Take revised filename minus first 3 characters (which would be ' QM')
strNewFilename_Short2 = Right(strNewFilename_Short1, Len(strNewFilename_Short1) - 3)
End If
' Add backslash to location to get new file in correct folder
strLocation = strLocation & "\"
' Save under corrected filename
ActiveDocument.SaveAs FileName:=strLocation & strNewFilename_Short2 _
, FileFormat:=wdFormatTemplate, LockComments:=False, Password:="", _
AddToRecentFiles:=True, WritePassword:="", ReadOnlyRecommended:=False, _
EmbedTrueTypeFonts:=False, SaveNativePictureFormat:=False, SaveFormsData _
:=False, SaveAsAOCELetter:=False
' Assign values to variables
strOldFilename2 = ActiveDocument.Name
' Take old filename minus last 4 characters (which would be '.dot')
strNewFilename_Short3 = Left(strOldFilename2, Len(strOldFilename2) - 4)
' Reset filename in Properties
With ActiveDocument
.BuiltInDocumentProperties(wdPropertyTitle) = strNewFilename_Short3
End With
' Remove all highlighting in document
Dim StoryRange As Range
For Each StoryRange In ActiveDocument.StoryRanges
StoryRange.HighlightColorIndex = wdNoHighlight
Next StoryRange
' Lock the document to allow user to tab through fields
ActiveDocument.Protect Type:=wdAllowOnlyFormFields
' Save and close new file
ActiveDocument.Close savechanges:=wdSaveChanges
' Update next active document
Next objDoc
' Unload forms when done
Unload frmQMCSO
End Sub
When I run the user form, I get an error saying "Object variable or with block variable not set". What am I doing wrong??
I have the following global variables set up in a module called "Variables":
Option Explicit
' Declare variables to be used in more than one procedure
Public strClientName As String
Public strBCName As String
Public strOldFilename1 As String
Public strOldFilename2 As String
Public strNewFilename_Short1 As String
Public strNewFilename_Short2 As String
Public strNewFilename_Short3 As String
Public strLocation As String
Public strVersion As String
Public objWord As Word.Application
Public objDoc As Word.Document
I'm running a user form (frmQMCSO) that has the following code in the OK button:
Private Sub cmdOK_Click()
On Error GoTo errhandle
' Hide form until it's done
frmQMCSO.Hide
' Turn screen updating off
Application.ScreenUpdating = False
If opt3x.Value = True Then strVersion = "3x4x"
If opt4x.Value = True Then strVersion = "4x"
strClientName = txtCompanyName.Value
strBCName = txtBCName.Value
strLocation = txtLocation.Value
' Update one document at a time
'For Each doc In Documents
For Each objDoc In Application.Documents
objDoc.Activate
' Make sure document is unlocked
If ActiveDocument.ProtectionType = wdAllowOnlyFormFields Then
ActiveDocument.Unprotect
End If
' Delete bookmarked instructions
If ActiveDocument.Bookmarks.Exists("Instructions") = True Then
ActiveDocument.Bookmarks("Instructions").Select
Selection.Delete
End If
' Replace variables with user's text
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "[Premier Company] Benefits Center"
.Replacement.Text = strBCName
.Forward = True
.Wrap = wdFindContinue
End With
Selection.Find.Execute Replace:=wdReplaceAll
With Selection.Find
.Text = "[Premier Company]"
.Replacement.Text = strClientName
.Forward = True
.Wrap = wdFindContinue
End With
Selection.Find.Execute Replace:=wdReplaceAll
' Remove QM and version from hidden text filename
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "QM "
.Font.Hidden = True
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
End With
Selection.Find.Execute Replace:=wdReplaceAll
With Selection.Find
.Text = " (3x4x)"
.Font.Hidden = True
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
End With
Selection.Find.Execute Replace:=wdReplaceAll
With Selection.Find
.Text = " (4x)"
.Font.Hidden = True
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
End With
Selection.Find.Execute Replace:=wdReplaceAll
' Assign values to variables
strOldFilename1 = ActiveDocument.Name
If strVersion = "3x4x" Then
' Take old filename minus last 11 characters (which would be ' (3x4x).dot')
strNewFilename_Short1 = Left(strOldFilename1, Len(strOldFilename1) - 11)
' Take revised filename minus first 3 characters (which would be ' QM')
strNewFilename_Short2 = Right(strNewFilename_Short1, Len(strNewFilename_Short1) - 3)
ElseIf strVersion = "4x" Then
' Take old filename minus last 9 characters (which would be ' (4x).dot')
strNewFilename_Short1 = Left(strOldFilename1, Len(strOldFilename1) - 9)
' Take revised filename minus first 3 characters (which would be ' QM')
strNewFilename_Short2 = Right(strNewFilename_Short1, Len(strNewFilename_Short1) - 3)
End If
' Add backslash to location to get new file in correct folder
strLocation = strLocation & "\"
' Save under corrected filename
ActiveDocument.SaveAs FileName:=strLocation & strNewFilename_Short2 _
, FileFormat:=wdFormatTemplate, LockComments:=False, Password:="", _
AddToRecentFiles:=True, WritePassword:="", ReadOnlyRecommended:=False, _
EmbedTrueTypeFonts:=False, SaveNativePictureFormat:=False, SaveFormsData _
:=False, SaveAsAOCELetter:=False
' Assign values to variables
strOldFilename2 = ActiveDocument.Name
' Take old filename minus last 4 characters (which would be '.dot')
strNewFilename_Short3 = Left(strOldFilename2, Len(strOldFilename2) - 4)
' Reset filename in Properties
With ActiveDocument
.BuiltInDocumentProperties(wdPropertyTitle) = strNewFilename_Short3
End With
' Remove all highlighting in document
Dim StoryRange As Range
For Each StoryRange In ActiveDocument.StoryRanges
StoryRange.HighlightColorIndex = wdNoHighlight
Next StoryRange
' Lock the document to allow user to tab through fields
ActiveDocument.Protect Type:=wdAllowOnlyFormFields
' Save and close new file
ActiveDocument.Close savechanges:=wdSaveChanges
' Update next active document
Next objDoc
' Unload forms when done
Unload frmQMCSO
End Sub
When I run the user form, I get an error saying "Object variable or with block variable not set". What am I doing wrong??