在文档标题中添加不带 .doc 扩展名的文件名字段

发布于 2024-09-27 13:36:31 字数 143 浏览 0 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

情独悲 2024-10-04 13:36:31

你的感觉很对。

插入>快速零件>领域> FileName 是可行的方法,但正如您从下面的屏幕截图中看到的,您无法选择打开或关闭文件扩展名。
替代文字
显示或不显示(莎士比亚风格)扩展名完全取决于 Windows 资源管理器设置以显示或隐藏已知的文件扩展名。因此,要么更改该设置,要么需要一些代码。

一个非常简单的宏如下:

Sub InsertCurrentFileName()
    Selection.InsertBefore Text:=Left(ActiveDocument.Name, Len(ActiveDocument.Name) - 4)
End Sub

它所做的只是去除“文件名字符串”的最后 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.
alt text
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:

Sub InsertCurrentFileName()
    Selection.InsertBefore Text:=Left(ActiveDocument.Name, Len(ActiveDocument.Name) - 4)
End Sub

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...):

迷爱 2024-10-04 13:36:31

添加[标题]而不是文件名
并可能添加一些列出的其他文档属性

add [Title] Instead of filename
and maybe add some of the Other Document Properties Listed

緦唸λ蓇 2024-10-04 13:36:31

创建一个宏,例如:

Dim f As String
f = Dir(ActiveDocument.FullName)
intPos = InStr(1, f, ".")
If intPos > 0 Then
    f = Left(f, intPos - 1)
End If
doc.Variables("BaseFileName").Value = f

插入一个字段,例如:

插入 DocVariable 字段

完成。

Create a macro something like:

Dim f As String
f = Dir(ActiveDocument.FullName)
intPos = InStr(1, f, ".")
If intPos > 0 Then
    f = Left(f, intPos - 1)
End If
doc.Variables("BaseFileName").Value = f

Insert a Field like:

Insert DocVariable Field

Done.

执笏见 2024-10-04 13:36:31

事实上,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 自定义属性并为其分配您的值:

  • 在 MS Word 中打开一个空白文档。
  • 将其另存为“MyTempReport.docx”。
  • 在 MS Word 菜单中,打开 MyTempReport.docx > >文件[选项卡]>信息>属性:点击>高级属性...>>自定义 [选项卡] ...
  • 输入“名称”:“FileBaseName”; “值”:“我的临时报告”。

在文档中插入自定义文档属性 FileBaseName

  • MS Word 菜单 >插入[选项卡]>文本[组]>快速零件>字段...
  • 从“字段名称”中选择“DocProperty”。
  • 从“字段属性”> “属性”:选择“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:

  • Open a blank document in MS Word.
  • Save it as "MyTempReport.docx".
  • In MS Word Menu with MyTempReport.docx open > File [tab] > Info > Properties: Click > Advanced Properties ... > Custom [tab] ...
  • Type in "Name": "FileBaseName"; "Value": "MyTempReport".

Insert the custom document property, FileBaseName, in your document:

  • MS Word menu > Insert [tab] > Text [group] > Quick Parts > Field ...
  • From "Field Names" choose "DocProperty".
  • From "Field Properties" > "Property": Choose "FileBaseName" > OK.

You should now have "MyTempReport" inserted as a field in your document.

Limitations:

  • If you rename your file this won't be reflected in your FileBaseName field. Instead you'll have to manually change the FileBaseName property from Advanced Properties ... (as above).
  • You'll have to create this custom property in every particular document you want it in.
淡莣 2024-10-04 13:36:31

当且仅当文件名长度不变时,您可以将扩展名的字体更改为“隐藏”:

  1. 要插入字段,请转到插入»快速部件(文本)»字段...,然后从列表中选择“文件名”(在“文档信息”类别中),并确保选中更新期间保留格式处的复选标记(这会附加选项 \* MERGEFORMAT< /code> 字段代码;如果缺少此字段,则每次更新字段值时以下隐藏字体效果都会消失);
  2. 选择显示的文件扩展名(包括 .);
  3. 操作到Home » Font(左下角箭头),然后勾选隐藏复选框;

我将此方法用于如下文件名,其中只有修订代码可能会更改:

  • ID_REV_some_descriptive_name.doc?,其中 REV 只是一个字母,如 ABC等;

If, and only if, the file name will not change in length, you may change the font of the extension to be "Hidden":

  1. to insert the field go to Insert » Quick Parts (Text) » Field…, then select "FileName" from the list (in category "Document Information") and ensure the check mark at Preserve formatting during updates is ticked (this appends the option \* MERGEFORMAT to the field codes; when this is missing, the following Hidden font effect will disappear every time the field value becomes updated);
  2. select the shown file name extension (including the .);
  3. manoeuvre to Home » Font (arrow at lower left corner), then tick the check box Hidden;

I am using this method for files names like the following, where only the revision code may change:

  • ID_REV_some_descriptive_name.doc?, where REV is just a single letter like A, B, C, etc.;
浮生未歇 2024-10-04 13:36:31

如上图所示插入文件名字段并另存为文件名。在打印预览模式下查看并返回到编辑模式:现在应该显示文件名而不是文档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'.

故事与诗 2024-10-04 13:36:31

只需在 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. :)

春庭雪 2024-10-04 13:36:31

为了让文档名称在字段中自动更新,您需要向宏中添加更多代码。每次打开文件时,该宏都会检查文件名,并更新您希望在其中显示文件名的所有字段。

Sub AutoOpen()
' AutoUpdate Macro which creates filename without extension and updates fields
Dim aStory As Range
Dim aField As Field
Dim fname As String
        fname = ActiveDocument.Name
        fname = Left(fname, Len(fname) - 5) 'removes the last five characters from the filename (.docm)

            With ActiveDocument
                .Variables("fname").Value = fname
            End With

       For Each aStory In ActiveDocument.StoryRanges 'updates all fields in the document when you open the file

            For Each aField In aStory.Fields
             aField.Update
          Next aField
       Next aStory

    End Sub

创建此宏后,只需返回文档即可:

  • 在“插入”选项卡中选择“快速部件”,然后单击“字段”。
    在“插入”选项卡中选择“快速零件”,然后单击“字段”。

  • 在“字段”选项中,选择 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.

Sub AutoOpen()
' AutoUpdate Macro which creates filename without extension and updates fields
Dim aStory As Range
Dim aField As Field
Dim fname As String
        fname = ActiveDocument.Name
        fname = Left(fname, Len(fname) - 5) 'removes the last five characters from the filename (.docm)

            With ActiveDocument
                .Variables("fname").Value = fname
            End With

       For Each aStory In ActiveDocument.StoryRanges 'updates all fields in the document when you open the file

            For Each aField In aStory.Fields
             aField.Update
          Next aField
       Next aStory

    End Sub

After you create this macro, simply go back to the document:

  • select 'Quick Parts' in the 'Insert' tab, and click on 'Field.'
    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.
    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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文