Outlook WordEditor Range.Paste overwrites previous paste

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:
  1. From Matlab, open a new email (Outlook 2010)
  2. Create a chart and copy it to clipboard
  3. Paste that image to the email
  4. 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 commentaires

I would expect this behavior. The range does not automatically change after a paste, since you might want to make other changes to what was pasted such as formatting or color changes.
Thank you Walter!
Would you happen to know the syntax for changing the range?
Things like this don't seem to work:
word_editor.Range.Start = 3;
% or
set(word_editor.Range,'Start',3);
There is some documentation here that should be relevant, but I can't figure it out.
Being able to set the range is all I need.
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.
Thanks again for taking the time to help, Walter. This method looks like it's exactly what I need. I'm working on figuring out the correct syntax to use in Matlab.
None of the below works, but I will keep testing/researching
set(word_editor.Range.Collapse,'wdCollapseEnd')
set(word_editor.Range,'Collapse','wdCollapseEnd')
word_editor.Range.Collapse('wdCollapseEnd')
I will update this thread with a solution when I find it.

Connectez-vous pour commenter.

 Réponse acceptée

Guillaume
Guillaume le 25 Jan 2017
Modifié(e) : Guillaume le 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.
Anyway, the two values for the WdCollapseDirection enumeration are:
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 commentaires

Thank you Guillaume! No luck immediately, but I will take a more careful look later today.
No luck with Range.Collapse but I found a roundabout way to pass in the start and end positions before the Paste method.
% create and display new email
h = actxGetRunningServer('outlook.application');
mail = h.CreateItem('olMail');
mail.To = 'someone@somewhere.com';
mail.Subject = 'subject line text';
mail.BodyFormat = 'olFormatHTML';
mail.Display
word_editor = mail.GetInspector.WordEditor;
% starting range is [0,1]
word_editor.Range.Start
word_editor.Range.End
% copy to clipboard and paste in current range
clipboard('copy','TEXT01');
word_editor.Range.Paste
% range after the paste becomes [0,7]
word_editor.Range.Start
word_editor.Range.End
% collapse
word_editor.Range.Collapse(0);
% range after Collapse remains [0,7]
word_editor.Range.Start
word_editor.Range.End
Not terribly elegant, but this works:
% I might just do something like this
range_end = word_editor.Range.End;
word_editor.Range(range_end-1,range_end).InsertParagraph
clipboard('copy','TEXT02');
range_end = word_editor.Range.End;
word_editor.Range(range_end-1,range_end).Paste
Thanks again Guillaume and Walter.
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.
You have made my day! Thank you very much Guillaume!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Environment and Settings dans Centre d'aide et File Exchange

Produits

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by