Select and format pasted article in word










0















I have many occasions that I have to copy article from web-page, paste to word and format in a certain way. I had this code to auto paste and format. However, it only work once, and it just doesn't change the font of the pasted article later on.



Sub Macro1()

Dim artic As Word.Range
Set artic = Selection.Range
'keep bold word bold and avoid paragraphs to cluster into one
artic.PasteAndFormat (wdFormatOriginalFormatting)
'paste and select pasted article
artic.Select
artic.Font.Name = "Calibri"
artic.Font.Size = 10.5
artic.Font.Italic = False
artic.ParagraphFormat.Alignment = wdAlignParagraphLeft

End Sub









share|improve this question






















  • Can you explain what you want this macro to do each time you run it?

    – Marcucciboy2
    Nov 15 '18 at 7:55











  • I would want the macro to paste an article with Paste Option:Keep Source Formatting, select the pasted article, format it to Calibri, size 10.5, not italic and align it to the left.

    – Marcoqwe123
    Nov 15 '18 at 8:09















0















I have many occasions that I have to copy article from web-page, paste to word and format in a certain way. I had this code to auto paste and format. However, it only work once, and it just doesn't change the font of the pasted article later on.



Sub Macro1()

Dim artic As Word.Range
Set artic = Selection.Range
'keep bold word bold and avoid paragraphs to cluster into one
artic.PasteAndFormat (wdFormatOriginalFormatting)
'paste and select pasted article
artic.Select
artic.Font.Name = "Calibri"
artic.Font.Size = 10.5
artic.Font.Italic = False
artic.ParagraphFormat.Alignment = wdAlignParagraphLeft

End Sub









share|improve this question






















  • Can you explain what you want this macro to do each time you run it?

    – Marcucciboy2
    Nov 15 '18 at 7:55











  • I would want the macro to paste an article with Paste Option:Keep Source Formatting, select the pasted article, format it to Calibri, size 10.5, not italic and align it to the left.

    – Marcoqwe123
    Nov 15 '18 at 8:09













0












0








0


1






I have many occasions that I have to copy article from web-page, paste to word and format in a certain way. I had this code to auto paste and format. However, it only work once, and it just doesn't change the font of the pasted article later on.



Sub Macro1()

Dim artic As Word.Range
Set artic = Selection.Range
'keep bold word bold and avoid paragraphs to cluster into one
artic.PasteAndFormat (wdFormatOriginalFormatting)
'paste and select pasted article
artic.Select
artic.Font.Name = "Calibri"
artic.Font.Size = 10.5
artic.Font.Italic = False
artic.ParagraphFormat.Alignment = wdAlignParagraphLeft

End Sub









share|improve this question














I have many occasions that I have to copy article from web-page, paste to word and format in a certain way. I had this code to auto paste and format. However, it only work once, and it just doesn't change the font of the pasted article later on.



Sub Macro1()

Dim artic As Word.Range
Set artic = Selection.Range
'keep bold word bold and avoid paragraphs to cluster into one
artic.PasteAndFormat (wdFormatOriginalFormatting)
'paste and select pasted article
artic.Select
artic.Font.Name = "Calibri"
artic.Font.Size = 10.5
artic.Font.Italic = False
artic.ParagraphFormat.Alignment = wdAlignParagraphLeft

End Sub






vba ms-word word-vba






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 15 '18 at 7:49









Marcoqwe123Marcoqwe123

31




31












  • Can you explain what you want this macro to do each time you run it?

    – Marcucciboy2
    Nov 15 '18 at 7:55











  • I would want the macro to paste an article with Paste Option:Keep Source Formatting, select the pasted article, format it to Calibri, size 10.5, not italic and align it to the left.

    – Marcoqwe123
    Nov 15 '18 at 8:09

















  • Can you explain what you want this macro to do each time you run it?

    – Marcucciboy2
    Nov 15 '18 at 7:55











  • I would want the macro to paste an article with Paste Option:Keep Source Formatting, select the pasted article, format it to Calibri, size 10.5, not italic and align it to the left.

    – Marcoqwe123
    Nov 15 '18 at 8:09
















Can you explain what you want this macro to do each time you run it?

– Marcucciboy2
Nov 15 '18 at 7:55





Can you explain what you want this macro to do each time you run it?

– Marcucciboy2
Nov 15 '18 at 7:55













I would want the macro to paste an article with Paste Option:Keep Source Formatting, select the pasted article, format it to Calibri, size 10.5, not italic and align it to the left.

– Marcoqwe123
Nov 15 '18 at 8:09





I would want the macro to paste an article with Paste Option:Keep Source Formatting, select the pasted article, format it to Calibri, size 10.5, not italic and align it to the left.

– Marcoqwe123
Nov 15 '18 at 8:09












1 Answer
1






active

oldest

votes


















0














If you set a breakpoint at artic.Font.Name = "Calibri" so that the code stops after artic.Select you'll see that the Paste method does not include what's been pasted. Generally, artic will be at the beginning of the pasted content.



This means the code needs to be able to locate the position just after where the Selection was before pasting. It also depends on whether the paste occurs at the end of the document, or not.



The following sample code worked for me in my tests. It uses two Ranges: one for where the content will be pasted, the other for the end position after pasting.



(Responding to request for more clarification about the Word object model): Think of a Range object like a selection, with the difference that there can be many Range objects, but only one Selection. When manipulating a Range it often helps to think of using the keyboard to reduce or expand it. Pressing the left- or right-arrow keys will "collapse" a selection to an insertion point; holding Shift and pressing these keys will expand/reduce a selection; holding Shift while clicking somewhere else in the document will also do that. Think of setting Range.Start or Range.End as the equivalent of this last. The start or end point of the Range is being arbitrarily set to another location in the document.



In the case of the first If in the code below the Range is being reduced/collapsed to its starting point (think left-arrow key), then moved one character to the right (think right-arrow key). This puts it beyond where new material will be pasted, so extending the paste point's end to this Range's starting point will pick up everything between the two.



Sub TestPasteAndSelect()
Dim artic As Word.Range, rng As Word.Range
Dim bNotAtEnd As Boolean

Set artic = Selection.Range
Set rng = artic.Duplicate
rng.End = ActiveDocument.content.End
If rng.Characters.Count > 1 Then
'selection is not at end of document
rng.Collapse wdCollapseStart
rng.MoveStart wdCharacter, 1
bNotAtEnd = True
End If
'keep bold word bold and avoid paragraphs to cluster into one
artic.PasteAndFormat (wdFormatOriginalFormatting)
'paste and select pasted article

'artic.Select
'rng.Select
If bNotAtEnd Then
artic.End = rng.Start
Else
Set artic = rng.Duplicate
End If
artic.Font.Name = "Calibri"
artic.Font.Size = 10.5
artic.Font.Italic = False
artic.ParagraphFormat.Alignment = wdAlignParagraphLeft
End Sub





share|improve this answer

























  • I have a bit of background in Python but not much in vba. I can see you mean the code highlights the area where "the Selection" is going to be at before "the Selection" is placed. I am quite loss in the first loop. How do ".End" ,"rng.Collapse wdCollapseStart" and "rng.MoveStart wdCharacter, 1" function?

    – Marcoqwe123
    Nov 16 '18 at 8:51











  • @Marcoqwe123 I've expanded on the principle in the Answer. Is it clearer now?

    – Cindy Meister
    Nov 16 '18 at 13:23











  • The expansion has been very helpful to me in learning the concept! Much appreciated

    – Marcoqwe123
    Nov 20 '18 at 0:32










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
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53314633%2fselect-and-format-pasted-article-in-word%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









0














If you set a breakpoint at artic.Font.Name = "Calibri" so that the code stops after artic.Select you'll see that the Paste method does not include what's been pasted. Generally, artic will be at the beginning of the pasted content.



This means the code needs to be able to locate the position just after where the Selection was before pasting. It also depends on whether the paste occurs at the end of the document, or not.



The following sample code worked for me in my tests. It uses two Ranges: one for where the content will be pasted, the other for the end position after pasting.



