使用VBScript或VBA在Microsoft Word中处理内容 [英] Manipulating content in Microsoft Word with VBScript or VBA

查看:125
本文介绍了使用VBScript或VBA在Microsoft Word中处理内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我来这里是关于如何使用Excel开发VBScript和VBA应用程序的大量建议,但是现在我面临着一个新的挑战:为Word开发VBScript/VBA应用程序.

I've come here for plenty of advice on how to develop VBScript and VBA applications using Excel, but now I've been faced with a new challenge: develop a VBScript/VBA application for Word.

我知道,在Excel中,如果我想在单元格B3中键入"my name",我将输入以下内容:

I know, in Excel, if I wanted to type "my name" in cell B3, I would type this:

Range("B3").Value = "my name"

我需要能够找到要输入正式信件的名称和地址的地方,以及今天的日期和我的姓名缩写作为签名.

I need to be able to locate where a name and address for a formal letter would be entered, as well as today's date, and my initials as a signature.

我以为我可以像在Excel中那样在Internet上找到Word的VBScript/VBA编程,但是使用Word似乎并不流行.如果有人有任何摘要可以帮助我入门,或者是一个非常好的链接,可以链接到我可以自己进行编码的Internet站点,则将不胜感激.

I thought I might be able to find VBScript/VBA programming for Word on the internet like I did for Excel, but it seems like working with Word is not as popular. If anyone has any snippets to get me started, or a really good link to a site on the internet where I can do the coding myself, it would be greatly appreciated.

更新

这是我目前正在使用的代码:

Here is the code I'm working with at the moment:

Public Sub WordVbaDemo()
  Dim doc As Document: Set doc = ActiveDocument ' Or any other document
  DateText = doc.Range(doc.Paragraphs(1).Range.End - 20, doc.Paragraphs(1).Range.End - 18).Text
End Sub`

我需要代码来处理日期正确的正式信件:

I need the code to work for a formal letter where the date is right-justified:

                                                               Date: November 7th, 2016

我上面的代码将在日期:"之后复制日期文本.原始模板没有预填日期.如果输入一个,则"Member:"字段看起来像这样偏移:

The code I have above will copy the date text after "Date: ". The original template doesn't have a prefilled date. If I enter one, the "Member: " field looks offset like this:

                                                               Date: November 7th, 2016
                                                   Member:

我正在寻找一种输入文本而不破坏对齐方式的方法.

I'm looking for a way to enter text without upsetting the alignment.

更新2

我忘了提到它必须作为外部脚本来工作.这意味着,如果我要打开NotePad并创建一个脚本,该脚本将在Word文档中填写字母,那么它应该是这样工作的.我为此深表歉意……忘了自己,忘记了细节.

I forgot to mention this has to work as an external script. This means, if I were to open NotePad and create a script that would fill out a letter in a Word document, that is how it should work. I do apologize for this...got ahead of myself and forgot that detail.

更新3

我正在使用以下代码,这些代码是从我用来查找任何打开的Internet Explorer窗口的代码中衍生而来的.我知道IE和Word是两个不同的东西,但是我希望可以使用Shell查找Word文档并能够操纵其内容.

I'm using the following code derived from code I use to find any open Internet Explorer windows. I know IE and Word are two different things, but I was hoping I could use Shell to find the Word doc and be able to manipulate the content.

Dim WinDoc, Window, TitleFound
Dim WShell, objShell

Function Check_Document()
    On Error Resume Next
        Set WShell = CreateObject("WScript.Shell")
        Set objShell = CreateObject("Shell.Application")
    On Error GoTo 0

    Window = "non-member template.docx" 'Tried this without the .docx and failed

    TitleFound = False

    For Each WinDoc In objShell.Windows()
        If Err.Number = 0 Then
             If InStr(WinDoc.Document.Title, (Window)) Then
                Set objWord = WinDoc
                TitleFound = True
                Exit For
             End If
          End If
    Next

    If TitleFound = False Then
        MsgBox "Word doc not found"
    Else
        MsgBox "Found Word doc!!"
    End If

End Function

推荐答案

大约6个月前,我在类似的船上.我已经在Excel中完成了VBA,但被要求在Word中执行更多操作.关于Word VBA的事情是,与Excel文档相比,自动化Word文档的理由要少得多.根据我的收集,大多数情况涉及创建法律文件.

I was in a similar boat about 6 months ago. I had done VBA in Excel, but was asked to do some more in Word. The thing about Word VBA is that there are far fewer reasons to need to automate a Word document than an Excel document. From what I've gathered, most situations involve creating legal documents.

我已经走了很长一段路,并且确实有许多站点为我添加了书签,我将为您进一步挖掘..但这是在Word中使用VBA的快速入门. http://word.mvps.org/faqs/MacrosVBA/VBABasicsIn15Mins.htm

I've come a long way and I do have a number of sites bookmarked that I'll dig further into for you.. but this one is a quickstart to using VBA in Word. http://word.mvps.org/faqs/MacrosVBA/VBABasicsIn15Mins.htm

但是有一个要点:考虑是否要对文档布局进行结构化(您的问题中的我不清楚).如果要对布局进行结构化(确切地知道所有内容的去向),则可能要使用书签.否则,您可以考虑使用z32a7ul指示的段落方法.

But one pointer: consider if the document layout is going to be structured or not.(It wasn't clear to me in your question). If the layout is going to be structured, where you know exactly where everything is going, you might want to use bookmarks. Otherwise, you may consider the paragraphs method as indicated by z32a7ul.

我的项目使用UserForms作为输入.有时这是一个真正的挑战,但是通过将Userforms与Bookmarks结合使用,我可以允许用户在userForms中来回导航以及重新运行宏(假设他们没有删除所需的书签).

My project uses UserForms as input. It's been a real challenge at times, but by using Userforms with Bookmarks, I'm able to allow the user to navigate back and forth in the userForms as well as re-run the macro (assuming they have not deleted required bookmarks).

当然,因为我还在学习,所以也要加一点盐.对于它的价值,我还面临着使其在Mac平台上都能工作的附加挑战.

Of course, take this with a grain of salt since I'm still learning as well. For what it's worth, I've also had the added challenge of making this all work on the Mac platform.

这篇关于使用VBScript或VBA在Microsoft Word中处理内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