Word VBA code to Automatically Resize Pics Not Fitting in Page to Fit Page
The need for this came from the image size issue in the automatically generated WordPress blog feed book that I faced recently, which is described in my post: Google Apps Script to Create WordPress Blog Book (or Book parts): Test Versions and Stable Version, https://ravisiyermisc.blogspot.com/2023/07/google-apps-script-to-create-wordpress.html . The extracts below from the post give more details of the issue (from sub-section "Run1 in section "20230710-WordPressFeedToBook version") :
Output file "Blog Feed Book", https://docs.google.com/document/d/1y6e1seiwK6CmFUGHS8ZFJ0L2OS5HJYfpODGTNNUGbVQ/edit?usp=drive_link , seems to be as expected in quick look but for image issue. In particular, it seems that all published posts were included in it. The file is 10.1 MB in size and has 413 pages in default page settings of Portrait, Letter, margins of 1 inch, of Google Docs. The image issue is that some images are bigger than page size. The images are NOT cropped as their size can be reduced manually to show the whole image on the page. The formatting issue that I had faced in previous runs for some posts, seems to have reduced significantly as I could not spot those issues in my quick look.
Downloaded "Blog Feed Book" from Google Docs as a .docx (Microsoft Word) document (shared here as a zip link which has download button to avoid Google Docs opening it automatically: https://drive.google.com/file/d/1rTBwoJ30AkotgnU4nP8fTUkZbOS6UaVM/view?usp=drive_link ). Word (2007) also has the image being too big for page issue but like in Google Docs the full image is present in the document and can be reduced in size manually to get it shown fully on the page. In Word, I was able to add page numbers and generate a Table of Contents for the various posts (file shared here as a zip link which has download button to avoid Google Docs opening it automatically: https://drive.google.com/file/d/14Wo15uxoIGG90O0u1HAt3Y4Z8JJ0_ys-/view?usp=drive_link ). On first look, the TOC seems to be OK but I will need to do a detailed check. The Titles of some posts towards the end of the book, are in a different font from the earlier ones in the TOC, but that itself is not a major issue for me.
Overall, I think this output is good enough for me, though I need to see if I can automatically reduce image sizes to page size (less margin size) in Word. That would fix the image size issue for me.
...
Some more info. about workaround for some images not fitting in Google Docs default page settings of Portrait, Letter and 1 inch margins (left, right, top and bottom): Changing the page settings of Google Docs (File->Page Setup) to pageless (for above "Blog Feed Book" it takes some time and so one has to wait after clicking OK in the dialog windows), seems to reduce sizes of all images in document to fit the view size! So pageless is a solution for viewing all images in the Google Docs document on computer! I have shared a pageless version of above "Blog Feed Book" here: https://docs.google.com/document/d/11clW4sR7i7vZQ6tg_WBZpsFn4D24nqvB6qfeh_-VRBo/edit?usp=drive_link .
--- end extract from post ---
Eventually I want a PDF file to be created from the blog feed book which will become my main blog book shared file. For above Pageless "Blog Feed Book", I used File -> Download -> PDF document to downloaded the pdf on my PC. Then I uploaded it to the same Google Drive shared folder as above file, and the pdf file's shared link is: https://drive.google.com/file/d/1XVhEL2TqZCYTGKL8-CkTxbsq81Ccr6fV/view?usp=drive_link . While the PDF file has most of the images fitting within the page, there are issues with images whose height is such that it exceeds the bottom of the PDF page in which case the part that exceeds the bottom is cut off, and the text that immediately follows the image is sometimes cut off too! Note that PDF page size option is not provided when choosing Download -> PDF document.
As an example of the image height exceeding bottom of PDF page issue, see page 4 of the PDF which has an image from the post titled, "Miscellaneous Notes about using Samsung M21 mobile; Published: 04 Feb 2023". Compare it with the image in the Pageless Google Doc above. Note that the PDF not only has cut off the bottom of the image, but it also has cut off the text, "Mobile Hotspot is 4th" which is part of the text that immediately follows the image in the Google Docs document (I think the term is 'text wrapped around image' but I am not sure). This issue repeats for some other images that exceed the bottom of page of PDF too. For example, pages 6, 7 and 13 (and more such pages).
The part of the image that is cut off is small and so is the text. But still it is an issue.
For PDF, the other option is PDF generation from Microsoft Word. For that, the first task was to explore possibility of automatically resizing images that exceed page writable area (page width less margins) to fit page writeable area. I got some success in this exploration.
I downloaded "Blog Feed Book.docx" associated zip file from link given earlier: https://drive.google.com/file/d/1rTBwoJ30AkotgnU4nP8fTUkZbOS6UaVM/view?usp=drive_link and unzipped it to get the "Blog Feed Book.docx" file whose key stats. are: size: 9,852 KB, 392 pages, 131,225 words and has many images. I opened it in Word 2007 and first added Page number to the footer. Next I ran the VBA macro ResizeBigImagesToFit(), whose code is given later on in this post. It took a little time to finish (few minutes, perhaps only around 1 to 2 minutes but I did not check the time). The main outcome is that it seems to have fixed the image size exceeding writeable area problem!
I zipped the Word document with image size fix and page number in footer: Blog Feed BookwPageNum-ImgIssueFix.docx and uploaded it (Blog Feed BookwPageNum-ImgIssueFix.zip) to the same folder as above "Blog Feed Book.docx" associated zip file. The zip file share link: https://drive.google.com/file/d/1SiltTrnRpWyG1iXMgzddv4CNNqsUO3UY/view?usp=drive_link .
The code in the article: How to Make All Pictures of Same Size in Microsoft Word, https://www.guidingtech.com/make-pictures-same-size-microsoft-word/ , helped me to write the following code. Many thanks to the author and publisher of the article.
--- Start VBA code extract ---
Sub ResizeBigImagesToFit()
'
' ResizeBigImagesToFit Macro
'
'
Dim i As Long
With ActiveDocument
For i = 1 To .InlineShapes.Count
With .InlineShapes(i)
PgWidth = ActiveDocument.PageSetup.PageWidth
PgLeftMargin = ActiveDocument.PageSetup.LeftMargin
PgRightMargin = ActiveDocument.PageSetup.RightMargin
PgWritingAreaWidth = PgWidth - PgLeftMargin - PgRightMargin
PgWritingAreaWidthInches = PgWritingAreaWidth / 72
If .Width > PgWritingAreaWidth Then
.LockAspectRatio = msoCTrue
.Width = PgWritingAreaWidth
End If
PgHeight = ActiveDocument.PageSetup.PageHeight
PgTopMargin = ActiveDocument.PageSetup.TopMargin
PgBottomMargin = ActiveDocument.PageSetup.BottomMargin
PgWritingAreaHeight = PgHeight - PgTopMargin - PgBottomMargin
PgWritingAreaHeightInches = PgWritingAreaHeight / 72
If .Height > PgWritingAreaHeight Then
.LockAspectRatio = msoCTrue
.Height = PgWritingAreaHeight
End If
End With
Next i
End With
End Sub
--- End VBA code extract ---
I also Googled for an easy way to do this automatic resizing of big pics in Google Docs. That would be very useful as it could be done on the main output document of the blog feed book maker program. I did not come come across suitable solutions. One possible solution is the doResize() function described here: https://github.com/tanaikech/ImgApp#doresize . As my needs are little different (change only big images size to fit page writeable area height and width), I would need to understand the code somewhat, at least enough to start trying out changes for my needs. I had a quick look at the code and documentation and felt that I will have to invest a lot of time to understand and modify the code. So I decided to drop this possibility.
Comments
Post a Comment