![]() |
Manipulating Word Range object in Excel
I am juggling a couple of Microsoft application objects from within Excel VBA
Add-In. Basically, I am creating an draft email using an Outlook object and using Word object as editor (in Outlook) - all with Office 2003. All working fine on a couple of machines but on one machine (same build as others) the code is failing during a Find/Replace in the Word Range object. It is actually causing Excel to crash with a 'Send to Microsoft' memory stack error. The code is roughly :- Dim WdDoc as Word.Document Dim WdRg as Word.Range Dim objEmail As Object Dim objDrafts As Object Set objOutlook = New Outlook.Application Set objDrafts = objOutlook.Session.GetDefaultFolder(olFolderDrafts ) Set objEmail = objDrafts.Items.Add Set wdDoc = objEmail.GetInspector.WordEditor Set WdRg = WdDoc.Range(2,100) ' assume text fred somewhere in this range WdRg.Find.Text = "fred" WdRg.Replacement.Text = "bert" WdRg.Find.Execute Any thoughts why the code should fail (big time, taking out Excel) just on one machine but works OK on the other clone builds? A User option or Add-In conflict? Thanks, Andy. |
Manipulating Word Range object in Excel
Hi, Andy. A few thoughts:
(1) Step through the code (F8) while watching your Locals. Is it the Find/Replace that's not working? Or is it possible you don't have an object getting set - either WdRg or WdDoc? (2) Would it be easier all the way around to simply compose your email in Word outside of Outlook, then open an Outlook object and copy/ paste your email text? This would avoid any "Word as Email Editor" problems. (3) Your Find and Replace code doesn't quite look like what is shown in the Word VBA Help for the Find and Replace objects. I would want to do something more like: WdRg.Find.Execute FindText:="fred", ReplaceWith:="bert", _ Replace:=wdReplaceAll Ed On Sep 5, 6:02 am, Andy wrote: I am juggling a couple of Microsoft application objects from within Excel VBA Add-In. Basically, I am creating an draft email using an Outlook object and using Word object as editor (in Outlook) - all with Office 2003. All working fine on a couple of machines but on one machine (same build as others) the code is failing during a Find/Replace in the Word Range object. It is actually causing Excel to crash with a 'Send to Microsoft' memory stack error. The code is roughly :- Dim WdDoc as Word.Document Dim WdRg as Word.Range Dim objEmail As Object Dim objDrafts As Object Set objOutlook = New Outlook.Application Set objDrafts = objOutlook.Session.GetDefaultFolder(olFolderDrafts ) Set objEmail = objDrafts.Items.Add Set wdDoc = objEmail.GetInspector.WordEditor Set WdRg = WdDoc.Range(2,100) ' assume text fred somewhere in this range WdRg.Find.Text = "fred" WdRg.Replacement.Text = "bert" WdRg.Find.Execute Any thoughts why the code should fail (big time, taking out Excel) just on one machine but works OK on the other clone builds? A User option or Add-In conflict? Thanks, Andy. |
Manipulating Word Range object in Excel
Thanks Ed - I'll take a look.
(2) Could have used this approach but need to insert some dynamic data from excel (3) I'll take another look at the VBA but it works fine every time on my machine and two others but not on one particular guy's machine which has the same 2003 build. Regards, Andy. "Ed from AZ" wrote: Hi, Andy. A few thoughts: (1) Step through the code (F8) while watching your Locals. Is it the Find/Replace that's not working? Or is it possible you don't have an object getting set - either WdRg or WdDoc? (2) Would it be easier all the way around to simply compose your email in Word outside of Outlook, then open an Outlook object and copy/ paste your email text? This would avoid any "Word as Email Editor" problems. (3) Your Find and Replace code doesn't quite look like what is shown in the Word VBA Help for the Find and Replace objects. I would want to do something more like: WdRg.Find.Execute FindText:="fred", ReplaceWith:="bert", _ Replace:=wdReplaceAll Ed On Sep 5, 6:02 am, Andy wrote: I am juggling a couple of Microsoft application objects from within Excel VBA Add-In. Basically, I am creating an draft email using an Outlook object and using Word object as editor (in Outlook) - all with Office 2003. All working fine on a couple of machines but on one machine (same build as others) the code is failing during a Find/Replace in the Word Range object. It is actually causing Excel to crash with a 'Send to Microsoft' memory stack error. The code is roughly :- Dim WdDoc as Word.Document Dim WdRg as Word.Range Dim objEmail As Object Dim objDrafts As Object Set objOutlook = New Outlook.Application Set objDrafts = objOutlook.Session.GetDefaultFolder(olFolderDrafts ) Set objEmail = objDrafts.Items.Add Set wdDoc = objEmail.GetInspector.WordEditor Set WdRg = WdDoc.Range(2,100) ' assume text fred somewhere in this range WdRg.Find.Text = "fred" WdRg.Replacement.Text = "bert" WdRg.Find.Execute Any thoughts why the code should fail (big time, taking out Excel) just on one machine but works OK on the other clone builds? A User option or Add-In conflict? Thanks, Andy. |
Manipulating Word Range object in Excel
(3) I'll take another look at the VBA but it works fine every time on my
machine and two others but not on one particular guy's machine which has the same 2003 build. I had the same issue with one person in my office. Come to find out, this person is one who knows computers and is really into setting her own preferences. We all started with the same image, but preferences can make a big difference. I'm not sure, but I think the Word object you're getting is still subject to the Outlook object, since you're opening the email editor of an Outlook object. You might be getting some interactions due to individual preference settings. Escpecially when you try to automate all this from Excel VBA!! You might also try to ask about this in the microsoft.public.outlook.program_vba NG. Good luck! Ed On Sep 5, 9:42 am, Andy wrote: Thanks Ed - I'll take a look. (2) Could have used this approach but need to insert some dynamic data from excel (3) I'll take another look at the VBA but it works fine every time on my machine and two others but not on one particular guy's machine which has the same 2003 build. Regards, Andy. "Ed from AZ" wrote: Hi, Andy. A few thoughts: (1) Step through the code (F8) while watching your Locals. Is it the Find/Replace that's not working? Or is it possible you don't have an object getting set - either WdRg or WdDoc? (2) Would it be easier all the way around to simply compose your email in Word outside of Outlook, then open an Outlook object and copy/ paste your email text? This would avoid any "Word as Email Editor" problems. (3) Your Find and Replace code doesn't quite look like what is shown in the Word VBA Help for the Find and Replace objects. I would want to do something more like: WdRg.Find.Execute FindText:="fred", ReplaceWith:="bert", _ Replace:=wdReplaceAll Ed On Sep 5, 6:02 am, Andy wrote: I am juggling a couple of Microsoft application objects from within Excel VBA Add-In. Basically, I am creating an draft email using an Outlook object and using Word object as editor (in Outlook) - all with Office 2003. All working fine on a couple of machines but on one machine (same build as others) the code is failing during a Find/Replace in the Word Range object. It is actually causing Excel to crash with a 'Send to Microsoft' memory stack error. The code is roughly :- Dim WdDoc as Word.Document Dim WdRg as Word.Range Dim objEmail As Object Dim objDrafts As Object Set objOutlook = New Outlook.Application Set objDrafts = objOutlook.Session.GetDefaultFolder(olFolderDrafts ) Set objEmail = objDrafts.Items.Add Set wdDoc = objEmail.GetInspector.WordEditor Set WdRg = WdDoc.Range(2,100) ' assume text fred somewhere in this range WdRg.Find.Text = "fred" WdRg.Replacement.Text = "bert" WdRg.Find.Execute Any thoughts why the code should fail (big time, taking out Excel) just on one machine but works OK on the other clone builds? A User option or Add-In conflict? Thanks, Andy.- Hide quoted text - - Show quoted text - |
All times are GMT +1. The time now is 02:27 PM. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com