Fitting images to document's margins in a docx file











up vote
3
down vote

favorite
1












I've got a docx file with many figures, all do not fit the margins of the document. I can manually adjust the sizes of the figures in the file, but would love to have some way to automate this (either from Word, from a command line tool, or any other means).



(PS: this is a follow-up to this question)










share|improve this question




















  • 1




    I'm sure this is possible with VBA, i.e. iterate through all embedded images in the doc and resize them to fit within the current page margins.
    – Karan
    Mar 23 '13 at 1:20










  • Thanks Karan, should I ask this in SO than?
    – Tal Galili
    Mar 23 '13 at 1:22










  • Macros are on-topic here too, so I'd recommend waiting for answers here for a while.
    – Karan
    Mar 23 '13 at 1:24

















up vote
3
down vote

favorite
1












I've got a docx file with many figures, all do not fit the margins of the document. I can manually adjust the sizes of the figures in the file, but would love to have some way to automate this (either from Word, from a command line tool, or any other means).



(PS: this is a follow-up to this question)










share|improve this question




















  • 1




    I'm sure this is possible with VBA, i.e. iterate through all embedded images in the doc and resize them to fit within the current page margins.
    – Karan
    Mar 23 '13 at 1:20










  • Thanks Karan, should I ask this in SO than?
    – Tal Galili
    Mar 23 '13 at 1:22










  • Macros are on-topic here too, so I'd recommend waiting for answers here for a while.
    – Karan
    Mar 23 '13 at 1:24















up vote
3
down vote

favorite
1









up vote
3
down vote

favorite
1






1





I've got a docx file with many figures, all do not fit the margins of the document. I can manually adjust the sizes of the figures in the file, but would love to have some way to automate this (either from Word, from a command line tool, or any other means).



(PS: this is a follow-up to this question)










share|improve this question















I've got a docx file with many figures, all do not fit the margins of the document. I can manually adjust the sizes of the figures in the file, but would love to have some way to automate this (either from Word, from a command line tool, or any other means).



(PS: this is a follow-up to this question)







microsoft-word images vba docx






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited May 23 '17 at 12:41









Community

1




1










asked Mar 22 '13 at 21:37









Tal Galili

1,360102845




1,360102845








  • 1




    I'm sure this is possible with VBA, i.e. iterate through all embedded images in the doc and resize them to fit within the current page margins.
    – Karan
    Mar 23 '13 at 1:20










  • Thanks Karan, should I ask this in SO than?
    – Tal Galili
    Mar 23 '13 at 1:22










  • Macros are on-topic here too, so I'd recommend waiting for answers here for a while.
    – Karan
    Mar 23 '13 at 1:24
















  • 1




    I'm sure this is possible with VBA, i.e. iterate through all embedded images in the doc and resize them to fit within the current page margins.
    – Karan
    Mar 23 '13 at 1:20










  • Thanks Karan, should I ask this in SO than?
    – Tal Galili
    Mar 23 '13 at 1:22










  • Macros are on-topic here too, so I'd recommend waiting for answers here for a while.
    – Karan
    Mar 23 '13 at 1:24










1




1




I'm sure this is possible with VBA, i.e. iterate through all embedded images in the doc and resize them to fit within the current page margins.
– Karan
Mar 23 '13 at 1:20




I'm sure this is possible with VBA, i.e. iterate through all embedded images in the doc and resize them to fit within the current page margins.
– Karan
Mar 23 '13 at 1:20












Thanks Karan, should I ask this in SO than?
– Tal Galili
Mar 23 '13 at 1:22




Thanks Karan, should I ask this in SO than?
– Tal Galili
Mar 23 '13 at 1:22












Macros are on-topic here too, so I'd recommend waiting for answers here for a while.
– Karan
Mar 23 '13 at 1:24






Macros are on-topic here too, so I'd recommend waiting for answers here for a while.
– Karan
Mar 23 '13 at 1:24












2 Answers
2






active

oldest

votes

















up vote
0
down vote













Reading Visual Basic Macro in Word to Resize/Center/Delete All Images, How to resize all images in Word document and How can I resize a table to fit the page's width fixed the Kelly Tessena Keck solution a bit.



Now it's working with any available page width (don't forget to fix height, if needed, too) :



Sub PicturesFitPageWidth()
' ResizePic Macro
' Resizes an image

Shapes = ActiveDocument.Shapes.Count
InLines = ActiveDocument.InlineShapes.Count
'Sets the variables to loop through all shapes in the document, one for shapes and one for inline shapes.


'Calculate usable width of page

With ActiveDocument.PageSetup
WidthAvail = .PageWidth - .LeftMargin - .RightMargin
End With

For ShapeLoop = 1 To Shapes
MsgBox Prompt:="Shape " & ShapeLoop & " width: " & ActiveDocument.Shapes(ShapeLoop).Width
If ActiveDocument.Shapes(ShapeLoop).Width > WidthAvail Then
ActiveDocument.Shapes(ShapeLoop).Width = WidthAvail
End If
Next ShapeLoop
'Loops through all shapes in the document. Checks to see if they're too wide, and if they are, resizes them.

For InLineLoop = 1 To InLines
MsgBox Prompt:="Inline " & InLineLoop & " width: " & ActiveDocument.InlineShapes(InLineLoop).Width
If ActiveDocument.InlineShapes(InLineLoop).Width > WidthAvail Then
ActiveDocument.InlineShapes(InLineLoop).Width = WidthAvail
End If
Next InLineLoop
'Loops through all shapes in the document. Checks to see if they're too wide, and if they are, resizes them.

End Sub





