|
-
Sep 22nd, 2005, 11:25 PM
#1
Thread Starter
Fanatic Member
Object variable or with set variable not set. (resolved)
Using codes from marco (excel) i have use codes like in vb like :
worksheet.Range("A5:P9").Select
Selection.Copy
..
but i got the above error msg at the selection.copy ..and worst of all.sometimes it works sometimes it does not..( ie.hit the error)..what happen?
Regard
Goh
Last edited by Goh; Sep 23rd, 2005 at 01:37 AM.
-
Sep 22nd, 2005, 11:32 PM
#2
Re: Object variable or with set variable not set.
I have this example of creating a text file of an Excel sheet.
VB Code:
Option Explicit
' Set a reference to M$ Excel xx.0 Object Library
Private Sub Form_Load()
Dim objExcel As Object
Dim objWorkbook As Object
Dim objSheet As Excel.Worksheet
Dim ff As Integer, r As Integer, c As Integer
Dim txt As String
Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open(App.Path & "\book1.xls")
Set objSheet = objExcel.Worksheets.Item(1)
ff = FreeFile
Open App.Path & "\Grid.txt" For Output As #ff
For r = 1 To objSheet.Cells.SpecialCells(xlCellTypeLastCell).Row
txt = ""
For c = 1 To objSheet.Cells.SpecialCells(xlCellTypeLastCell).Column
If objSheet.Cells(r, c).Value <> "" Then
txt = txt & objSheet.Cells(r, c).Value & ", "
End If
Next c
If Len(txt) > 0 Then txt = Left(txt, Len(txt) - 2) ' remove last ", "
Print #ff, txt
Next r
Close #ff
Set objSheet = Nothing
objWorkbook.Close
Set objWorkbook = Nothing
objExcel.Quit
Set objExcel = Nothing
Unload Me
End Sub
-
Sep 22nd, 2005, 11:46 PM
#3
Re: Object variable or with set variable not set.
You need to post more of your code so we can see what is really going on. You state your using vb, but is it vb6 or vb.net, etc.?
@dglienna, whay are you using Object as a variable type when you have set a reference to excel? Your early binding AND late binding in the same project. No need, just do one or the other. 
Moved from Classic VB forum
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Sep 22nd, 2005, 11:56 PM
#4
Thread Starter
Fanatic Member
Re: Object variable or with set variable not set.
I am using this version of coding:
..
..
Set xlApp = New Excel.Application
Set xlBook = xlApp.Workbooks.Add
Set xlsheet = xlApp.ActiveSheet
..
is there a difference between the one you have given and this? I mean which is a better one? Whats the difference?Sorry to ask this as what i am doing now is amending codes that have been written in the past. Thus careful consideration has to be done if there is a need to use the 'object'..thanks
Regards
Alfred
-
Sep 22nd, 2005, 11:58 PM
#5
Re: Object variable or with set variable not set.
Can you post the code where you posted
???
worksheet.Range("A5:P9").Select
Selection.Copy
???
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Sep 23rd, 2005, 12:00 AM
#6
Re: Object variable or with set variable not set.
Hmmm. How would you write it? I pulled that from somewhere, and modified it slightly.
-
Sep 23rd, 2005, 12:05 AM
#7
Re: Object variable or with set variable not set.
@Dave:
Just like post #4.
VB Code:
Dim objExcel As Exce.Application
Dim objWorkbook As Excel.Workbook
Dim objSheet As Excel.Worksheet
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Sep 23rd, 2005, 12:12 AM
#8
Thread Starter
Fanatic Member
Re: Object variable or with set variable not set.
VB Code:
Function LoadListInExcel() As Boolean
LoadListInExcel= True
Set rstLoadRecord = New ADODB.Recordset
Set xlApp = New Excel.Application
Set xlBook = xlApp.Workbooks.Add
Set xlsheet = xlApp.ActiveSheet
.
.
.
Call setBorder(xlsheet)
Call setColTitleWidth(xlsheet)
End function
Private Sub setBorder(worksheet As Excel.worksheet)
With worksheet
.cell...
.range...
end with
End Function
Private Sub setColTitleWidth(worksheet As Excel.worksheet)
If lstr <> Trim$(rstLoadRecord![Ready]) Then
'cut and paste heading
.Range("A5:P9").Select
[COLOR=Sienna]Selection.Copy[/COLOR]
NextRows = CStr(NextRows + 5)
lstrCellCopy = "A" & NextRows
.Range(lstrCellCopy).Select
ActiveSheet.Paste
NextRows = CStr(NextRows + 5)
End if
End Function
...sorry that i miss out those DIM .. ..Does it got smthing to do with instance..?
Last edited by RobDog888; Sep 23rd, 2005 at 12:20 AM.
Reason: Added vbcode tags
-
Sep 23rd, 2005, 12:15 AM
#9
Re: Object variable or with set variable not set.
-
Sep 23rd, 2005, 12:23 AM
#10
Re: Object variable or with set variable not set.
No prob. Anytime Dave 
Goh, you have a block of code here that is not correct.
VB Code:
If lstr <> Trim$(rstLoadRecord![Ready]) Then
'cut and paste heading
.Range("A5:P9").Select '<---WHERE IS THE WITH BLOCK?
Selection.Copy
NextRows = CStr(NextRows + 5)
lstrCellCopy = "A" & NextRows
.Range(lstrCellCopy).Select '<---WHERE IS THE WITH BLOCK?
ActiveSheet.Paste
NextRows = CStr(NextRows + 5)
End if
And this other procedure you have multiple dots???
VB Code:
Private Sub setBorder(worksheet As Excel.worksheet)
With worksheet
.cell... '<-- WHATS UP WITH THE "..." ?
.range...
end with
End Function
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Sep 23rd, 2005, 01:36 AM
#11
Thread Starter
Fanatic Member
Re: Object variable or with set variable not set. (Resolved)
Hi thanks for the reply..so that i have given incomplete codes...but some how it seems there is no problem now..thank you to all will reflect if any more problems.
Regards
Alfred
-
Sep 25th, 2005, 07:37 PM
#12
Thread Starter
Fanatic Member
Re: Object variable or with set variable not set. (resolved)
hi guys i still got errors at times thanks for any suggestion.
Function LoadListInExcel() As Boolean
LoadListInExcel= True
Set rstLoadRecord = New ADODB.Recordset
Set xlApp = New Excel.Application
Set xlBook = xlApp.Workbooks.Add
Set xlsheet = xlApp.ActiveSheet
'==================
' codes
'==================
Call setBorder(xlsheet)
Call setColTitleWidth(xlsheet)
End function
Private Sub setBorder(worksheet As Excel.worksheet)
With worksheet
' .cell ' sorry i did not complete this part of the codes as it works fine here.
' .range ' sorry i did not complete this part of the codes as it works fine here.
'==================
' codes
'==================
End with
End Function
Private Sub setColTitleWidth(worksheet As Excel.worksheet)
With worksheet
If lstr <> Trim$(rstLoadRecord![Ready]) Then
'cut and paste heading
.Range("A5:P9").Select
Selection.Copy ' this is the part the error ocurs.
NextRows = CStr(NextRows + 5)
lstrCellCopy = "A" & NextRows
.Range(lstrCellCopy).Select
ActiveSheet.Paste
NextRows = CStr(NextRows + 5)
End if
End With
End Function
-
Sep 25th, 2005, 09:28 PM
#13
Thread Starter
Fanatic Member
Re: Object variable or with set variable not set. (resolved)
Hi everyone I just found out that the error occurs only when the codes rerun the second time ie consecutively. the first time is ok. Only the second time the error hits. Is there something i need to close or set something as new ? thanks
-
Sep 26th, 2005, 01:43 AM
#14
Thread Starter
Fanatic Member
Re: Object variable or with set variable not set. (resolved)
mm..for the "selection.copy" how do i amend to avoid the error ?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|