PDA

View Full Version : Solved: Error: Object variable or with block variable not set



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??

Bob Phillips
01-15-2007, 11:42 AM
I don't get that error, but the error handler label is missing.

fumei
01-15-2007, 11:57 AM
Looking at it.

BTW: If ActiveDocument.Bookmarks.Exists("Instructions") = True Then
ActiveDocument.Bookmarks("Instructions").Select
Selection.Delete
End If You do not need to select the bookmarks to delete it, and its text. ActiveDocument.Bookmarks("Instructions").Range.Deletewill do it. No point in making Selections when you do not have to.

fumei
01-15-2007, 12:23 PM
So far, I am not seeing anything that would give that error.

mileski
01-15-2007, 12:51 PM
Hi clhare,

First this: in your cmdOk, set the label errhandle.

The code should come at the end of the sub, between "Unload frmQMSO" and End Sub:


Exit Sub

errhandle:

'some code here

End Sub

Second, the error you get means that the problem isn't in your cmdOk Sub, but in the declaration section of the module. Such an error you get when you declare objects like here:

Public objWord As Word.Application
Public objDoc As Word.Document

try disabling these lines, and in the code instead of objWord and objDoc use direct Word.Application and Word.Document, so you can debug the problem.

Second, check your References. Maybe something is missing.

You can try this way to write the command:


Dim objDoc As Word.Document

For Each objDoc In Documents

'code comes here

Next

Notice, I'm using only Documents. This comes from Word.Documents


I hope it will solve your problem

Norie
01-15-2007, 12:56 PM
Robert

Are you sure the error is with the declarations?

They work fine for me.

clhare
01-15-2007, 01:13 PM
I made the updates suggested in the last post and I'm no longer getting an error, but now it doesn't cycle through all the documents. I tried running the macro with 3 open documents and only 2 were updated. The third one is still open when the macro finishes.

I also checked the references, and there does not appear to be a problem with them.

mileski
01-15-2007, 01:19 PM
Robert

Are you sure the error is with the declarations?

They work fine for me.


Hi Norie,

I'm sure the problem is there. I also don't get the error. But, the error suggests that some object isn't properly set, and as I see the next post, it solved the problem :)

Maybe the real truth is in the References that clhare has in the doc.

clhare
01-15-2007, 01:22 PM
Any idea why it doesn't cycle through all the open documents anymore?

Norie
01-15-2007, 01:34 PM
Cheryl

Can you post the current code?

Also have you tried stepping through it?

Robert

What I mean is I don't think there is a problem with the actual declarations
but there could be a problem with how the variables are being used.

Mind you I've not fully examined the code, I'm a bit rusty on Word VBA, more of an Excel/Access person.

One thing I do notice is the use of Activate and ActiveDocument.

Now that sort of thing in Excel VBA could cause problems.

I don't see why the object variable objDoc can't be used instead of ActiveDocument.

mileski
01-15-2007, 01:42 PM
Any idea why it doesn't cycle through all the open documents anymore?
Here is the code I changed for you:


Private Sub cmdOK_Click()

Dim objDoc As Word.Document

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 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").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

Application.ScreenUpdating = True ' <= Watch this line!

MsgBox "The Procedure finished properly!"

Unload frmQMCSO

Exit Sub

errhandle:

'Catch the error here, or whatever you want to do next.
Application.ScreenUpdating = True ' <= Watch this line!
MsgBox "You got some error in Code: " & Err.Description
End Sub


I'm pretty sure now, that your problem is in your code where you change the filename. I don't get this part right, but I'm sure you want to accomplish something.

Start the procedure, and look at the result Msgbox. Will it be "The procedure ended successfully" or "You got some error in Code..."

mileski
01-15-2007, 01:47 PM
Cheryl

Can you post the current code?

Also have you tried stepping through it?

Robert

What I mean is I don't think there is a problem with the actual declarations
but there could be a problem with how the variables are being used.

Mind you I've not fully examined the code, I'm a bit rusty on Word VBA, more of an Excel/Access person.

One thing I do notice is the use of Activate and ActiveDocument.

Now that sort of thing in Excel VBA could cause problems.

I don't see why the object variable objDoc can't be used instead of ActiveDocument.

Yes, of course. But, for debuging reasons, like I suggested, it's better to use direct use of the object, instead of using a new object. Just for debugging reasons.

Norie
01-15-2007, 01:57 PM
Robert

Sorry I don't understand what you mean by using a new object.

As the loop runs objDoc refers to each open document, so why not just use it?

mileski
01-15-2007, 02:06 PM
Robert

Sorry I don't understand what you mean by using a new object.

As the loop runs objDoc refers to each open document, so why not just use it?

Well, first of all, he is showing us only a part of the vba that he uses. Second, we don't have his References. Third, I think the problem happens not with that object, but with Application.Documents <= object.

if you use directly for each obj in Documents, "Documents" is under Word.Documents and not Application.Documents. (don't ask me why is this the problem, because I don't have all the relevant informatons)

Why, don't ask me. If he attaches the whole Document structure, maybe we shall find the problem. Working just with partial code, isn't my idea of guessing what's wrong with it. :)

Norie
01-15-2007, 02:29 PM
Robert

I'm still not 100% sure what you mean.

Documents is a property of both Word and Application.

And Application is a property of Word.

mileski
01-15-2007, 02:39 PM
Robert

I'm still not 100% sure what you mean.

Documents is a property of both Word and Application.

And Application is a property of Word.


Norie, I'm also unsure about the resolution of the problem, which indeed resolved it, but, as I explained in the previous post, not having all the relevant information, leads to unsure soltions. ;)

fumei
01-15-2007, 03:08 PM
I don't see that References are the issue. This is not a created instance of Word. It is Word being used in itself. In which case, Word.Documents is the same as Application.Documents.


As the loop runs objDoc refers to each open document, so why not just use it?I agree. I think objDoc should be used.

mileski
01-15-2007, 04:05 PM
Didn't the guy say, that he solved the problem with my proposed suggestion?

Norie
01-15-2007, 04:37 PM
Robert

I think the original problem is solved. ie the "Object variable or with block variable not set" error.

But the OP is now asking why the code isn't working for all the open documents.

And I see that, perhaps wrongly, as a problem with how the documents are being referenced.

Like I said I'm a little rusty with Word VBA but I do know that in Excel VBA
using Activate and/or Select/Selection is not normally needed and indeed can be a bad idea.

Bob Phillips
01-15-2007, 05:45 PM
Robert

I think the original problem is solved. ie the "Object variable or with block variable not set" error.

But the OP is now asking why the code isn't working for all the open documents.

And I see that, perhaps wrongly, as a problem with how the documents are being referenced.

Like I said I'm a little rusty with Word VBA but I do know that in Excel VBA
using Activate and/or Select/Selection is not normally needed and indeed can be a bad idea.

But he is activating objDoc as he iterates through all the documents, wasteful I agree, but he should be working on the correct document at all times.

Isn't the problem maybe something to do with the saving logic?

clhare
01-17-2007, 07:14 AM
The new code works perfectly. Thank you very much for your help!

mileski
01-17-2007, 09:45 AM
The new code works perfectly. Thank you very much for your help!


I was glad that I could help you ;) Anytime.