简体   繁体   English

VBA:选择活动的Word文档

[英]VBA: selecting an active word document

I recently learned how to manipulate Word documents through VBA in Excel. 我最近学习了如何通过Excel中的VBA处理Word文档。 My problem is I want my macro to be able to determine an active word document with a proper filename and select that document and continue the macro. 我的问题是我希望宏能够确定具有正确文件名的活动Word文档,然后选择该文档并继续执行宏。 I have this code below 我下面有这段代码

Sub CreateNewWordFile()
  Dim wrd As Word.Application
  Dim Opfile As Object
  Dim AcFile As Object

  Set Opfile = GetObject(, "Word.Application")
  Debug.Print Opfile.Documents.count
  For Each AcFile In Opfile.Documents
    If AcFile = "SampleWord " & Format(Now, "mm-dd-yyyy") & ".docx" Then
      Set wrd = AcFile.Application
      With wrd
       .Activate
       .Selection.TypeParagraph
       .Selection.TypeParagraph
       .Selection.Font.Size = 20
       .Selection.TypeText "success"
       .ActiveDocument.Save
       GoTo Label1
      End With
    End If
  Next
  Set wrd = New Word.Application

 With wrd
   .Visible = True
   .Activate
   .Documents.Add
   With .Selection
       .ParagraphFormat.Alignment = wdAlignParagraphCenter
       .BoldRun
       .Font.Size = 18
       .TypeText "Sample Word File"
       .BoldRun
       .TypeParagraph
       .Font.Size = 12
       .ParagraphFormat.Alignment = wdAlignParagraphLeft
       .TypeParagraph
       .ParagraphFormat.Alignment = wdAlignParagraphCenter
       .Font.Size = 15
       .TypeText "samples"
   End With

  ActiveDocument.SaveAs2 Filename:="Documents\SampleWord " & Format(Now, "mm-dd-yyyy") & ".docx"

  End With

Label1:

  Set Opfile = Nothing
  Set wrd = Nothing

End Sub

Below the For Each loop is my code for creating a Word document in case there is no open Word document with the proper filename. For Each循环下面是我的代码,用于在没有打开的Word文档且文件名正确的情况下创建Word文档。 The problem is when the For Each Loop runs the macro it gives the error 问题是当For Each循环运行宏时,它给出错误

ActiveX component can't create Object ActiveX组件无法创建对象

But when I turn it into a comment and just run my code that creates a Word document and uncomment it for my second run to test, it works. 但是,当我将其变成注释并仅运行创建Word文档的代码并取消注释以进行第二次测试时,它就可以工作。 Also I notice that the Documents.count doesn't count the open Word documents. 另外,我注意到Documents.count不计算打开的Word文档。 I tried opening several Word documents but it doesn't count them. 我尝试打开多个Word文档,但没有算在内。 I hope someone can help, thank you. 希望有人能帮助您,谢谢。

I'm going to give you a lot of information, but not change your code. 我将为您提供很多信息,但不会更改您的代码。 You'll learn more if you can implement what I explain. 如果可以实施我的解释,您将学到更多。 Your main problem comes from not fully grasping what's happening when you use GetObject vs. New Word.Application . 您的主要问题来自于不完全了解使用GetObjectNew Word.Application时发生的情况。 Once you get that sorted you should be OK. 一旦得到排序,就可以了。

If Word is not running at all, then GetObject returns the error message you're seeing. 如果Word根本没有运行,则GetObject返回您所看到的错误消息。 The typical way to handle that is to test the error and start Word, if necessary, for example 例如,处理错误并启动Word的典型方法是,例如

On Error Resume Next
Set wrd = GetObject(, "Word.Application")
If err.number = 429 Then
    Set wrd = new Word.Application
End If
On Error GoTo 0

HOWEVER, since you're looking for a specific document, as long as that document has been saved and you know the file path, you could (but don't have to) use 但是,由于您要查找的是特定文档,因此只要保存了该文档并且知道文件路径,就可以(但不必)使用

Dim wrdDoc as Object
Set wrdDoc = GetObject("C:\ExamplePath\DocName.docx")
Set wrd = wrdDoc.Application

It's also not necessary to loop the Documents collection to pick up a document with a certain name. 也不必循环Documents集合以选择具有特定名称的文档。 You can also do: 您也可以:

Set acFile = wrd.Documents("Filename")

You can test whether that document exists using 您可以使用以下方法测试该文档是否存在

If acFile Is Nothing Then
  'put the code to create a new DOCUMENT here
  Set acFile = wrd.Documents.Add
  'Do all the formatting, etc. here
End If

The main problem with Documents.Count comes from using Documents.Count的主要问题来自使用

Set wrd = New Word.Application

every time the code doesn't find the specific document. 每当代码未找到特定文档时。 This creates a new instance of Word every time it executes. 每次执行时都会创建一个的Word 实例 Each instance is independent of any others, which is why Documents.Count doesn't return a number equal to all the documents you've generated. 每个实例都独立于其他实例,这就是为什么Documents.Count不返回等于您生成的所有文档的数字的原因。 It is only running for the current instance of Word. 它仅在Word的当前实例上运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM