Help with an error I am getting in VBA?
Everytime I try to use a Selection.Something I get an ‘Object or With variable not Set’ error. It used to only happen every other time I ran the program (for some reason), and now it happens every time. Any Ideas why?
Observing members:
0
Composing members:
0
9 Answers
You might not have used selection with proper object. You can check it out in object browser (View->Object Browser) if the object you’ve used has that property/method or not.
For example, if you are trying to read a selected range in Excel, it’s in window object. So, you should use: “ActiveWindow.RangeSelection”.
And, if you want to set it to a variable, make sure it has been declared correctly as data type, in this case Range.
Example:
Dim MyRange As Range
Set MyRange = ActiveWindow.RangeSelection
If you have used “With” block, make sure you end it with “End With”.
Example:
With Cells(1,1)
.Value=1
End With
There’s more sophisticated way to get an input range. You can use InputBox.
Make sure you use InputBox method of the application object. You can restrict the input to a range by giving the named argument “Type” a value of 8.
InputBox function doesn’t have the named argument “Type” to restrict the input.
Example:
Dim MyRange As Range
Set MyRange = Application.InputBox(“Select the Range”, “Input Range”, Type:=8)
Here is the code I was using (that did work, and now doesn’t, for some reason):
Public Sub Load_Next_Document(WordApp As Word.Application, File_Path As String,_ NewFilePath As String, NewDoc As Word.Document)
Dim Old_Doc As Word.Document
Set NewDoc = WordApp.Documents.Add
Set Old_Doc = WordApp.Documents.Open(File_Path)
Old_Doc.Activate
Selection.WholeStory ’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’‘broken line
Selection.Copy ’’’’’’’’’’’’’’’’’’’’’’’’‘also broken
NewDoc.Activate
Selection.WholeStory ’’’’’’‘broke
Selection.Paste ’’’’’’’’’’’’’‘broke
Selection.WholeStory ’’’’’’’’’’’’’’‘broke
Selection.Font.Name = “Courier New” ’’’’’’’’’’’’’’’’‘broke
Old_Doc.Close
NewFilePath = Left(File_Path, Len(File_Path) – 4) & ” – ” & MonthName(Month_(DateTime.Now)) & ” ” & Str(Day(DateTime.Now)) & ”.doc”
End Sub
There seems nothing wrong with selection.
But, how do you get the “File_Path”? Have you called this procedure from another?
As far as I have understood, this procedure opens a file and copies its contents to a new one and change the font to Courier New; and close the old file. Then, do you pass this NewFilePath to File_Path in the next iteration?
Try replacing “_NewFilePath” with “NewFilePath” in the arguments list, if it is not mistakenly typed.
the ‘_’ is just to show that the line of code continues on the next line. It isn’t part of the variable name.
and yes, that is what it does… I call this procedure and i pass in the File_Path of the origional doc
I just tested some of my previous programs that I haven’t even changed since they worked, and even their Selection.Something lines aren’t working…
Is it a computer or word setting?
Sorry, don’t know what’s going wrong with it then?
Once, I got some unknowing errors in Excel. Then, I closed everything, shut the computer down and restarted again. It worked. You can try this.
ok, i will. I just tested it on another machine and it worked, so that must be it.
Answer this question
This question is in the General Section. Responses must be helpful and on-topic.