vba在.docx中丢弃错误,但不在.dotm文件中使用内容控制

发布于 2025-02-08 19:47:44 字数 6283 浏览 0 评论 0原文

嗨,我已经写了一个非常简单的VBA代码...现在我不是VBA专家,但是该代码保存在.dotm文件中,而在模板文件中,所有内容都可以完美地运行。

该文件应自动填充文档标题中的出口中的内容控制字段,但是当我运行特定CC字段的代码时head_client_name 应该设置文本以匹配并用wduppercase大写。这一切都发生在模板宏启用文件中

,但是一旦选择了文件来创建新的文档文件,CC就不会在退出时更新。我做错了什么,或者文件为什么这样做?

就像一面一样,原始模板文档被保存为MS Word 97文件,然后在开发VBA代码期间将其保存到.dotm文件。我不知道这是否会导致问题。

Option Explicit
Private runOnce As Boolean
 
Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean)
 
    Dim i As ContentControl
    Dim n As Integer
   
    
    n = 0
    Set i = ThisDocument.SelectContentControlsByTag("Rev Table").Item(1)
    Select Case ContentControl.Title
    Case "Client Logo"
        If runOnce = True Then
            runOnce = False
            Exit Sub
        Else
            Call HeadLogoUpdate
            runOnce = True
        End If
       
    Case "Project_num"
      'MsgBox "The user selected a file, specifically: " & ContentControl.Range.Text
        For Each ContentControl In ThisDocument.SelectContentControlsByTag("Doc_num")
            ContentControl.LockContents = False
            ContentControl.Range.Text = ThisDocument.SelectContentControlsByTitle("Project_num").Item(1).Range.Text
            ContentControl.LockContents = True
        Next ContentControl
       
        For Each ContentControl In ThisDocument.SelectContentControlsByTitle("Head_Project_num")
            ContentControl.LockContents = False
            ContentControl.Range.Text = ThisDocument.SelectContentControlsByTitle("Project_num").Item(1).Range.Text
            ContentControl.LockContents = True
        Next ContentControl
       
    Case "Client_Name"
        For Each ContentControl In ThisDocument.SelectContentControlsByTitle("Head_Client_Name")
            ContentControl.LockContents = False
            ContentControl.Range.Text = ThisDocument.SelectContentControlsByTitle("Client_Name").Item(1).Range.Text
            ContentControl.Range.Case = wdUpperCase
            ContentControl.LockContents = True
        Next ContentControl
       
    Case "Project_Name"
        For Each ContentControl In ThisDocument.SelectContentControlsByTitle("Head_Project_Name")
            ContentControl.LockContents = False
            ContentControl.Range.Text = ThisDocument.SelectContentControlsByTitle("Project_Name").Item(1).Range.Text
            ContentControl.Range.Case = wdUpperCase
            ContentControl.LockContents = True
        Next ContentControl
       
    Case "Rev. No."       
        For Each ContentControl In ThisDocument.SelectContentControlsByTitle("Head_Rev")
            ContentControl.LockContents = False
            If i.RepeatingSectionItems.Count > 1 Then
                ContentControl.Range.Text = ThisDocument.SelectContentControlsByTitle("Rev. No.").Item(i.RepeatingSectionItems.Count).Range.Text
            Else
                ContentControl.Range.Text = ThisDocument.SelectContentControlsByTitle("Rev. No.").Item(1).Range.Text
            End If
            ContentControl.LockContents = True
        Next ContentControl
    Case "Date"
        'MsgBox i.RepeatingSectionItems.Count
        For Each ContentControl In ThisDocument.SelectContentControlsByTitle("Head_Date")
            ContentControl.LockContents = False
            If i.RepeatingSectionItems.Count > 1 Then
                ContentControl.Range.Text = ThisDocument.SelectContentControlsByTitle("Date").Item(i.RepeatingSectionItems.Count - 1).Range.Text
            Else
                ContentControl.Range.Text = Format(ThisDocument.SelectContentControlsByTitle("Date").Item(1).Range.Text, "yyyy/MM/dd")
            End If
            ContentControl.LockContents = True
        Next ContentControl
       
 
    Case Else
     'The user exited some other content control that we don't care about.
    End Select
    ActiveWindow.ActivePane.View.Type = wdPrintView
lbl_Exit:
  Exit Sub
End Sub
 
 
Sub HeadLogoUpdate()
'
    Dim cc As ContentControl
    Dim CLheight As Long, CLwidth As Long, HCLheight As Long, ScaleHeight As Long
    Dim n As Integer
   
    n = 0 'Integer to count the number of times for each loops
   
