简体   繁体   English

将现有样式的创建代码添加到 VBA Word

[英]Adding creating code of existing style to VBA Word

I would like to make a Word template.我想做一个Word模板。 I would also like to have use a VBA macro to edit styles of my documents.我还想使用 VBA 宏来编辑文档的样式。 I struggle with the styling.我在造型上挣扎。 I would like to check if the style already exists and if not then create it and later apply it.我想检查样式是否已经存在,如果不存在,则创建它并稍后应用它。 However, I struggle a lot with defining of the new styles.但是,我在定义新样式时遇到了很多困难。 I would like to copy a style from different document and place a creating code in my VBA code.我想从不同的文档中复制一个样式并在我的 VBA 代码中放置一个创建代码。 Is there faster way than just recreate the old one with record?有没有比用记录重新创建旧的更快的方法? I'm not sure if I would be able to recreate the style to all details and I have also a lot of styles to rewrite.我不确定我是否能够重新创建所有细节的样式,而且我还有很多样式要重写。 I tried to find any clues or answers to my problems but can't find any.我试图找到我的问题的任何线索或答案,但找不到任何线索。 I would be glad for clues or answers and if more information will be needed, I will of course provide all I can.我很乐意提供线索或答案,如果需要更多信息,我当然会尽我所能。

You can check whether a Style exists and, if not, create it via code like:您可以检查样式是否存在,如果不存在,则通过以下代码创建它:

With ActiveDocument
  .Styles.Add Name:="MyStyle", Type:=wdStyleTypeParagraph
  On Error GoTo 0
  With .Styles("MyStyle").ParagraphFormat
    .Alignment = wdAlignParagraphCenter
    .SpaceAfter = 0
    .SpaceBefore = 0
  End With
End With

Better still would be to use the Organizer to transfer the Styles from your source file.更好的是使用管理器从源文件中传输样式。 See: https://docs.microsoft.com/en-us/office/vba/api/word.application.organizercopy请参阅: https ://docs.microsoft.com/en-us/office/vba/api/word.application.organizercopy

For a stand-alone Style copier that uses the Organizer, see: https://www.msofficeforums.com/28497-post34.html .有关使用 Organizer 的独立 Style 复印机,请参阅: https ://www.msofficeforums.com/28497-post34.html。

And, if you want to update existing document Styles, simply ensure the updates are in the template upon which the documents are based and apply the 'Automatically update document styles' property (see under Developer|Document Template>Templates).而且,如果您想更新现有文档样式,只需确保更新位于文档所基于的模板中并应用“自动更新文档样式”属性(请参阅开发人员|文档模板>模板)。 This too can be automated.这也可以自动化。

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

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