Outlook WordEditor Range.Paste overwrites previous paste

23 次查看(过去 30 天)
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 个评论
Walter Roberson
Walter Roberson 2017-1-24
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.
Tony L.
Tony L. 2017-1-25
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.

请先登录,再进行评论。

采纳的回答

Guillaume
Guillaume 2017-1-25
编辑:Guillaume 2017-1-25
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 个评论
Guillaume
Guillaume 2017-1-25
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.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Environment and Settings 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by