'This part sets the scale for the logo in the header
    HCLheight = 0.9  'This is the height of the SGS Bateman logo in the header in cm
    HCLheight = HCLheight / Application.PointsToCentimeters(1)
   
    CLheight = ThisDocument.SelectContentControlsByTitle("Client Logo").Item(1).Range.InlineShapes(1).Height
    CLwidth = ThisDocument.SelectContentControlsByTitle("Client Logo").Item(1).Range.InlineShapes(1).Width
    ScaleHeight = HCLheight * 100 / CLheight
    CLheight = CLheight / Application.PointsToCentimeters(1)
    Dim CLheightDisplay As Long
    CLheightDisplay = Format(CLheight, "#.00")
    CLwidth = CLwidth / Application.PointsToCentimeters(1)
    Dim CLwidthDisplay As Long
    CLwidthDisplay = Format(CLwidth, "#.00")
   
'Select and copy the logo in the first page for pasting in the header
    ActiveDocument.SelectContentControlsByTitle("Client Logo")(1).Range.Select
    Selection.Copy
   
'Run through the document and paste the logo in the content controls the header and scale to fit.
    For Each cc In ActiveDocument.SelectContentControlsByTitle("Head Client Logo")
        n = n + 1
        'Activate the header section
        If ActiveWindow.View.SplitSpecial <> wdPaneNone Then
            ActiveWindow.Panes(2).Close
        End If
        If ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow. _
            ActivePane.View.Type = wdOutlineView Then
            ActiveWindow.ActivePane.View.Type = wdPrintView
        End If
        ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
       
        'Select the content control
        ActiveDocument.SelectContentControlsByTitle("Head Client Logo").Item(n).Range.Select
        Selection.Paste
        ThisDocument.SelectContentControlsByTitle("Head Client Logo").Item(n).Range.InlineShapes(1).LockAspectRatio = msoTrue
       ThisDocument.SelectContentControlsByTitle("Head Client Logo").Item(n).Range.InlineShapes(1).ScaleHeight = ScaleHeight
 
        ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument 'Activate the page view again/main document
    Next cc
   
End Sub

Hi I've written a very simple bit of VBA code... Now I'm no VBA expert by any means, but the code is saved in a .dotm file and while in the template file everything runs perfectly.

The file is suppose to autofill the content control fields on exit that are located in the header of the document, but when I run the code for a particular CC field titled Client_Name the corresponding CC field, Head_Client_Name is supposed to set the text to match and to capitalise the text with wdUpperCase. This all happens in the template macro enabled file

However once the file is selected to create a new document file, the CC doesn't update on exit. What am I doing wrong or why is the file doing this?

Just as a side, the original template document was saved as a MS Word 97 file which was then saved to .dotm file during development of the VBA code. I don't know if this would contribute to the issues.

Option Explicit
Private runOnce As Boolean
 
Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean)
 
    Dim i As ContentControl
    Dim n As Integer
   
    
    n = 0
    Set i = ThisDocument.SelectContentControlsByTag("Rev Table").Item(1)
    Select Case ContentControl.Title
    Case "Client Logo"
        If runOnce = True Then
            runOnce = False
            Exit Sub
        Else
            Call HeadLogoUpdate
            runOnce = True
        End If
       
    Case "Project_num"
      'MsgBox "The user selected a file, specifically: " & ContentControl.Range.Text
        For Each ContentControl In ThisDocument.SelectContentControlsByTag("Doc_num")
            ContentControl.LockContents = False
            ContentControl.Range.Text = ThisDocument.SelectContentControlsByTitle("Project_num").Item(1).Range.Text
            ContentControl.LockContents = True
        Next ContentControl
       
        For Each ContentControl In ThisDocument.SelectContentControlsByTitle("Head_Project_num")
            ContentControl.LockContents = False
            ContentControl.Range.Text = ThisDocument.SelectContentControlsByTitle("Project_num").Item(1).Range.Text
            ContentControl.LockContents = True
        Next ContentControl
       
    Case "Client_Name"
        For Each ContentControl In ThisDocument.SelectContentControlsByTitle("Head_Client_Name")
            ContentControl.LockContents = False
            ContentControl.Range.Text = ThisDocument.SelectContentControlsByTitle("Client_Name").Item(1).Range.Text
            ContentControl.Range.Case = wdUpperCase
            ContentControl.LockContents = True
        Next ContentControl
       
    Case "Project_Name"
        For Each ContentControl In ThisDocument.SelectContentControlsByTitle("Head_Project_Name")
            ContentControl.LockContents = False
            ContentControl.Range.Text = ThisDocument.SelectContentControlsByTitle("Project_Name").Item(1).Range.Text
            ContentControl.Range.Case = wdUpperCase
            ContentControl.LockContents = True
        Next ContentControl
       
    Case "Rev. No."       
        For Each ContentControl In ThisDocument.SelectContentControlsByTitle("Head_Rev")
            ContentControl.LockContents = False
            If i.RepeatingSectionItems.Count > 1 Then
                ContentControl.Range.Text = ThisDocument.SelectContentControlsByTitle("Rev. No.").Item(i.RepeatingSectionItems.Count).Range.Text
            Else
                ContentControl.Range.Text = ThisDocument.SelectContentControlsByTitle("Rev. No.").Item(1).Range.Text
            End If
            ContentControl.LockContents = True
        Next ContentControl
    Case "Date"
        'MsgBox i.RepeatingSectionItems.Count
        For Each ContentControl In ThisDocument.SelectContentControlsByTitle("Head_Date")
            ContentControl.LockContents = False
            If i.RepeatingSectionItems.Count > 1 Then
                ContentControl.Range.Text = ThisDocument.SelectContentControlsByTitle("Date").Item(i.RepeatingSectionItems.Count - 1).Range.Text
            Else
                ContentControl.Range.Text = Format(ThisDocument.SelectContentControlsByTitle("Date").Item(1).Range.Text, "yyyy/MM/dd")
            End If
            ContentControl.LockContents = True
        Next ContentControl
       
 
    Case Else
     'The user exited some other content control that we don't care about.
    End Select
    ActiveWindow.ActivePane.View.Type = wdPrintView
