VBA - Runtime Error 4605. This method or property is not available because a document window is not active
I'm trying to use VBA (in Word) to create an appointment in Outlook based on the data provided in the user form that is loaded when the word document containing the macro is opened. Everything seems to be working alright but I end up having some run time error when the macro executes the paste function.
Dim OutApp As Object
Dim OutMail As Object
Dim OutInsp As Outlook.Inspector
Dim WdApp As Word.Application
Dim OutDoc As Word.Document
Dim WdSel As Word.Selection
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(1)
If request_Form.ComboBox3.Value = "****" Then
With ActiveDocument.Bookmarks("recipient_Name")
.Range.Text = "Uwe"
End With
With ActiveDocument.Bookmarks("applicant_Name")
.Range.Text = Replace(request_Form.TextBox1.Value, "_", ", ")
End With
With OutMail
.MeetingStatus = olMeeting
.RequiredAttendees = "@live.in"
.Subject = request_Form.TextBox1.Value & " Work From Home"
.Location = "Home"
.BusyStatus = olFree
.ReminderSet = False
If request_Form.CheckBox1.Value = False Then
.Start = DateValue(request_Form.TextBox2.Value) + TimeValue(request_Form.ComboBox1.Value)
.End = DateValue(request_Form.TextBox2.Value) + TimeValue(request_Form.ComboBox2.Value)
Else
.AllDayEvent = True
End If
.Display
End With
Set OutInsp = OutMail.GetInspector
Set OutDoc = OutInsp.WordEditor
Set WdApp = OutDoc.Application
Set WdSel = WdApp.Windows(1).Selection
Documents("Home Office Request Form.docm").Range.Copy
With OutMail
WdSel.PasteAndFormat Type:=wdFormatOriginalFormatting
End With
save_file
MsgBox "A copy of this file has been stored in your desktop for your
reference.", vbOKOnly + vbInformation, "Status Information"
End Sub
"Macro Function - The program creates an appointment based on the data filled in the userform and copies the entire content in the word document and pastes in the body of the appointment". When this piece of code "wdsel.pasteandformat" is executed it results in run time error but interestingly this error doesn't seem to happen quite often so its hard for me to understand what going wrong here Lets says 70% of the time it works good and probably for the rest 30% of the time it doesn't.
I would be really glad if someone could help me understand whats going wrong with the above code.
Thanks for your help.
Regards,
Rahul
vba outlook ms-word word-vba
add a comment |
I'm trying to use VBA (in Word) to create an appointment in Outlook based on the data provided in the user form that is loaded when the word document containing the macro is opened. Everything seems to be working alright but I end up having some run time error when the macro executes the paste function.
Dim OutApp As Object
Dim OutMail As Object
Dim OutInsp As Outlook.Inspector
Dim WdApp As Word.Application
Dim OutDoc As Word.Document
Dim WdSel As Word.Selection
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(1)
If request_Form.ComboBox3.Value = "****" Then
With ActiveDocument.Bookmarks("recipient_Name")
.Range.Text = "Uwe"
End With
With ActiveDocument.Bookmarks("applicant_Name")
.Range.Text = Replace(request_Form.TextBox1.Value, "_", ", ")
End With
With OutMail
.MeetingStatus = olMeeting
.RequiredAttendees = "@live.in"
.Subject = request_Form.TextBox1.Value & " Work From Home"
.Location = "Home"
.BusyStatus = olFree
.ReminderSet = False
If request_Form.CheckBox1.Value = False Then
.Start = DateValue(request_Form.TextBox2.Value) + TimeValue(request_Form.ComboBox1.Value)
.End = DateValue(request_Form.TextBox2.Value) + TimeValue(request_Form.ComboBox2.Value)
Else
.AllDayEvent = True
End If
.Display
End With
Set OutInsp = OutMail.GetInspector
Set OutDoc = OutInsp.WordEditor
Set WdApp = OutDoc.Application
Set WdSel = WdApp.Windows(1).Selection
Documents("Home Office Request Form.docm").Range.Copy
With OutMail
WdSel.PasteAndFormat Type:=wdFormatOriginalFormatting
End With
save_file
MsgBox "A copy of this file has been stored in your desktop for your
reference.", vbOKOnly + vbInformation, "Status Information"
End Sub
"Macro Function - The program creates an appointment based on the data filled in the userform and copies the entire content in the word document and pastes in the body of the appointment". When this piece of code "wdsel.pasteandformat" is executed it results in run time error but interestingly this error doesn't seem to happen quite often so its hard for me to understand what going wrong here Lets says 70% of the time it works good and probably for the rest 30% of the time it doesn't.
I would be really glad if someone could help me understand whats going wrong with the above code.
Thanks for your help.
Regards,
Rahul
vba outlook ms-word word-vba
Chances are that when you open Outlook it's the Outlook window which becomes active. At that moment access to the selection in the non-active Word window might become inaccessible. To cure the problem, take everything you want from Word before opening Outlook. If that doesn't cure the problem avoid the Selection object entirely as it may not be accessible itself once its window lost focus. Copy the text into memory and use that to paste into Outlook.
– Variatus
Jan 3 at 7:49
@Variatus Thank you so much for your comments. Unfortunately, your first suggestion didn't work out or I'm not sure If I executed that properly. Anyway your second suggestion worked like a charm. I removed the selection object completely and used the data copied into clipboard to paste into Outlook. Thank you so much :)
– Arjun
Jan 3 at 8:49
add a comment |
I'm trying to use VBA (in Word) to create an appointment in Outlook based on the data provided in the user form that is loaded when the word document containing the macro is opened. Everything seems to be working alright but I end up having some run time error when the macro executes the paste function.
Dim OutApp As Object
Dim OutMail As Object
Dim OutInsp As Outlook.Inspector
Dim WdApp As Word.Application
Dim OutDoc As Word.Document
Dim WdSel As Word.Selection
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(1)
If request_Form.ComboBox3.Value = "****" Then
With ActiveDocument.Bookmarks("recipient_Name")
.Range.Text = "Uwe"
End With
With ActiveDocument.Bookmarks("applicant_Name")
.Range.Text = Replace(request_Form.TextBox1.Value, "_", ", ")
End With
With OutMail
.MeetingStatus = olMeeting
.RequiredAttendees = "@live.in"
.Subject = request_Form.TextBox1.Value & " Work From Home"
.Location = "Home"
.BusyStatus = olFree
.ReminderSet = False
If request_Form.CheckBox1.Value = False Then
.Start = DateValue(request_Form.TextBox2.Value) + TimeValue(request_Form.ComboBox1.Value)
.End = DateValue(request_Form.TextBox2.Value) + TimeValue(request_Form.ComboBox2.Value)
Else
.AllDayEvent = True
End If
.Display
End With
Set OutInsp = OutMail.GetInspector
Set OutDoc = OutInsp.WordEditor
Set WdApp = OutDoc.Application
Set WdSel = WdApp.Windows(1).Selection
Documents("Home Office Request Form.docm").Range.Copy
With OutMail
WdSel.PasteAndFormat Type:=wdFormatOriginalFormatting
End With
save_file
MsgBox "A copy of this file has been stored in your desktop for your
reference.", vbOKOnly + vbInformation, "Status Information"
End Sub
"Macro Function - The program creates an appointment based on the data filled in the userform and copies the entire content in the word document and pastes in the body of the appointment". When this piece of code "wdsel.pasteandformat" is executed it results in run time error but interestingly this error doesn't seem to happen quite often so its hard for me to understand what going wrong here Lets says 70% of the time it works good and probably for the rest 30% of the time it doesn't.
I would be really glad if someone could help me understand whats going wrong with the above code.
Thanks for your help.
Regards,
Rahul
vba outlook ms-word word-vba
I'm trying to use VBA (in Word) to create an appointment in Outlook based on the data provided in the user form that is loaded when the word document containing the macro is opened. Everything seems to be working alright but I end up having some run time error when the macro executes the paste function.
Dim OutApp As Object
Dim OutMail As Object
Dim OutInsp As Outlook.Inspector
Dim WdApp As Word.Application
Dim OutDoc As Word.Document
Dim WdSel As Word.Selection
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(1)
If request_Form.ComboBox3.Value = "****" Then
With ActiveDocument.Bookmarks("recipient_Name")
.Range.Text = "Uwe"
End With
With ActiveDocument.Bookmarks("applicant_Name")
.Range.Text = Replace(request_Form.TextBox1.Value, "_", ", ")
End With
With OutMail
.MeetingStatus = olMeeting
.RequiredAttendees = "@live.in"
.Subject = request_Form.TextBox1.Value & " Work From Home"
.Location = "Home"
.BusyStatus = olFree
.ReminderSet = False
If request_Form.CheckBox1.Value = False Then
.Start = DateValue(request_Form.TextBox2.Value) + TimeValue(request_Form.ComboBox1.Value)
.End = DateValue(request_Form.TextBox2.Value) + TimeValue(request_Form.ComboBox2.Value)
Else
.AllDayEvent = True
End If
.Display
End With
Set OutInsp = OutMail.GetInspector
Set OutDoc = OutInsp.WordEditor
Set WdApp = OutDoc.Application
Set WdSel = WdApp.Windows(1).Selection
Documents("Home Office Request Form.docm").Range.Copy
With OutMail
WdSel.PasteAndFormat Type:=wdFormatOriginalFormatting
End With
save_file
MsgBox "A copy of this file has been stored in your desktop for your
reference.", vbOKOnly + vbInformation, "Status Information"
End Sub
"Macro Function - The program creates an appointment based on the data filled in the userform and copies the entire content in the word document and pastes in the body of the appointment". When this piece of code "wdsel.pasteandformat" is executed it results in run time error but interestingly this error doesn't seem to happen quite often so its hard for me to understand what going wrong here Lets says 70% of the time it works good and probably for the rest 30% of the time it doesn't.
I would be really glad if someone could help me understand whats going wrong with the above code.
Thanks for your help.
Regards,
Rahul
vba outlook ms-word word-vba
vba outlook ms-word word-vba
asked Jan 3 at 3:52
ArjunArjun
555
555
Chances are that when you open Outlook it's the Outlook window which becomes active. At that moment access to the selection in the non-active Word window might become inaccessible. To cure the problem, take everything you want from Word before opening Outlook. If that doesn't cure the problem avoid the Selection object entirely as it may not be accessible itself once its window lost focus. Copy the text into memory and use that to paste into Outlook.
– Variatus
Jan 3 at 7:49
@Variatus Thank you so much for your comments. Unfortunately, your first suggestion didn't work out or I'm not sure If I executed that properly. Anyway your second suggestion worked like a charm. I removed the selection object completely and used the data copied into clipboard to paste into Outlook. Thank you so much :)
– Arjun
Jan 3 at 8:49
add a comment |
Chances are that when you open Outlook it's the Outlook window which becomes active. At that moment access to the selection in the non-active Word window might become inaccessible. To cure the problem, take everything you want from Word before opening Outlook. If that doesn't cure the problem avoid the Selection object entirely as it may not be accessible itself once its window lost focus. Copy the text into memory and use that to paste into Outlook.
– Variatus
Jan 3 at 7:49
@Variatus Thank you so much for your comments. Unfortunately, your first suggestion didn't work out or I'm not sure If I executed that properly. Anyway your second suggestion worked like a charm. I removed the selection object completely and used the data copied into clipboard to paste into Outlook. Thank you so much :)
– Arjun
Jan 3 at 8:49
Chances are that when you open Outlook it's the Outlook window which becomes active. At that moment access to the selection in the non-active Word window might become inaccessible. To cure the problem, take everything you want from Word before opening Outlook. If that doesn't cure the problem avoid the Selection object entirely as it may not be accessible itself once its window lost focus. Copy the text into memory and use that to paste into Outlook.
– Variatus
Jan 3 at 7:49
Chances are that when you open Outlook it's the Outlook window which becomes active. At that moment access to the selection in the non-active Word window might become inaccessible. To cure the problem, take everything you want from Word before opening Outlook. If that doesn't cure the problem avoid the Selection object entirely as it may not be accessible itself once its window lost focus. Copy the text into memory and use that to paste into Outlook.
– Variatus
Jan 3 at 7:49
@Variatus Thank you so much for your comments. Unfortunately, your first suggestion didn't work out or I'm not sure If I executed that properly. Anyway your second suggestion worked like a charm. I removed the selection object completely and used the data copied into clipboard to paste into Outlook. Thank you so much :)
– Arjun
Jan 3 at 8:49
@Variatus Thank you so much for your comments. Unfortunately, your first suggestion didn't work out or I'm not sure If I executed that properly. Anyway your second suggestion worked like a charm. I removed the selection object completely and used the data copied into clipboard to paste into Outlook. Thank you so much :)
– Arjun
Jan 3 at 8:49
add a comment |
1 Answer
1
active
oldest
votes
So, we have an answer to your problem. Here it is.
Chances are that when you open Outlook it's the Outlook window which becomes active. At that moment access to the selection object in the non-active Word window might become inaccessible. To cure the problem, take everything you want from Word before opening Outlook. That includes copying the text into memory and use that to paste into Outlook.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54016127%2fvba-runtime-error-4605-this-method-or-property-is-not-available-because-a-doc%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
So, we have an answer to your problem. Here it is.
Chances are that when you open Outlook it's the Outlook window which becomes active. At that moment access to the selection object in the non-active Word window might become inaccessible. To cure the problem, take everything you want from Word before opening Outlook. That includes copying the text into memory and use that to paste into Outlook.
add a comment |
So, we have an answer to your problem. Here it is.
Chances are that when you open Outlook it's the Outlook window which becomes active. At that moment access to the selection object in the non-active Word window might become inaccessible. To cure the problem, take everything you want from Word before opening Outlook. That includes copying the text into memory and use that to paste into Outlook.
add a comment |
So, we have an answer to your problem. Here it is.
Chances are that when you open Outlook it's the Outlook window which becomes active. At that moment access to the selection object in the non-active Word window might become inaccessible. To cure the problem, take everything you want from Word before opening Outlook. That includes copying the text into memory and use that to paste into Outlook.
So, we have an answer to your problem. Here it is.
Chances are that when you open Outlook it's the Outlook window which becomes active. At that moment access to the selection object in the non-active Word window might become inaccessible. To cure the problem, take everything you want from Word before opening Outlook. That includes copying the text into memory and use that to paste into Outlook.
answered Jan 3 at 10:01
VariatusVariatus
5,7251524
5,7251524
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54016127%2fvba-runtime-error-4605-this-method-or-property-is-not-available-because-a-doc%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Chances are that when you open Outlook it's the Outlook window which becomes active. At that moment access to the selection in the non-active Word window might become inaccessible. To cure the problem, take everything you want from Word before opening Outlook. If that doesn't cure the problem avoid the Selection object entirely as it may not be accessible itself once its window lost focus. Copy the text into memory and use that to paste into Outlook.
– Variatus
Jan 3 at 7:49
@Variatus Thank you so much for your comments. Unfortunately, your first suggestion didn't work out or I'm not sure If I executed that properly. Anyway your second suggestion worked like a charm. I removed the selection object completely and used the data copied into clipboard to paste into Outlook. Thank you so much :)
– Arjun
Jan 3 at 8:49