标签: word-vba

使用VBA,如何在Windows资源管理器中检查是否隐藏了已知文件类型的文件扩展名?

无需打开Windows资源管理器 我想从Word VBA检查这个.

谢谢.

编辑:

此代码现在有效:

Set WshShell = CreateObject("WScript.Shell")

If WshShell.RegRead("HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced\HideFileExt") = 0 Then
    MsgBox Prompt:="In Windows Explorer, set the folder options to hide file extensions of known file types." _
        & vbCrLf & vbCrLf & " This prevents the file extension from appearing as part of the document number in" _
        & "the document body and page headers.", Buttons:=vbOKOnly + vbCritical, Title:="Critical"
End If
Run Code Online (Sandbox Code Playgroud)

file-extension vba windows-explorer word-vba

1
推荐指数
1
解决办法
1787
查看次数

vba中For循环的循环行为不一致

我已经编写了一个用于删除MS Word 2010页面的宏,如下所示 -

Sub deleteAlternatePages()
    CurrentPage = Selection.Information(wdActiveEndPageNumber)
    TotalPages = Selection.Information(wdNumberOfPagesInDocument)

    maxLoop = TotalPages - 1

    Dim loopCtr As Integer
    loopCtr = (maxLoop / 2)

    For i = 1 To loopCtr
        boolDelete = Selection.Information(wdActiveEndPageNumber)
        Call deleteOnePage
        CurPage = Selection.Information(wdActiveEndPageNumber)
        Selection.GoTo What:=wdGoToPage, Which:=wdGoToNext, Count:=1
    Next

End Sub
Run Code Online (Sandbox Code Playgroud)

从word运行宏时,循环无法正常工作.但是,当我调试代码时,它可以正常工作.

vba loops ms-word word-vba

1
推荐指数
1
解决办法
482
查看次数

使用宏从Word 2007中的功能区中的下拉列表中获取所选项目

我看到以下问题解释了如何从下拉列表中获取所选项目: -

http://social.msdn.microsoft.com/Forums/en-US/vsto/thread/d1cf7b3e-68cf-4b82-b806-a3431acde3b6/

上面的线程建议事先获得项目的哈希表,在下拉列表的onAction()中缓存选定的id,并使用该选定的id从按钮的onAction()中的哈希表中查找项目.

但是,就我而言,我从数据库中填充了功能区XML.这个功能区XML显然有用于下拉列表的XML,我使用宏来与功能区中的下拉列表和其他控件进行交互.我不确定如何预先设置一个可以由宏使用的集合,类似于上述线程中描述的方法.

ribbon ms-word word-vba

1
推荐指数
1
解决办法
9542
查看次数

在word自动化中设置列宽会引发异常

我正在使用Word自动化构建一个表,我需要一个非常窄的列,但每次我使用Microsoft.Office.Interop.Word.Column.SetWidth方法来设置宽度时,它会抛出"超出范围的值Exception"如果我把它变成一个更大的值,它就可以工作但是它必须尽可能小

有人建议将间距和填充设置为零,我做了,但它仍然抛出异常

      table.LeftPadding = 0;
      table.RightPadding = 0;
      table.Spacing = 0;
      table.Columns[4].SetWidth(9f, WdRulerStyle.wdAdjustNone);
Run Code Online (Sandbox Code Playgroud)

c# vsto word-automation word-vba c#-4.0

1
推荐指数
1
解决办法
7625
查看次数

如何使用vba在userform中声明自定义事件

我有userform收集一些用户输入.现在我要做的是,当单击"确定"按钮时,声明从userform抛出一些事件.我是vba的新手,所以我不知道怎么做.任何代码或指向教程的链接将不胜感激.

Load UserForm1
UserForm1.Show
//here I want to capture UserForm1 OK button's click event and read the data
Run Code Online (Sandbox Code Playgroud)

vba ms-word word-vba

1
推荐指数
1
解决办法
2789
查看次数

标题2文本与excel中完全相同的文本:VBA不匹配

我正在创建一个项目,让用户在Excel中创建任务列表,然后将用户创建的任务文本与预制单词文档中的第二个标题文本(标题2)进行比较.我能够获取第二个头文本并将其保存到一个数组,然后获取用户任务列表并将其保存在一个数组中.然后,我尝试使用该函数查看程序内的任务文本(第二个标题)是否在用户任务列表中

    If IsError(Application.Match(ProgArray(x), TaskArray, 0)) Then
        'Find within word document and highlight red
    End if
Run Code Online (Sandbox Code Playgroud)

我得到的问题是,这总是返回错误,因为出于某种原因,即使内置的监视屏幕调试器另有说明,word文档中的文本也不等于excel表中的完全相同的文本.

起初,我使用比较文本软件来确定来自单词的标题文本可能实际上已经复制了额外的行.解释图片:这里的例子

但后来我尝试修剪,并检查标题文本是否有vbNewLine

    If Right$(StrFound, 2) = vbCrLf Or Right$(StrFound, 2) = vbNewLine Then