lbl_Exit:
  Exit Sub
End Sub
 
 
Sub HeadLogoUpdate()
'
    Dim cc As ContentControl
    Dim CLheight As Long, CLwidth As Long, HCLheight As Long, ScaleHeight As Long
    Dim n As Integer
   
    n = 0 'Integer to count the number of times for each loops
   
'This part sets the scale for the logo in the header
    HCLheight = 0.9  'This is the height of the SGS Bateman logo in the header in cm
    HCLheight = HCLheight / Application.PointsToCentimeters(1)
   
    CLheight = ThisDocument.SelectContentControlsByTitle("Client Logo").Item(1).Range.InlineShapes(1).Height
    CLwidth = ThisDocument.SelectContentControlsByTitle("Client Logo").Item(1).Range.InlineShapes(1).Width
    ScaleHeight = HCLheight * 100 / CLheight
    CLheight = CLheight / Application.PointsToCentimeters(1)
    Dim CLheightDisplay As Long
    CLheightDisplay = Format(CLheight, "#.00")
    CLwidth = CLwidth / Application.PointsToCentimeters(1)
    Dim CLwidthDisplay As Long
    CLwidthDisplay = Format(CLwidth, "#.00")
   
'Select and copy the logo in the first page for pasting in the header
    ActiveDocument.SelectContentControlsByTitle("Client Logo")(1).Range.Select
    Selection.Copy
   
'Run through the document and paste the logo in the content controls the header and scale to fit.
    For Each cc In ActiveDocument.SelectContentControlsByTitle("Head Client Logo")
        n = n + 1
        'Activate the header section
        If ActiveWindow.View.SplitSpecial <> wdPaneNone Then
            ActiveWindow.Panes(2).Close
        End If
        If ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow. _
            ActivePane.View.Type = wdOutlineView Then
            ActiveWindow.ActivePane.View.Type = wdPrintView
        End If
        ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
       
        'Select the content control
        ActiveDocument.SelectContentControlsByTitle("Head Client Logo").Item(n).Range.Select
        Selection.Paste
        ThisDocument.SelectContentControlsByTitle("Head Client Logo").Item(n).Range.InlineShapes(1).LockAspectRatio = msoTrue
       ThisDocument.SelectContentControlsByTitle("Head Client Logo").Item(n).Range.InlineShapes(1).ScaleHeight = ScaleHeight
 
        ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument 'Activate the page view again/main document
    Next cc
   
End Sub

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

软糯酥胸 2025-02-15 19:47:44

问题是由于您使用“ thisDocument” - 您应该使用“ ActivedOcument”。由于宏位于您的模板中,因此“ thisDocument”是指模板,而不是从中创建的文档 - 是活动文档。

The problem is due to your use of 'ThisDocument' - you should use 'ActiveDocument'. Since the macro is in your template, 'ThisDocument' refers to the template, not to the document created from it - which is the active document.

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