|
-
Oct 26th, 2010, 05:00 AM
#1
Thread Starter
Addicted Member
[RESOLVED] For loop problem
This code works up to the start of the second iteration where SelectedArray(i) = 1 (this array can only take the values 0 or 1), then it crashes. How do I fix it?
Code:
For i = 1 To big_deep
If SelectedArray(i) = 1 Then
Set wrdApp = CreateObject("Word.Application")
wrdApp.Visible = True
Set wrdDoc = wrdApp.Documents.Open("C:\My Documents\Geek Stuff\Document for Office Holder.doc")
wrdDoc.Activate
With wrdDoc
' code that defines position_name & other stuff
ActiveDocument.Bookmarks("offr_name").Range.InsertBefore position_name
' more code to populate document here
If Dir(ActiveWorkbook.Path & "\" & position_name & ".doc") <> "" Then
Kill ActiveWorkbook.Path & "\" & position_name & ".doc"
End If
.SaveAs (ActiveWorkbook.Path & "\" & Left(position_name, 20) & ".doc")
.Close ' close the document
End With
wrdApp.Documents.Open ("C:\My Documents\Geek Stuff\Document for Office Holder.doc")
For Each wrdDoc In Documents
wrdDoc.Close SaveChanges:=wdSaveChanges
Next wrdDoc
' wrdApp.Close
wrdApp.Quit
oXLSheet.Cells(i + 2, big_wide + 2).Select
oXLSheet.Cells(i + 2, big_wide + 2).Value = Format(Date, "d-mmm-yy")
MsgBox ("Document for " & position_name & " created")
' _________________________________________ end of creation of new doc
End If
ActiveWorkbook.Save
Next i
End Sub
The problem is that this all works fine for the first value of i for which SelectedArray(i) = 1 [members of SelectedArray can only take the values 0 or 1, this links up with the checkboxes the user has selected in the userform] but if the user selects two (or more) checkboxes, the routine crashes on its second pass.
The code that the routine doesn't like is the line:
Code:
ActiveDocument.Bookmarks("offr_name").Range.InsertBefore position_name
and the problem it claims it has is that there is no word document open, although in fact there is. (I inserted the message box down towards the end and also the save command to let the routine catch up with itself, but that doesn't fix the problem.)
(This code is being run from an excel file, hence the need to open an instance of Word.)
Thanks
-
Oct 26th, 2010, 06:13 AM
#2
Re: For loop problem
as you have already set a document object, you should avoid using activedocument
i would avoid opening and closing word within a loop, once opened, leave open until loop finished
you can try doevents after each word operation, to allow other processes to complete
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Oct 26th, 2010, 06:44 AM
#3
Thread Starter
Addicted Member
Re: For loop problem
OOPS! PLEASE IGNORE THIS. I POSTED IT IN THE WRONG PLACE. IT BELONGS WITH http://www.vbforums.com/showthread.p...2&goto=newpost. (I blame my mouse, think I'm going to have to get one that works reliably.) My apologies.
Strange. The code picks up the first cell as an error, despite the fact that the entry in this cell looks like "[email protected]" (This code is part of a larger routine that checks for data integrity, and similar coding works fine for the other columns.)
Last edited by Resource Dragon; Oct 27th, 2010 at 06:05 AM.
Reason: Posted in the wrong thread.
-
Oct 26th, 2010, 03:28 PM
#4
Re: For loop problem
Did you incorporate the suggestions Pete gave?
A good exercise for the Heart is to bend down and help another up...
Please Mark your Thread " Resolved", if the query is solved
MyGear:
★ CPU ★ Ryzen 5 5800X
★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
★ Keyboard ★ TVS Electronics Gold Keyboard
★ Mouse ★ Logitech G502 Hero
-
Oct 26th, 2010, 03:30 PM
#5
Re: For loop problem
you need to test the value of the cell as excel may have automatically converted an email address to a hyperlink
vb Code:
msgbox cells(x, y) msgbox cells(x, y).text msgbox cells(x, y).hyperlinks(1).texttodisplay
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Oct 27th, 2010, 06:02 AM
#6
Thread Starter
Addicted Member
Re: For loop problem
Unfortunately I haven't had time to look at Pete's suggestions yet but I will do so, I ended up busy with some other stuff at work today. I can't search VB Forums at work & I can't log into VB Forums there, either. I can however open up a link to see a particular discussion thread, so I e-mail the links to myself for reading at work when the problem is work-related.
-
Nov 10th, 2010, 07:57 AM
#7
Thread Starter
Addicted Member
Re: For loop problem
I am going to mark this thread as resolved, as I suspect that Pete's suggestions (post no 2 in this thread) will solve the problem. (The project fell by the wayside - one of those ones where someon higher up the foodchain said that further instructions in regard to what the documents are meant to do would be coming out at some stage.)
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
|