Outlook WordEditor Range.Paste overwrites previous paste
23 views (last 30 days)
Show older comments
I posted this on stackoverflow as well, not sure if that's against the rules or in bad form.
What I'm trying to do:
- From Matlab, open a new email (Outlook 2010)
- Create a chart and copy it to clipboard
- Paste that image to the email
- Repeat steps 2 and 3 a couple more times to paste additional charts
Problem: Pastes after the first simply overwrite the existing image. I have tries this with text as well, getting the same overwrite.
What I think I need: a way to advance the "cursor" or set the range or selection before each paste such that it excludes the existing image/text (essentially goes to the end of the document).
Code:
Get current running instance of Outlook, create a new email and get its WordEditor:
h = actxGetRunningServer('outlook.application');
mail = h.CreateItem('olMail');
mail.To = 'someone@somewhere.com';
mail.Subject = 'subject line text';
mail.BodyFormat = 'olFormatHTML';
word_editor = mail.GetInspector.WordEditor;
mail.Display
Open Matlab figure, copy to clipboard, close:
F = open('fig.fig');
print(F,'-clipboard','-dmeta')
close(F)
Paste clipboard:
word_editor.Range.Paste
Up to this point everything works. Any additional copy/pastes after this simply overwrite the previous paste.
I have tried things like adding paragraphs, attempting to get the current range to maybe find a way to change it but no luck.
Any assistance will be greatly appreciated.
4 Comments
Walter Roberson
on 24 Jan 2017
It looks to me as if you should use the Collapse method, specifying wdCollapseEnd, which would position you just after the end of the range. Then Paste would paste after the range. If you then collapsed again you should be ready to paste again.
Accepted Answer
Guillaume
on 25 Jan 2017
Edited: Guillaume
on 25 Jan 2017
As far as I know, matlab does not understand named constants. You have to use their numerical equivalent which unfortunately is rarely linked from the method documentation on MSDN. A direct search for the enumeration name should get you there though or failing that, the object browser in the VBA editor of any office program.
wdCollapseEnd 0
wdCollapseStart 1
So,
word_editor.Range.Collapse(0);
will get you there. It certainly worked for me when I tested pasting text in an email.
4 Comments
Guillaume
on 25 Jan 2017
Hum, indeed it did not work when I tried now. Looking back on what I tested originally, the only difference is that I stored the range in a variable. For some reason, it appears to make a lot of difference:
Does not work:
h = actxGetRunningServer('outlook.application');
mail = h.CreateItem('olMail');
mail.To = 'someone@somewhere.com';
mail.Subject = 'subject line text';
mail.BodyFormat = 'olFormatHTML';
word_editor = mail.GetInspector.WordEditor;
mail.Display
imshow('img.png');
print(gcf, '-clipboard', '-dmeta');
for p = 1:4
word_editor.Range.Collapse(0);
word_editor.Range.Paste;
end
Works
h = actxGetRunningServer('outlook.application');
mail = h.CreateItem('olMail');
mail.To = 'someone@somewhere.com';
mail.Subject = 'subject line text';
mail.BodyFormat = 'olFormatHTML';
word_editor = mail.GetInspector.WordEditor;
mail.Display
imshow('img.png');
print(gcf, '-clipboard', '-dmeta');
rg = word_editor.Range
for p = 1:4
rg.Collapse(0);
rg.Paste;
end
I have no clue why, since they're references, the objects should be exactly the same.
More Answers (0)
See Also
Categories
Find more on Environment and Settings in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!