(Responding to request for more clarification about the Word object model): Think of a Range object like a selection, with the difference that there can be many Range objects, but only one Selection. When manipulating a Range it often helps to think of using the keyboard to reduce or expand it. Pressing the left- or right-arrow keys will "collapse" a selection to an insertion point; holding Shift and pressing these keys will expand/reduce a selection; holding Shift while clicking somewhere else in the document will also do that. Think of setting Range.Start or Range.End as the equivalent of this last. The start or end point of the Range is being arbitrarily set to another location in the document.



In the case of the first If in the code below the Range is being reduced/collapsed to its starting point (think left-arrow key), then moved one character to the right (think right-arrow key). This puts it beyond where new material will be pasted, so extending the paste point's end to this Range's starting point will pick up everything between the two.



Sub TestPasteAndSelect()
Dim artic As Word.Range, rng As Word.Range
Dim bNotAtEnd As Boolean

Set artic = Selection.Range
Set rng = artic.Duplicate
rng.End = ActiveDocument.content.End
If rng.Characters.Count > 1 Then
'selection is not at end of document
rng.Collapse wdCollapseStart
rng.MoveStart wdCharacter, 1
bNotAtEnd = True
End If
'keep bold word bold and avoid paragraphs to cluster into one
artic.PasteAndFormat (wdFormatOriginalFormatting)
'paste and select pasted article

'artic.Select
'rng.Select
If bNotAtEnd Then
artic.End = rng.Start
Else
Set artic = rng.Duplicate
End If
artic.Font.Name = "Calibri"
artic.Font.Size = 10.5
artic.Font.Italic = False
artic.ParagraphFormat.Alignment = wdAlignParagraphLeft
End Sub





share|improve this answer

























  • I have a bit of background in Python but not much in vba. I can see you mean the code highlights the area where "the Selection" is going to be at before "the Selection" is placed. I am quite loss in the first loop. How do ".End" ,"rng.Collapse wdCollapseStart" and "rng.MoveStart wdCharacter, 1" function?

    – Marcoqwe123
    Nov 16 '18 at 8:51











  • @Marcoqwe123 I've expanded on the principle in the Answer. Is it clearer now?

    – Cindy Meister
    Nov 16 '18 at 13:23











  • The expansion has been very helpful to me in learning the concept! Much appreciated

    – Marcoqwe123
    Nov 20 '18 at 0:32















0














If you set a breakpoint at artic.Font.Name = "Calibri" so that the code stops after artic.Select you'll see that the Paste method does not include what's been pasted. Generally, artic will be at the beginning of the pasted content.



This means the code needs to be able to locate the position just after where the Selection was before pasting. It also depends on whether the paste occurs at the end of the document, or not.



The following sample code worked for me in my tests. It uses two Ranges: one for where the content will be pasted, the other for the end position after pasting.



(Responding to request for more clarification about the Word object model): Think of a Range object like a selection, with the difference that there can be many Range objects, but only one Selection. When manipulating a Range it often helps to think of using the keyboard to reduce or expand it. Pressing the left- or right-arrow keys will "collapse" a selection to an insertion point; holding Shift and pressing these keys will expand/reduce a selection; holding Shift while clicking somewhere else in the document will also do that. Think of setting Range.Start or Range.End as the equivalent of this last. The start or end point of the Range is being arbitrarily set to another location in the document.



In the case of the first If in the code below the Range is being reduced/collapsed to its starting point (think left-arrow key), then moved one character to the right (think right-arrow key). This puts it beyond where new material will be pasted, so extending the paste point's end to this Range's starting point will pick up everything between the two.



Sub TestPasteAndSelect()
Dim artic As Word.Range, rng As Word.Range
Dim bNotAtEnd As Boolean

Set artic = Selection.Range
Set rng = artic.Duplicate
rng.End = ActiveDocument.content.End
If rng.Characters.Count > 1 Then
'selection is not at end of document
rng.Collapse wdCollapseStart
rng.MoveStart wdCharacter, 1
bNotAtEnd = True
End If
'keep bold word bold and avoid paragraphs to cluster into one
artic.PasteAndFormat (wdFormatOriginalFormatting)
'paste and select pasted article

