简体   繁体   English

VBA 宏计算 MS Word 文档中所有图形占用的区域

[英]VBA macro to calculate area occupied by all figures in MS Word document

(I apologise for my English.) I am preparing textbook for students and I would need to calculate area occupied by all figures in MS Word document. (我为我的英语道歉。)我正在为学生准备教科书,我需要计算 MS Word 文档中所有数字所占的面积。 Simple example with just two figures:只有两个数字的简单示例:

Figure 1: 12 cm x 10 cm = 120 cm2 Figure 2: 8 cm x 10 cm = 80 cm2 TOTAL: 200 cm2图 1:12 cm x 10 cm = 120 cm2 图 2:8 cm x 10 cm = 80 cm2 总计:200 cm2

The total number is enough , it is not necessary to know size of each figure (but can be helpful, if not too complicated).总数就足够了,不必知道每个图形的大小(但如果不是太复杂,可能会有所帮助)。 Many, many years ago I wrote such macro in WordPerfect 6 for DOS — it was quite easy.很多很多年前,我在 WordPerfect 6 for DOS 中编写了这样的宏——这很容易。 But my knowledge of VBA is very, very limited.但是我对 VBA 的了解非常非常有限。 I tried really hard to look at VBA help, but I have no clue what to look for.我非常努力地查看 VBA 帮助,但我不知道要寻找什么。 I also tried Google etc. (and of course StackOverlow) without finding any hint.我还尝试了 Google 等(当然还有 StackOverlow),但没有找到任何提示。

I need that because in our country, the total size (length) of manuscript of the textbook is calculated as AA total = AA text + AA figures , where AA text = Chars in doc/36,000 and AA figures = area/2,300 .我需要那个,因为在我们国家,教科书手稿的总大小(长度)计算为AA total = AA text + AA figures ,其中AA text = Chars in doc/36,000AA figures = area/2,300 AA text can be easily calculated from File, Information, but no easy way for calculation of AA figures. AA 文本可以很容易地从文件、信息中计算出来,但没有简单的方法来计算 AA 数字。

Also, my MS Word is not in English (not sure if such VBA macro works without any problems in different language version of MS Word).另外,我的 MS Word 不是英文的(不确定这样的 VBA 宏在不同语言版本的 MS Word 中是否正常工作)。 Any help welcome.欢迎任何帮助。

Thank you very much in advance.非常感谢您提前。

Jiri吉里

Answer by macropod is quite clear for me, but I still have problem with the macro. macropod的回答对我来说很清楚,但我仍然对宏有问题。 (1) Probably because I do not have any inline figures in my document, the macro ends with error on the following line (the second one). (1) 可能是因为我的文档中没有任何内联数字,宏在下一行(第二行)以错误结束。 The error message is No. 91 " Object variable or With block variable not set ".错误消息为 91 号“ Object 变量或未设置块变量”。 First, I was confused, and came to wrong conclusion: There is missing explicit assignment of zero to Sizes variable somewhere at the beginning of the macro.首先,我很困惑,得出了错误的结论:在宏开头的某处缺少对Sizes变量的显式赋值为零。 And modified macro such way.并以这种方式修改宏。

Sizes = Sizes + PointsToCentimeters(.Height) * PointsToCentimeters(.Width)  

(2) Later I used only the first half of the macro: the first loop ( For Each Shp In.Shapes ). (2) 后来我只使用了宏的前半部分:第一个循环( For Each Shp In.Shapes )。 Now the macro runs without any errors but looks like the macro cannot see any figures and the final message is: The document contains 0 Floatings shapes, whose area totals 0,00cm .现在宏运行没有任何错误,但看起来宏看不到任何数字,最终消息是:文档包含 0 个浮动形状,其面积总计 0,00cm All my figures were inserted as: Insert > Pictures > This Device .我所有的数字都插入为: Insert > Pictures > This Device

I have prepared minimalist Word.docm document (just one page, two figures and macro) for better replication of the problem.我准备了极简的 Word.docm 文档(只有一页,两个数字和宏),以便更好地复制问题。 But even on Help center of Stackoverflow I cannot find if it possible/acceptable to add such file to question.但即使在 Stackoverflow 的帮助中心,我也无法找到是否可以/可接受将此类文件添加到问题中。 I welcome any suggestion what the problem with the macro is.我欢迎任何关于宏问题的建议。

It all depends on what you mean by 'figures'.这完全取决于你所说的“数字”是什么意思。 For example, the following macro gets the combined sizes of all 'images' in the document body:例如,以下宏获取文档正文中所有“图像”的组合大小:

Sub Demo()
Dim Shp As Shape, iShp As InlineShape, Sizes As Single, i As Long
With ActiveDocument
  For Each Shp In .Shapes
    With Shp
      i = i + 1
      Sizes = Sizes + PointsToCentimeters(.Height) * PointsToCentimeters(.Width)
    End With
  Next
  For Each iShp In .InlineShapes
    With Shp
      i = i + 1
      Sizes = Sizes + PointsToCentimeters(.Height) * PointsToCentimeters(.Width)
    End With
  Next
End With
MsgBox "The document contains " & i & " Floating & Inline Shapes, whose area totals " & Format(Sizes, "0.00") & "cm" & Chr(178)
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM