在文档标题中添加不带 .doc 扩展名的文件名字段
在 Office 2007 中工作,我想在文档标题中添加文件名字段。该文档稍后将成为 PDF,因此我不需要扩展名。
我玩过插入 ->快速零件 ->场上,无济于事。我有一种直觉,它需要一个公式...
如果您能提供帮助,请提前致谢。
Working in Office 2007, I'd like to add a filename field in my document header. The document will later be a PDF, so I don't want the extension.
I've played with Insert -> QuickParts -> Field, to no avail. I've got a gut feeling that it requires a formula...
Thanks in advance if you can help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
你的感觉很对。
插入>快速零件>领域> FileName
是可行的方法,但正如您从下面的屏幕截图中看到的,您无法选择打开或关闭文件扩展名。显示或不显示(莎士比亚风格)扩展名完全取决于 Windows 资源管理器设置以显示或隐藏已知的文件扩展名。因此,要么更改该设置,要么需要一些代码。
一个非常简单的宏如下:
它所做的只是去除“文件名字符串”的最后 4 个字符,例如“.doc” - 如果您保护“.docx”则“.”将被保留。此外,此宏将运行一次,当文件名更改时,您需要再次运行它。
也许您可以更多地解释一下您想要通过在文档标题中包含文件名来实现什么?您是否尝试在转换过程中使用文档标题中的文件名来设置某些 PDF 属性?为什么不使用文档标题?您稍后需要 PDF 中的原始文件名 - 为什么?
另外两个页面可以帮助您解决问题(都依赖于宏...):
Your feeling is quite right.
Insert > QuickParts > Field > FileName
is the way to go, but as you see from the screenshot below, you do not have the option to turn the file extension on or off.To show or not to show (Shakespearean style) the extension is purely up to the Windows Explorer setting to show or hide known file extensions. So, either you change that setting or you need some code.
A very simple Macro would be the following:
What it does is simply strip the last 4 characters of the "Filename string" e.g. ".doc" - if you safe a ".docx" the "." would be preserved. Also this Macro would run once and you would need to run it again when the filename changes.
Maybe you could explain some more what you want to achieve with having the filename in the document header? Are you trying to use the filename in the document header to set some PDF property during conversion? Why not use the Document Title? Do you need the original filename in the PDF later - why?
Two more pages to help you with your problem (both relying on Macros...):
添加[标题]而不是文件名
并可能添加一些列出的其他文档属性
add [Title] Instead of filename
and maybe add some of the Other Document Properties Listed
创建一个宏,例如:
插入一个字段,例如:
完成。
Create a macro something like:
Insert a Field like:
Done.
事实上,FileName 字段没有“仅基本名称”选项。
长期:功能请求
对于长期功能,我已向 Microsoft 提出了功能请求:
https ://office365.uservoice.com/forums/264636-general/suggestions/13860672-for-word-create-a-basename-only-field-option-for
我邀请您和其他访问这篇文章的人,对该建议进行投票。
短期解决方法
宏(VBA 代码)
正如 @DennisG 所建议的那样,编写宏可能是最方便的解决方法。
自定义属性
但是您可能希望避免将任何宏与您的 Word 文档关联,以避免安全问题(例如,如果您分发文档)。因此,另一种解决方法是使用硬编码值“MyDocumentBaseName”创建自定义属性
FileBaseName
:创建 FileBaseName 自定义属性并为其分配您的值:
在文档中插入自定义文档属性
FileBaseName
:您现在应该已将“MyTempReport”作为字段插入到文档中。
限制:
FileBaseName
字段中。相反,您必须从“高级属性...”手动更改FileBaseName
属性(如上所述)。Indeed there is no "basename only" option for the FileName field.
Long term: Feature request
For the long term I've made a feature request with Microsoft at:
https://office365.uservoice.com/forums/264636-general/suggestions/13860672-for-word-create-a-basename-only-field-option-for
I invite you, and others visiting this post, to up-vote that suggestion.
Short term workarounds
Macro (VBA code)
Writing a macro, as @DennisG has suggested, is probably the most convenient work around.
Custom property
But you may want to avoid associating any macro with your word document to avoid security issues (e.g. if you distribute your document). So another workaround is to create custom property
FileBaseName
with a hard coded value "MyDocumentBaseName":Create the FileBaseName custom property and assign it your value:
Insert the custom document property,
FileBaseName
, in your document:You should now have "MyTempReport" inserted as a field in your document.
Limitations:
FileBaseName
field. Instead you'll have to manually change theFileBaseName
property from Advanced Properties ... (as above).当且仅当文件名长度不变时,您可以将扩展名的字体更改为“隐藏”:
\* MERGEFORMAT< /code> 字段代码;如果缺少此字段,则每次更新字段值时以下隐藏字体效果都会消失);
.
);我将此方法用于如下文件名,其中只有修订代码可能会更改:
ID_REV_some_descriptive_name.doc?
,其中REV
只是一个字母,如A
、B
、C
等;If, and only if, the file name will not change in length, you may change the font of the extension to be "Hidden":
\* MERGEFORMAT
to the field codes; when this is missing, the following Hidden font effect will disappear every time the field value becomes updated);.
);I am using this method for files names like the following, where only the revision code may change:
ID_REV_some_descriptive_name.doc?
, whereREV
is just a single letter likeA
,B
,C
, etc.;如上图所示插入文件名字段并另存为文件名。在打印预览模式下查看并返回到编辑模式:现在应该显示文件名而不是文档n
一旦获得文件名,您就可以使用不同的颜色格式化不同的部分:因此将.扩展名的颜色更改为文档的背景颜色(可能是白色)。
我在页脚中执行了此操作,以便 pdf 版本不会显示“.docx”。
Insert the file name field as shown in the diagram above and save as filename. View in print preview mode and return to edit mode: filename should now show instead of Documentn
Once you have the file name you can format different parts with different colors: so change the color of the .extension to the background color of the document (white probably).
I did this in a footer so that the pdf version didn't show '.docx'.
只需在 Windows 资源管理器中隐藏文件扩展名即可。之后重新打开文档。然后导出。该名称不会有扩展名。 :)
Just hide the file name extensions in Windows Explorer. After that reopen the document. Then export it. The name will not have the extension. :)
为了让文档名称在字段中自动更新,您需要向宏中添加更多代码。每次打开文件时,该宏都会检查文件名,并更新您希望在其中显示文件名的所有字段。
创建此宏后,只需返回文档即可:
在“插入”选项卡中选择“快速部件”,然后单击“字段”。
在“字段”选项中,选择 DocVariable,然后在“字段属性”中输入 fname,然后单击“确定”。
属性中输入 fname,然后单击确定。">
然后它将插入您的文件名称(无扩展名)放入您的文档中,每次打开文件时都会更新,并显示新的文件名。
In order to have the document name automatically update in the fields, you need to add a little more code to your macro. This macro will check what the filename is every time you open the file and will update all the fields in which you want the filename to be seen.
After you create this macro, simply go back to the document:
select 'Quick Parts' in the 'Insert' tab, and click on 'Field.'
In the Field options, select DocVariable, and type in fname into Field Properties and then click OK.
It will then insert your file name, sans extension, into your doc, updating every time you open the file, and display the new filename.