简体   繁体   English

用于更改文档属性 Word 的 VBA

[英]VBA for changing doc properties Word

I am trying to create a code that need to change a custom document property in a word document.我正在尝试创建一个需要更改 word 文档中自定义文档属性的代码。 This custom document property is the information that must be inserted in the document but sometimes need to be changed as requested.此自定义文档属性是必须插入文档但有时需要根据要求进行更改的信息。 So far I have created the code for inserting the custom document property, it works fine, see below.到目前为止,我已经创建了用于插入自定义文档属性的代码,它工作正常,见下文。

Sub AddCustomDocProperty()

Selection.Fields.Add _
    Range:=Selection.Range, _
    Type:=wdFieldEmpty, _
    Text:="DOCPROPERTY  ""ProjectName"" ", _
    PreserveFormatting:=False
    
End Sub

The custom document property is named as ProjectName which will be linked with a specific field where the user will input the project name, eg New Screwdriver自定义文档属性被命名为 ProjectName,它将与用户将输入项目名称的特定字段链接,例如新螺丝刀

Let's say the custom document property shows New Screwdriver and I need to change to New Screwdriver 2021.假设自定义文档属性显示新螺丝刀,我需要更改为新螺丝刀 2021。

How can I write a code that change the docproperty based on the new input given in Project name field?如何编写基于项目名称字段中给出的新输入更改文档属性的代码?

I tried to find something in the forum but I did not find anything.我试图在论坛中找到一些东西,但我没有找到任何东西。

Thanks in advance.提前致谢。

A user can't easily change the value of such a field by simply typing.用户无法通过简单的输入轻松更改此类字段的值。 Whatever additional text they type will fall outside the field.他们输入的任何其他文本都将超出该字段。 Then it's difficult for you to retrieve with VBA.那么你就很难用 VBA 检索了。 Consider using a content control, formfield, or even a bookmarked table cell for their input instead.考虑使用内容控件、表单域,甚至是带有书签的表格单元格作为输入。

But to answer your question, this will update a custom document property:但是要回答您的问题,这将更新自定义文档属性:

Sub SetProp()
    ActiveDocument.CustomDocumentProperties("ProjectName").value = "New Screwdriver 2021"
End Sub

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

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