Problem:-
During a recent rebranding exercise at work, all the word documents (saved as RTF files))
I have word documents that are 2, 3 or even 4MB in size.
Looking at the pictures in the documents I noticed that some are scaled down to 48%, 70%, 88%, whatever. If I cut the image out of the document, paste it into Paint.NET, resize it, paste it back into the document and position it as per the original, I can get the size of the documents down to less than 1/10th of the manually fudged one.
I want to programmatically process 1150 Word documents and find pictures in there that are scaled. I then want to pull the pictures out, resize them, and then put them back in replacing the manually added pictures. Saving disk space.
I am having difficulty navigating the Word Object model and finding pictures programmatically.
This web page on MSDN says you can add them, like this
this.Application.Selection.InlineShapes.AddPicture(@"C:\SamplePicture.jpg");
so I thought using the InlineShapes collection might give me access to the collection of pictures in the document.
I have interop declared.
using Microsoft.Office.Interop.Word;
using Microsoft.Office.Interop;
using Microsoft.Office;
and I open the Word application, and document like this (this works)
private void OpenWordApplication()
{
_WordApp = new Microsoft.Office.Interop.Word.Application();
if (chkVisibleWord.CheckState == CheckState.Checked) {
_WordApp.Visible = true;
}
else
{
_WordApp.Visible = false;
}
}
private void OpenTheDocument(string DocumentPath)
{
_WordDoc = _WordApp.Documents.Open(DocumentPath);
changesMade = false;
}
When I try to find eh Pictures in the InlineShapes, I can't seem to get hold of them.
_WordApp.Selection.HomeKey(WdUnits.wdStory);
int picCount = _WordApp.ActiveDocument.InlineShapes.Count;
MessageBox.Show(string.Format("There are {0} images in this document", picCount));
I get a message box saying the count is zero.
NOTE: The application opens the documents in Word just fine. It does other things to the documents depending on what checkboxes I have checked on the form, the issue to me seems to be right down at accessing the InlineShapes collection.
Any pointers. I appreciate your attention so far?
Thanks in advance
Selectionstatements have no bearing on getting a count (the Inline Shapes Collection does not need to have the active document's text selected). If yourUsingstatements and the code establishing_WordAppare correct, you should get an accurate count. Please post additional code includingUsingstatements. - joeschwaint picCount = _WordApp.ActiveDocument.Shapes.Count;return the correct number of images in the document? - joeschwa