Run Code Online (Sandbox Code Playgroud)

也没有用,因为这个if语句从未被触发过.

我的问题是,从一个word文档中获取文本还会提取一些我不知道的隐藏价值,如果是这样的话有什么方法吗?谢谢你,对不起文本之墙.

最后这是我的完整代码:(它不漂亮,因为我现在只是为了功能)

'Sub CheckHeader()
Dim blnFound As Boolean
Dim StrFound As String
Dim x As Integer, y As Integer, z As Integer
Dim TaskTotal As Integer
Dim ProgArray(149) As String
Dim TaskArray() As String
Dim NotInArray() As String
Dim NotInProg() As String
Dim appWd As Object
Dim TaskSheet As Worksheet …
Run Code Online (Sandbox Code Playgroud)

vba compiler-errors ms-word excel-vba word-vba

1
推荐指数
1
解决办法
581
查看次数

如何使用Word VBA将多选列表框值返回到句子中?

我在userform上创建了一个多选列表框.列表框中有9个项目.如何将这些选定的项目收集到一个句子中?

列表框包含返回支票的原因.列表框中的项目是较长字符串的标识符或占位符,因此选择"unsigned"会创建返回的字符串,"检查未签名".

用户可以选择几个原因,因此根据选择,我需要格式为"x,y和z"或"y和z"或"z"的句子结构.(例如:"支票未签署,支票已过期,支票为第三方支票.")

似乎需要从选择中创建一个数组,选择计数,然后用"If then"语句来创建句子,但我很难过.我可以计算所选项目,如果只选择了1项,我可以创建句子,但是复合句子让我感到困惑.

vba ms-word word-vba

1
推荐指数
1
解决办法
3万
查看次数

VBA:格式化MS Word文本

我正在尝试格式化多个单词的文本.到目前为止,下面的代码只允许我格式化一个单词的字体.我需要添加/删除什么才能使我输入的单词格式化?

干杯!

Sub FnFindAndFormat()

    Dim objWord
    Dim objDoc
    Dim intParaCount
    Dim objParagraph
    Set objWord = CreateObject("Word.Application")
    Set objDoc = objWord.Documents.Open("C:\USERPATH")
    objWord.Visible = True
    intParaCount = objDoc.Paragraphs.Count

    Set objParagraph = objDoc.Paragraphs(1).range
    objParagraph.Find.Text = "deal"

    Do
        objParagraph.Find.Execute
        If objParagraph.Find.Found Then
            objParagraph.Font.Name = "Times New Roman"
            objParagraph.Font.Size = 20
            objParagraph.Font.Bold = True
            objParagraph.Font.Color = RGB(200, 200, 0)
        End If


    Loop While objParagraph.Find.Found

End Sub
Run Code Online (Sandbox Code Playgroud)

excel vba ms-word excel-vba word-vba

1
推荐指数
1
解决办法
5703
查看次数

在Word中更改自定义文档属性

我正在尝试更改文档的属性,然后再保存它,但是下面的属性均未添加。

我该如何解决这个问题?谢谢。

'**
 ' Set the required properties for this document
 '*
Function SetProperties(ByVal DocumentName As String, _
                          ByRef tempDoc As Document) As Boolean

    Call UpdateCustomDocumentProperty(tempDoc, "Title", DocumentName & ".pdf", 4)
    Call UpdateCustomDocumentProperty(tempDoc, "Subject", "New Starter Guides", 4)
    Call UpdateCustomDocumentProperty(tempDoc, "Keywords", "new starters, guide, help", 4)

    SetProperties = True

End Function

'**
 ' Update a single custom value
 '*
Function UpdateCustomDocumentProperty(ByRef doc As Document, _
                                      ByVal propertyName As String, _
                                      ByVal propertyValue As Variant, _
                                      ByVal propertyType As Office.MsoDocProperties)

    On …
Run Code Online (Sandbox Code Playgroud)

vba word-vba word-2010

1
推荐指数
1
解决办法
9369
查看次数

VBA:崩溃后如何关闭Excel.Application

我的Word宏使用以下代码打开和访问Excel工作簿:

Dim objExcel As New Excel.Application
Dim exWb As Excel.Workbook

Set exWb = objExcel.Workbooks.Open("C:\Folder\Filename.xls")   

 ' Main procedure

exWb.Close
Set exWb = Nothing

objExcel.Quit
Set objExcel = Nothing
Run Code Online (Sandbox Code Playgroud)

由于我仍在调试中,因此宏通常在到达“关闭”和“退出”命令之前就停止了,从而使Excel实例和工作簿在后台处于打开状态。

有时我可以通过打开任务管理器并结束任务来关闭它们,但是我怀疑那是最好的解决方案。我可以在Word中编写一个宏来关闭所有打开的工作簿并退出Excel的所有(后台)实例吗?

excel vba ms-word word-vba

1
推荐指数
1
解决办法
914
查看次数