且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

计算机名称在word模板中

更新时间:2023-11-08 23:27:46


Randy Robbins,


您可以参考以下示例在Header中添加计算机名称。

 Sub demo1()

Dim HdrText As String
Dim BoldRange As Range
Dim HdrRange As Range


Set HdrRange = ThisDocument.Sections.Item(1).Headers(wdHeaderFooterPrimary).Range
HdrText ="本文件已在计算机名称上编辑:" &安培; Environ("ComputerName")
HdrRange.Text = HdrText

'要添加一些粗体格式,您可以使用下面的代码。如果你不想要任何格式化,你可以删除它。
Set BoldRange = HdrRange.Words(1)
BoldRange.Expand(wdSentence)
BoldRange.Font.Bold = True

End Sub




输出:



您可以参考以下示例在页脚中添加计算机名称。


Sub demo2()

Dim FtrText As String
Dim BoldRange As Range
Dim FtrRange As Range


设置FtrRange = ThisDocument.Sections.Item(1).Footers(wdHeaderFooterPrimary).Range
FtrText ="此文档在计算机上编辑命名:" &安培; Environ("ComputerName")
FtrRange.Text = FtrText

'要添加一些粗体格式,您可以使用下面的代码。如果你不想要任何格式化,你可以删除它。
Set BoldRange = FtrRange.Words(1)
BoldRange.Expand(wdSentence)
BoldRange.Font.Bold = True

End Sub

输出:




您可以根据自己的要求修改代码。


问候


Deepak


hi, I want to be able to identify where a doctor is dictating a report from. since the doctors move but the computers do not. i think this could be done if i can put the computer name in the word template they are using. Anywhere in the document will work. preferably at the end or a report footer/header.

I have heard of similar questions but cant find the answer i am looking for.

thanks

Hi Randy Robbins,

you can refer example below to add computer name in Header.

Sub demo1()

Dim HdrText As String
Dim BoldRange As Range
Dim HdrRange As Range


    Set HdrRange = ThisDocument.Sections.Item(1).Headers(wdHeaderFooterPrimary).Range
    HdrText = "This Document was edited on Computer Named : " & Environ("ComputerName")
    HdrRange.Text = HdrText

    'To add some bold formatting you can use code below. you can remove it if you dont want any formatting.
    Set BoldRange = HdrRange.Words(1)
    BoldRange.Expand (wdSentence)
    BoldRange.Font.Bold = True
    
End Sub


Output:

you can refer example below to add computer name in Footer.

Sub demo2()

Dim FtrText As String
Dim BoldRange As Range
Dim FtrRange As Range


    Set FtrRange = ThisDocument.Sections.Item(1).Footers(wdHeaderFooterPrimary).Range
    FtrText = "This Document was edited on Computer Named : " & Environ("ComputerName")
    FtrRange.Text = FtrText

    'To add some bold formatting you can use code below. you can remove it if you dont want any formatting.
    Set BoldRange = FtrRange.Words(1)
    BoldRange.Expand (wdSentence)
    BoldRange.Font.Bold = True
    
End Sub

Output:

you can modify code as per your requirement.

Regards

Deepak