share|improve this answer




























    up vote
    0
    down vote













    You can do this with the following VBA code. It counts the shapes in the document, checks their width against the available space on the page, and resizes if necessary.



    Note that Word has two different collections for Shapes and InlineShapes, hence the two different For loops. Also, it uses a series of If/ElseIf statements to identify the page width based on standard paper sizes. Currently, the only options are letter size in either portrait or landscape, but you can add more ElseIfs for any paper sizes you need.



    Sub ResizePic()
    ' ResizePic Macro
    ' Resizes an image

    Shapes = ActiveDocument.Shapes.Count
    InLines = ActiveDocument.InlineShapes.Count
    'Sets the variables to loop through all shapes in the document, one for shapes and one for inline shapes.


    RightMar = ActiveDocument.PageSetup.RightMargin
    LeftMar = ActiveDocument.PageSetup.LeftMargin
    PaperType = ActiveDocument.PageSetup.PaperSize
    PageLayout = ActiveDocument.PageSetup.Orientation
    'Sets up variables for margin sizes, paper type, and page layout.
    ' This is used to find the usable width of the document, which is the max width for the picture.

    If PaperType = wdPaperLetter And PageLayout = wdPortrait Then
    WidthAvail = InchesToPoints(8.5) - (LeftMar + RightMar)
    ElseIf PaperType = wdPaperLetter And PageLayout = wdLandscape Then
    WidthAvail = InchesToPoints(11) - (LeftMar + RightMar)
    End If
    'Identifies the usable width of the document, based on margins and paper size.

    For ShapeLoop = 1 To Shapes
    MsgBox Prompt:="Shape " & ShapeLoop & " width: " & ActiveDocument.Shapes(ShapeLoop).Width
    If ActiveDocument.Shapes(ShapeLoop).Width > WidthAvail Then
    ActiveDocument.Shapes(ShapeLoop).Width = WidthAvail
    End If
    Next ShapeLoop
    'Loops through all shapes in the document. Checks to see if they're too wide, and if they are, resizes them.

    For InLineLoop = 1 To InLines
    MsgBox Prompt:="Inline " & InLineLoop & " width: " & ActiveDocument.InlineShapes(InLineLoop).Width
    If ActiveDocument.InlineShapes(InLineLoop).Width > WidthAvail Then
    ActiveDocument.InlineShapes(InLineLoop).Width = WidthAvail
    End If
    Next InLineLoop
    'Loops through all shapes in the document. Checks to see if they're too wide, and if they are, resizes them.

    End Sub





    share|improve this answer























    • For me it just looped through the pictures in document and they are gone from my view(. It's a pity as the script looks like a kind of working one.
      – WebComer
      May 28 at 15:29










    • I think the script should check if the shape is a pic, as for a document with just two pics it did find 5 shapes and set their width to zero, so they did gone from the view.
      – WebComer
      May 28 at 15:47













    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "3"
    };
    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',
    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%2fsuperuser.com%2fquestions%2f570121%2ffitting-images-to-documents-margins-in-a-docx-file%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    Reading Visual Basic Macro in Word to Resize/Center/Delete All Images, How to resize all images in Word document and How can I resize a table to fit the page's width fixed the Kelly Tessena Keck solution a bit.



    Now it's working with any available page width (don't forget to fix height, if needed, too) :



    Sub PicturesFitPageWidth()
    ' ResizePic Macro
    ' Resizes an image

    Shapes = ActiveDocument.Shapes.Count
    InLines = ActiveDocument.InlineShapes.Count
    'Sets the variables to loop through all shapes in the document, one for shapes and one for inline shapes.


    'Calculate usable width of page

    With ActiveDocument.PageSetup
    WidthAvail = .PageWidth - .LeftMargin - .RightMargin
    End With

    For ShapeLoop = 1 To Shapes
    MsgBox Prompt:="Shape " & ShapeLoop & " width: " & ActiveDocument.Shapes(ShapeLoop).Width
    If ActiveDocument.Shapes(ShapeLoop).Width > WidthAvail Then
    ActiveDocument.Shapes(ShapeLoop).Width = WidthAvail
    End If
    Next ShapeLoop
    'Loops through all shapes in the document. Checks to see if they're too wide, and if they are, resizes them.

    For InLineLoop = 1 To InLines
    MsgBox Prompt:="Inline " & InLineLoop & " width: " & ActiveDocument.InlineShapes(InLineLoop).Width
    If ActiveDocument.InlineShapes(InLineLoop).Width > WidthAvail Then
    ActiveDocument.InlineShapes(InLineLoop).Width = WidthAvail
    End If
    Next InLineLoop
    'Loops through all shapes in the document. Checks to see if they're too wide, and if they are, resizes them.

    End Sub





    share|improve this answer

























      up vote
      0
      down vote













      Reading Visual Basic Macro in Word to Resize/Center/Delete All Images, How to resize all images in Word document and How can I resize a table to fit the page's width fixed the Kelly Tessena Keck solution a bit.



      Now it's working with any available page width (don't forget to fix height, if needed, too) :



      Sub PicturesFitPageWidth()
      ' ResizePic Macro
      ' Resizes an image

      Shapes = ActiveDocument.Shapes.Count
      InLines = ActiveDocument.InlineShapes.Count
      'Sets the variables to loop through all shapes in the document, one for shapes and one for inline shapes.


      'Calculate usable width of page

      With ActiveDocument.PageSetup
      WidthAvail = .PageWidth - .LeftMargin - .RightMargin
      End With

      For ShapeLoop = 1 To Shapes
      MsgBox Prompt:="Shape " & ShapeLoop & " width: " & ActiveDocument.Shapes(ShapeLoop).Width
      If ActiveDocument.Shapes(ShapeLoop).Width > WidthAvail Then
      ActiveDocument.Shapes(ShapeLoop).Width = WidthAvail
      End If
      Next ShapeLoop
      'Loops through all shapes in the document. Checks to see if they're too wide, and if they are, resizes them.

      For InLineLoop = 1 To InLines
      MsgBox Prompt:="Inline " & InLineLoop & " width: " & ActiveDocument.InlineShapes(InLineLoop).Width
      If ActiveDocument.InlineShapes(InLineLoop).Width > WidthAvail Then
      ActiveDocument.InlineShapes(InLineLoop).Width = WidthAvail
      End If
      Next InLineLoop
      'Loops through all shapes in the document. Checks to see if they're too wide, and if they are, resizes them.

      End Sub





      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        Reading Visual Basic Macro in Word to Resize/Center/Delete All Images, How to resize all images in Word document and How can I resize a table to fit the page's width fixed the Kelly Tessena Keck solution a bit.



        Now it's working with any available page width (don't forget to fix height, if needed, too) :



        Sub PicturesFitPageWidth()
        ' ResizePic Macro
        ' Resizes an image

        Shapes = ActiveDocument.Shapes.Count
        InLines = ActiveDocument.InlineShapes.Count
        'Sets the variables to loop through all shapes in the document, one for shapes and one for inline shapes.


        'Calculate usable width of page

        With ActiveDocument.PageSetup
        WidthAvail = .PageWidth - .LeftMargin - .RightMargin
        End With

        For ShapeLoop = 1 To Shapes
        MsgBox Prompt:="Shape " & ShapeLoop & " width: " & ActiveDocument.Shapes(ShapeLoop).Width
        If ActiveDocument.Shapes(ShapeLoop).Width > WidthAvail Then
        ActiveDocument.Shapes(ShapeLoop).Width = WidthAvail
        End If
        Next ShapeLoop
        'Loops through all shapes in the document. Checks to see if they're too wide, and if they are, resizes them.

        For InLineLoop = 1 To InLines
        MsgBox Prompt:="Inline " & InLineLoop & " width: " & ActiveDocument.InlineShapes(InLineLoop).Width
        If ActiveDocument.InlineShapes(InLineLoop).Width > WidthAvail Then
        ActiveDocument.InlineShapes(InLineLoop).Width = WidthAvail
        End If
        Next InLineLoop
        'Loops through all shapes in the document. Checks to see if they're too wide, and if they are, resizes them.

        End Sub





        share|improve this answer












        Reading Visual Basic Macro in Word to Resize/Center/Delete All Images, How to resize all images in Word document and How can I resize a table to fit the page's width fixed the Kelly Tessena Keck solution a bit.



        Now it's working with any available page width (don't forget to fix height, if needed, too) :



        Sub PicturesFitPageWidth()
        ' ResizePic Macro
        ' Resizes an image

        Shapes = ActiveDocument.Shapes.Count
        InLines = ActiveDocument.InlineShapes.Count
        'Sets the variables to loop through all shapes in the document, one for shapes and one for inline shapes.


        'Calculate usable width of page

        With ActiveDocument.PageSetup
        WidthAvail = .PageWidth - .LeftMargin - .RightMargin
        End With

        For ShapeLoop = 1 To Shapes
        MsgBox Prompt:="Shape " & ShapeLoop & " width: " & ActiveDocument.Shapes(ShapeLoop).Width
        If ActiveDocument.Shapes(ShapeLoop).Width > WidthAvail Then
        ActiveDocument.Shapes(ShapeLoop).Width = WidthAvail
        End If
        Next ShapeLoop
        'Loops through all shapes in the document. Checks to see if they're too wide, and if they are, resizes them.

        For InLineLoop = 1 To InLines
        MsgBox Prompt:="Inline " & InLineLoop & " width: " & ActiveDocument.InlineShapes(InLineLoop).Width
        If ActiveDocument.InlineShapes(InLineLoop).Width > WidthAvail Then
        ActiveDocument.InlineShapes(InLineLoop).Width = WidthAvail
        End If
        Next InLineLoop
        'Loops through all shapes in the document. Checks to see if they're too wide, and if they are, resizes them.

        End Sub






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered May 28 at 20:20









        WebComer

        995




        995
























            up vote
            0
            down vote













            You can do this with the following VBA code. It counts the shapes in the document, checks their width against the available space on the page, and resizes if necessary.



            Note that Word has two different collections for Shapes and InlineShapes, hence the two different For loops. Also, it uses a series of If/ElseIf statements to identify the page width based on standard paper sizes. Currently, the only options are letter size in either portrait or landscape, but you can add more ElseIfs for any paper sizes you need.



            Sub ResizePic()
            ' ResizePic Macro
            ' Resizes an image

            Shapes = ActiveDocument.Shapes.Count
            InLines = ActiveDocument.InlineShapes.Count
            'Sets the variables to loop through all shapes in the document, one for shapes and one for inline shapes.


            RightMar = ActiveDocument.PageSetup.RightMargin
            LeftMar = ActiveDocument.PageSetup.LeftMargin
            PaperType = ActiveDocument.PageSetup.PaperSize
            PageLayout = ActiveDocument.PageSetup.Orientation
            'Sets up variables for margin sizes, paper type, and page layout.
            ' This is used to find the usable width of the document, which is the max width for the picture.

            If PaperType = wdPaperLetter And PageLayout = wdPortrait Then
            WidthAvail = InchesToPoints(8.5) - (LeftMar + RightMar)
            ElseIf PaperType = wdPaperLetter And PageLayout = wdLandscape Then
            WidthAvail = InchesToPoints(11) - (LeftMar + RightMar)
            End If
            'Identifies the usable width of the document, based on margins and paper size.

            For ShapeLoop = 1 To Shapes
            MsgBox Prompt:="Shape " & ShapeLoop & " width: " & ActiveDocument.Shapes(ShapeLoop).Width
            If ActiveDocument.Shapes(ShapeLoop).Width > WidthAvail Then
            ActiveDocument.Shapes(ShapeLoop).Width = WidthAvail
            End If
            Next ShapeLoop
            'Loops through all shapes in the document. Checks to see if they're too wide, and if they are, resizes them.

            For InLineLoop = 1 To InLines
            MsgBox Prompt:="Inline " & InLineLoop & " width: " & ActiveDocument.InlineShapes(InLineLoop).Width
            If ActiveDocument.InlineShapes(InLineLoop).Width > WidthAvail Then
            ActiveDocument.InlineShapes(InLineLoop).Width = WidthAvail
            End If
            Next InLineLoop
            'Loops through all shapes in the document. Checks to see if they're too wide, and if they are, resizes them.

            End Sub





            share|improve this answer























            • For me it just looped through the pictures in document and they are gone from my view(. It's a pity as the script looks like a kind of working one.
              – WebComer
              May 28 at 15:29










            • I think the script should check if the shape is a pic, as for a document with just two pics it did find 5 shapes and set their width to zero, so they did gone from the view.
              – WebComer
              May 28 at 15:47

















            up vote
            0
            down vote













            You can do this with the following VBA code. It counts the shapes in the document, checks their width against the available space on the page, and resizes if necessary.



            Note that Word has two different collections for Shapes and InlineShapes, hence the two different For loops. Also, it uses a series of If/ElseIf statements to identify the page width based on standard paper sizes. Currently, the only options are letter size in either portrait or landscape, but you can add more ElseIfs for any paper sizes you need.



            Sub ResizePic()
            ' ResizePic Macro
            ' Resizes an image

            Shapes = ActiveDocument.Shapes.Count
            InLines = ActiveDocument.InlineShapes.Count
            'Sets the variables to loop through all shapes in the document, one for shapes and one for inline shapes.


            RightMar = ActiveDocument.PageSetup.RightMargin
            LeftMar = ActiveDocument.PageSetup.LeftMargin
            PaperType = ActiveDocument.PageSetup.PaperSize
            PageLayout = ActiveDocument.PageSetup.Orientation
            'Sets up variables for margin sizes, paper type, and page layout.
            ' This is used to find the usable width of the document, which is the max width for the picture.

            If PaperType = wdPaperLetter And PageLayout = wdPortrait Then
            WidthAvail = InchesToPoints(8.5) - (LeftMar + RightMar)
            ElseIf PaperType = wdPaperLetter And PageLayout = wdLandscape Then
            WidthAvail = InchesToPoints(11) - (LeftMar + RightMar)
            End If
            'Identifies the usable width of the document, based on margins and paper size.

            For ShapeLoop = 1 To Shapes
            MsgBox Prompt:="Shape " & ShapeLoop & " width: " & ActiveDocument.Shapes(ShapeLoop).Width
            If ActiveDocument.Shapes(ShapeLoop).Width > WidthAvail Then
            ActiveDocument.Shapes(ShapeLoop).Width = WidthAvail
            End If
            Next ShapeLoop
            'Loops through all shapes in the document. Checks to see if they're too wide, and if they are, resizes them.

            For InLineLoop = 1 To InLines
            MsgBox Prompt:="Inline " & InLineLoop & " width: " & ActiveDocument.InlineShapes(InLineLoop).Width
            If ActiveDocument.InlineShapes(InLineLoop).Width > WidthAvail Then
            ActiveDocument.InlineShapes(InLineLoop).Width = WidthAvail
            End If
            Next InLineLoop
            'Loops through all shapes in the document. Checks to see if they're too wide, and if they are, resizes them.

            End Sub





            share|improve this answer























            • For me it just looped through the pictures in document and they are gone from my view(. It's a pity as the script looks like a kind of working one.
              – WebComer
              May 28 at 15:29










            • I think the script should check if the shape is a pic, as for a document with just two pics it did find 5 shapes and set their width to zero, so they did gone from the view.
              – WebComer
              May 28 at 15:47















            up vote
            0
            down vote










            up vote
            0
            down vote









            You can do this with the following VBA code. It counts the shapes in the document, checks their width against the available space on the page, and resizes if necessary.



            Note that Word has two different collections for Shapes and InlineShapes, hence the two different For loops. Also, it uses a series of If/ElseIf statements to identify the page width based on standard paper sizes. Currently, the only options are letter size in either portrait or landscape, but you can add more ElseIfs for any paper sizes you need.



            Sub ResizePic()
            ' ResizePic Macro
            ' Resizes an image

            Shapes = ActiveDocument.Shapes.Count
            InLines = ActiveDocument.InlineShapes.Count
            'Sets the variables to loop through all shapes in the document, one for shapes and one for inline shapes.


            RightMar = ActiveDocument.PageSetup.RightMargin
            LeftMar = ActiveDocument.PageSetup.LeftMargin
            PaperType = ActiveDocument.PageSetup.PaperSize
            PageLayout = ActiveDocument.PageSetup.Orientation
            'Sets up variables for margin sizes, paper type, and page layout.
            ' This is used to find the usable width of the document, which is the max width for the picture.

            If PaperType = wdPaperLetter And PageLayout = wdPortrait Then
            WidthAvail = InchesToPoints(8.5) - (LeftMar + RightMar)
            ElseIf PaperType = wdPaperLetter And PageLayout = wdLandscape Then
            WidthAvail = InchesToPoints(11) - (LeftMar + RightMar)
            End If
            'Identifies the usable width of the document, based on margins and paper size.

            For ShapeLoop = 1 To Shapes
            MsgBox Prompt:="Shape " & ShapeLoop & " width: " & ActiveDocument.Shapes(ShapeLoop).Width
            If ActiveDocument.Shapes(ShapeLoop).Width > WidthAvail Then
            ActiveDocument.Shapes(ShapeLoop).Width = WidthAvail
            End If
            Next ShapeLoop
            'Loops through all shapes in the document. Checks to see if they're too wide, and if they are, resizes them.

            For InLineLoop = 1 To InLines
            MsgBox Prompt:="Inline " & InLineLoop & " width: " & ActiveDocument.InlineShapes(InLineLoop).Width
            If ActiveDocument.InlineShapes(InLineLoop).Width > WidthAvail Then
            ActiveDocument.InlineShapes(InLineLoop).Width = WidthAvail
            End If
            Next InLineLoop
            'Loops through all shapes in the document. Checks to see if they're too wide, and if they are, resizes them.

            End Sub





            share|improve this answer














            You can do this with the following VBA code. It counts the shapes in the document, checks their width against the available space on the page, and resizes if necessary.



            Note that Word has two different collections for Shapes and InlineShapes, hence the two different For loops. Also, it uses a series of If/ElseIf statements to identify the page width based on standard paper sizes. Currently, the only options are letter size in either portrait or landscape, but you can add more ElseIfs for any paper sizes you need.



            Sub ResizePic()
            ' ResizePic Macro
            ' Resizes an image

            Shapes = ActiveDocument.Shapes.Count
            InLines = ActiveDocument.InlineShapes.Count
            'Sets the variables to loop through all shapes in the document, one for shapes and one for inline shapes.


            RightMar = ActiveDocument.PageSetup.RightMargin
            LeftMar = ActiveDocument.PageSetup.LeftMargin
            PaperType = ActiveDocument.PageSetup.PaperSize
            PageLayout = ActiveDocument.PageSetup.Orientation
            'Sets up variables for margin sizes, paper type, and page layout.
            ' This is used to find the usable width of the document, which is the max width for the picture.

            If PaperType = wdPaperLetter And PageLayout = wdPortrait Then
            WidthAvail = InchesToPoints(8.5) - (LeftMar + RightMar)
            ElseIf PaperType = wdPaperLetter And PageLayout = wdLandscape Then
            WidthAvail = InchesToPoints(11) - (LeftMar + RightMar)
            End If
            'Identifies the usable width of the document, based on margins and paper size.

            For ShapeLoop = 1 To Shapes
            MsgBox Prompt:="Shape " & ShapeLoop & " width: " & ActiveDocument.Shapes(ShapeLoop).Width
            If ActiveDocument.Shapes(ShapeLoop).Width > WidthAvail Then
            ActiveDocument.Shapes(ShapeLoop).Width = WidthAvail
            End If
            Next ShapeLoop
            'Loops through all shapes in the document. Checks to see if they're too wide, and if they are, resizes them.

            For InLineLoop = 1 To InLines
            MsgBox Prompt:="Inline " & InLineLoop & " width: " & ActiveDocument.InlineShapes(InLineLoop).Width
            If ActiveDocument.InlineShapes(InLineLoop).Width > WidthAvail Then
            ActiveDocument.InlineShapes(InLineLoop).Width = WidthAvail
            End If
            Next InLineLoop
            'Loops through all shapes in the document. Checks to see if they're too wide, and if they are, resizes them.

            End Sub






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Sep 9 at 2:16









            phuclv

            8,68063688




            8,68063688










            answered Dec 9 '14 at 20:19









            Kelly Tessena Keck

            590217




            590217












            • For me it just looped through the pictures in document and they are gone from my view(. It's a pity as the script looks like a kind of working one.
              – WebComer
              May 28 at 15:29










            • I think the script should check if the shape is a pic, as for a document with just two pics it did find 5 shapes and set their width to zero, so they did gone from the view.
              – WebComer
              May 28 at 15:47




















            • For me it just looped through the pictures in document and they are gone from my view(. It's a pity as the script looks like a kind of working one.
              – WebComer
              May 28 at 15:29










            • I think the script should check if the shape is a pic, as for a document with just two pics it did find 5 shapes and set their width to zero, so they did gone from the view.
              – WebComer
              May 28 at 15:47


















            For me it just looped through the pictures in document and they are gone from my view(. It's a pity as the script looks like a kind of working one.
            – WebComer
            May 28 at 15:29




            For me it just looped through the pictures in document and they are gone from my view(. It's a pity as the script looks like a kind of working one.
            – WebComer
            May 28 at 15:29












            I think the script should check if the shape is a pic, as for a document with just two pics it did find 5 shapes and set their width to zero, so they did gone from the view.
            – WebComer
            May 28 at 15:47






            I think the script should check if the shape is a pic, as for a document with just two pics it did find 5 shapes and set their width to zero, so they did gone from the view.
            – WebComer
            May 28 at 15:47




















             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f570121%2ffitting-images-to-documents-margins-in-a-docx-file%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







            Popular posts from this blog

            Список кардиналов, возведённых папой римским Каликстом III

            Deduzione

            Mysql.sock missing - “Can't connect to local MySQL server through socket”