【问题标题】:VBA Automatically add autocorrection entries in word from an excel listVBA自动从excel列表中添加word中的自动更正条目
【发布时间】:2018-01-16 16:08:35
【问题描述】:

我正在尝试将大量自动更正条目添加到加载 excel 文件/工作表数据的 WORD 中。 这实际上不是为了自动更正目的,而是为了使用快捷方式来引入完整的段落以更快地编写。

首先我尝试过:

Sub autocorrectlist() 
'TRYING OUT IF using variables works for a new autocorrect entry.
Dim myString1 As String
Dim myString2 As String
myString1 = "BBBB"
myString2 = "BBBB works works works"
AutoCorrect.Entries.Add Name:=myString1, Value:=myString2

这行得通。在 Word 中写 BBBB 时,用另一个表达式代替。 让我们尝试从 excel 文件中读取值。 这里的代码很长,但它基本上包括打开一个 excel 文件并读取 excel 表的条目,一列是所谓的快捷方式(必须替换的文本)的条目,另一列是替换文本)

'lets create a huge list of modifiers
Dim i As Integer 'counter

'Read Excel File Megaclause
Dim oXL As Excel.Application
Dim oWB As Excel.Workbook
Dim oSheet As Excel.Worksheet
Dim oRng As Excel.range
Dim ExcelWasNotRunning As Boolean
Dim WorkbookToWorkOn As String

'specify the workbook to work on
WorkbookToWorkOn = "C:\Users\JF30443\Desktop\WORK\EXCEL\megaclause7.xlsm"

'If Excel is running, get a handle on it; otherwise start a new instance of Excel
On Error Resume Next
Set oXL = GetObject(, "Excel.Application")

If Err Then
     ExcelWasNotRunning = True
     Set oXL = New Excel.Application
End If

On Error GoTo Err_Handler
oXL.Visible = True

'Open the workbook
 Set oWB = oXL.Workbooks.Open(FileName:=WorkbookToWorkOn)

'Process each of the spreadsheets in the workbook
Dim mySht As Worksheet
Set mySht = oWB.Worksheets("sc6")
Dim lastRow As Integer
lastRow = mySht.Cells(mySht.Rows.Count, "A").End(xlUp).Row
Dim myName As String
Dim myAuto As String

'reading the values from the sheet
For i = 1 To lastRow
myName = mySht.Cells(i, 1).Value
myAuto = mySht.Cells(i, 2).Value
If myName <> "" And myAuto <> "" Then
    MsgBox ("nr:" & i & "myName:" & myName & "//myauto:" & myAuto)
    'note here: actually it read correctly the two first values of the SC6 sheet
    'Since the values displayed by the msgbox are correct
    'the following line gives an error
    Application.AutoCorrect.Entries.Add Name:=myName, Value:=myAuto
    'the error being:
    'AutoCorrect cannot replace text which contains a space character.
End If
Next


If ExcelWasNotRunning Then
oXL.Quit
End If

'Make sure you release object references.
Set oRng = Nothing
Set oSheet = Nothing
Set oWB = Nothing
Set oXL = Nothing

'quit
Exit Sub

Err_Handler:
MsgBox WorkbookToWorkOn & " caused a problem. " & Err.Description, vbCritical, _
       "Error: " & Err.Number
 If ExcelWasNotRunning Then
   oXL.Quit
End If

End Sub

正如行间代码中所述,代码读取工作表是因为 msgbox 正确显示了名为“SC3”的工作表的前两个值,但在它给出错误之后立即显示。 我从互联网上部分复制了这段代码,但我认为最困难的部分(从 excel 中的 word 读取)工作,然后我无法添加条目。

更奇怪的是,上面粘贴的代码的第一部分(带有“BBBB”的那部分)可以正常工作,这与循环中的方法基本相同。

我在网上搜索了信息,但没有找到与 taht 错误相关的任何信息。

欢迎帮助

谢谢。

【问题讨论】:

  • 错误消息说明了一切:“'自动更正无法替换包含空格字符的文本”。这听起来好像 myName 包含一个空格(BBBBBB 没有)。用于自动更正的术语可能不包含空格。如果您没有看到空格,请检查条目的开头和结尾。注意:构建块(自动图文集)可能是比自动更正更好的方法来存储您想要快速插入的内容。您可以存储格式、整个段落、图形...输入标识符,然后按 F3。
  • 当然,天哪。我把 trim() 函数放在了错误的地方。不幸的是,在任何情况下,自动更正替代文本都不能大于 255 个字符...
  • 那么你绝对应该看看 Building Blocks/AutoText 方法。这没有大小限制。

标签: vba excel autocomplete ms-word shortcut


【解决方案1】:

问题是工作表名称中的初始空格

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-14
    • 2020-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多