'artic.Select
'rng.Select
If bNotAtEnd Then
artic.End = rng.Start
Else
Set artic = rng.Duplicate
End If
artic.Font.Name = "Calibri"
artic.Font.Size = 10.5
artic.Font.Italic = False
artic.ParagraphFormat.Alignment = wdAlignParagraphLeft
End Sub





share|improve this answer

























  • I have a bit of background in Python but not much in vba. I can see you mean the code highlights the area where "the Selection" is going to be at before "the Selection" is placed. I am quite loss in the first loop. How do ".End" ,"rng.Collapse wdCollapseStart" and "rng.MoveStart wdCharacter, 1" function?

    – Marcoqwe123
    Nov 16 '18 at 8:51











  • @Marcoqwe123 I've expanded on the principle in the Answer. Is it clearer now?

    – Cindy Meister
    Nov 16 '18 at 13:23











  • The expansion has been very helpful to me in learning the concept! Much appreciated

    – Marcoqwe123
    Nov 20 '18 at 0:32













0












0








0







If you set a breakpoint at artic.Font.Name = "Calibri" so that the code stops after artic.Select you'll see that the Paste method does not include what's been pasted. Generally, artic will be at the beginning of the pasted content.



This means the code needs to be able to locate the position just after where the Selection was before pasting. It also depends on whether the paste occurs at the end of the document, or not.



The following sample code worked for me in my tests. It uses two Ranges: one for where the content will be pasted, the other for the end position after pasting.



(Responding to request for more clarification about the Word object model): Think of a Range object like a selection, with the difference that there can be many Range objects, but only one Selection. When manipulating a Range it often helps to think of using the keyboard to reduce or expand it. Pressing the left- or right-arrow keys will "collapse" a selection to an insertion point; holding Shift and pressing these keys will expand/reduce a selection; holding Shift while clicking somewhere else in the document will also do that. Think of setting Range.Start or Range.End as the equivalent of this last. The start or end point of the Range is being arbitrarily set to another location in the document.



In the case of the first If in the code below the Range is being reduced/collapsed to its starting point (think left-arrow key), then moved one character to the right (think right-arrow key). This puts it beyond where new material will be pasted, so extending the paste point's end to this Range's starting point will pick up everything between the two.



Sub TestPasteAndSelect()
Dim artic As Word.Range, rng As Word.Range
Dim bNotAtEnd As Boolean

Set artic = Selection.Range
Set rng = artic.Duplicate
rng.End = ActiveDocument.content.End
If rng.Characters.Count > 1 Then
'selection is not at end of document
rng.Collapse wdCollapseStart
rng.MoveStart wdCharacter, 1
bNotAtEnd = True
End If
'keep bold word bold and avoid paragraphs to cluster into one
artic.PasteAndFormat (wdFormatOriginalFormatting)
'paste and select pasted article

'artic.Select
'rng.Select
If bNotAtEnd Then
artic.End = rng.Start
Else
Set artic = rng.Duplicate
End If
artic.Font.Name = "Calibri"
artic.Font.Size = 10.5
artic.Font.Italic = False
artic.ParagraphFormat.Alignment = wdAlignParagraphLeft
End Sub





share|improve this answer















If you set a breakpoint at artic.Font.Name = "Calibri" so that the code stops after artic.Select you'll see that the Paste method does not include what's been pasted. Generally, artic will be at the beginning of the pasted content.



This means the code needs to be able to locate the position just after where the Selection was before pasting. It also depends on whether the paste occurs at the end of the document, or not.



The following sample code worked for me in my tests. It uses two Ranges: one for where the content will be pasted, the other for the end position after pasting.



(Responding to request for more clarification about the Word object model): Think of a Range object like a selection, with the difference that there can be many Range objects, but only one Selection. When manipulating a Range it often helps to think of using the keyboard to reduce or expand it. Pressing the left- or right-arrow keys will "collapse" a selection to an insertion point; holding Shift and pressing these keys will expand/reduce a selection; holding Shift while clicking somewhere else in the document will also do that. Think of setting Range.Start or Range.End as the equivalent of this last. The start or end point of the Range is being arbitrarily set to another location in the document.



In the case of the first If in the code below the Range is being reduced/collapsed to its starting point (think left-arrow key), then moved one character to the right (think right-arrow key). This puts it beyond where new material will be pasted, so extending the paste point's end to this Range's starting point will pick up everything between the two.



Sub TestPasteAndSelect()
Dim artic As Word.Range, rng As Word.Range
Dim bNotAtEnd As Boolean

Set artic = Selection.Range
Set rng = artic.Duplicate
rng.End = ActiveDocument.content.End
If rng.Characters.Count > 1 Then
'selection is not at end of document
rng.Collapse wdCollapseStart
rng.MoveStart wdCharacter, 1
bNotAtEnd = True
End If
'keep bold word bold and avoid paragraphs to cluster into one
artic.PasteAndFormat (wdFormatOriginalFormatting)
'paste and select pasted article

'artic.Select
'rng.Select
If bNotAtEnd Then
artic.End = rng.Start
Else
Set artic = rng.Duplicate
End If
artic.Font.Name = "Calibri"
artic.Font.Size = 10.5
artic.Font.Italic = False
artic.ParagraphFormat.Alignment = wdAlignParagraphLeft
End Sub






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 16 '18 at 13:23

























answered Nov 15 '18 at 12:17









Cindy MeisterCindy Meister

15.8k102437




15.8k102437












  • I have a bit of background in Python but not much in vba. I can see you mean the code highlights the area where "the Selection" is going to be at before "the Selection" is placed. I am quite loss in the first loop. How do ".End" ,"rng.Collapse wdCollapseStart" and "rng.MoveStart wdCharacter, 1" function?

    – Marcoqwe123
    Nov 16 '18 at 8:51











  • @Marcoqwe123 I've expanded on the principle in the Answer. Is it clearer now?

    – Cindy Meister
    Nov 16 '18 at 13:23











  • The expansion has been very helpful to me in learning the concept! Much appreciated

    – Marcoqwe123
    Nov 20 '18 at 0:32

















  • I have a bit of background in Python but not much in vba. I can see you mean the code highlights the area where "the Selection" is going to be at before "the Selection" is placed. I am quite loss in the first loop. How do ".End" ,"rng.Collapse wdCollapseStart" and "rng.MoveStart wdCharacter, 1" function?

    – Marcoqwe123
    Nov 16 '18 at 8:51











  • @Marcoqwe123 I've expanded on the principle in the Answer. Is it clearer now?

    – Cindy Meister
    Nov 16 '18 at 13:23











  • The expansion has been very helpful to me in learning the concept! Much appreciated

    – Marcoqwe123
    Nov 20 '18 at 0:32
















I have a bit of background in Python but not much in vba. I can see you mean the code highlights the area where "the Selection" is going to be at before "the Selection" is placed. I am quite loss in the first loop. How do ".End" ,"rng.Collapse wdCollapseStart" and "rng.MoveStart wdCharacter, 1" function?

– Marcoqwe123
Nov 16 '18 at 8:51





I have a bit of background in Python but not much in vba. I can see you mean the code highlights the area where "the Selection" is going to be at before "the Selection" is placed. I am quite loss in the first loop. How do ".End" ,"rng.Collapse wdCollapseStart" and "rng.MoveStart wdCharacter, 1" function?

– Marcoqwe123
Nov 16 '18 at 8:51













@Marcoqwe123 I've expanded on the principle in the Answer. Is it clearer now?

– Cindy Meister
Nov 16 '18 at 13:23





@Marcoqwe123 I've expanded on the principle in the Answer. Is it clearer now?

– Cindy Meister
Nov 16 '18 at 13:23













The expansion has been very helpful to me in learning the concept! Much appreciated

– Marcoqwe123
Nov 20 '18 at 0:32





The expansion has been very helpful to me in learning the concept! Much appreciated

– Marcoqwe123
Nov 20 '18 at 0:32



















draft saved

draft discarded
















































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.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53314633%2fselect-and-format-pasted-article-in-word%23new-answer', 'question_page');

);

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







這個網誌中的熱門文章

How to read a connectionString WITH PROVIDER in .NET Core?

Node.js Script on GitHub Pages or Amazon S3

Museum of Modern and Contemporary Art of Trento and Rovereto