从VBA打开word 2007文档不起作用,但适用于使用Word 2003创建的文档 [英] Opening word 2007 document from VBA not working, but works for documents created with Word 2003

查看:163
本文介绍了从VBA打开word 2007文档不起作用,但适用于使用Word 2003创建的文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码用于将Word文档中的数据填充到Excel中 - 它在开发它的Office 2003下运行正常。 它首先在Office 2007中工作正常,但它只适用于在Word 2003中创建
的原始文档。 在Word 2007中创建源文件(使用.dot模板创建并以.doc格式保存)时,它不起作用 - 它会在文档打开行引发错误"Set wdDoc = wdApp.Documents.Open(filename := pname& fname)"


关于这里可能出现什么问题的任何想法?是否可以在Word 2007中使用Word 2003 .doc模板?

 Sub GetInvoiceDetail()

如果ActiveSheet.Name<> "收据(工作)"然后
MsgBox"此函数凸轮仅用于收据(作业)表单",vbOKOnly,"错误"和"错误"。
退出Sub
结束如果

Dim wdApp As Object
Dim wdDoc As Object
Dim pname,fname
Dim t1,t3,t4 ,t5,t8
暗淡选择

pname = ActiveWorkbook.Sheets(" Tables")。范围(" K5")。值
fname =" I-" ; &安培; ActiveCell

设置wdApp = CreateObject(" Word.application")
On Error GoTo Err_Handler
设置wdDoc = wdApp.Documents.Open(filename:= pname& fname)
On Error GoTo 0
t1 = wdDoc.formfields(" Text1")。result
t3 = wdDoc.formfields(" Text3")。result
t4 = wdDoc。 formfields(" Text4")。result
t5 = wdDoc.formfields(" Text5")。result
t8 = wdDoc.formfields(" Text8")。result
choice = MsgBox (" Invoice Number:& ActiveCell& vbCr& _
" Dated:& t1& vbCr& _
" For:"& t8& ; vbCr& vbCr& _
" To:"& vbCr& t3& vbCr& vbCr& _
" Location:& vbCr& t4& ; vbCr& vbCr& _
" Description:& vbCr& t5& _
vbCr& vbCr&"你想要复制发票D吗? ATA?",_
vbYesNoCancel," Get Invoice Data")

如果choice = vbYes则
ActiveCell.Offset(0,1).Value = CDbl(t8 )
ActiveCell.Offset(0,3).Value = CDate(t1)
结束如果

wdDoc.Close savechanges:= False
设置wdDoc = Nothing
wdApp.Quit
设置wdApp = Nothing
退出Sub
Err_Handler:
MsgBox("未找到发票文件 - 您是否创建了此发票?")
wdApp.Quit
设置wdApp = Nothing
End Sub

解决方案

Williamp99,



我代表正在处理此问题的Word工程师回复。为了回答您的问题,我们需要以下信息:


1.       ;
Word 2007内部版本号是什么?&rsquo ;重新运行此自动化?
$
Office按钮> 资源

在括号中提供以12.0开头的两个数字。####。####



2.      
请为Excel 2007提供相同内容建立你的运行宏。



3.     &NBSP;&NBSP;
什么是完整路径和文件名由&
Debug.Print pname& ;;返回的Word文件你2007年环境中的fname





4.   ;&NBSP;&NBSP;&NBSP;&NBSP;
邮件的确切文字是什么你在代码中看到的是什么,当它到达行时
  "设置wdDoc = wdApp.Documents.Open(文件名:= pname& fname)" b $ b b请注意,您可能需要禁用

On Error GoTo Err_Handler

以获取VBA中的完整错误消息。 style ="">


< span style ="font-size:small"> 5.      
是否始终手动创建Word文件或是通过自动化?

如果它是自动化的,创建文件的自动化是什么?



6.      
你有Word文件扩展名显示在Windows资源管理器中?
$
要求验证当您在WD2007中保存文件时,它将以预期的.doc扩展名保存。



7。      
使用Word 2003,您可以使用.dot模板打开/编辑/保存在Word 2007中创建的.doc文件吗?


谢谢,



将Buffington用于
Microsoft Excel支持


The following bit of code is used to populated data from a Word document to Excel - it worked fine under Office 2003 where it was developed.  It first looked to work fine in Office 2007, however it only works with the original documents created in Word 2003.  It does not work when the source file is created in Word 2007 (created using a .dot template and saving in .doc format) - it throws an error an the document open line "Set wdDoc = wdApp.Documents.Open(filename:=pname & fname)"

Any ideas as to what might be going wrong here? Could it be the use of the Word 2003 .doc template in Word 2007?

Sub GetInvoiceDetail()
  
  If ActiveSheet.Name <> "Receipts (Jobs)" Then
    MsgBox "This function cam only be used in the Receipts (Jobs) sheet", vbOKOnly, "Error"
    Exit Sub
  End If
  
  Dim wdApp As Object
  Dim wdDoc As Object
  Dim pname, fname
  Dim t1, t3, t4, t5, t8
  Dim choice
  
  pname = ActiveWorkbook.Sheets("Tables").Range("K5").Value
  fname = "I-" & ActiveCell
  
  Set wdApp = CreateObject("Word.application")
  On Error GoTo Err_Handler
  Set wdDoc = wdApp.Documents.Open(filename:=pname & fname)
  On Error GoTo 0
  t1 = wdDoc.formfields("Text1").result
  t3 = wdDoc.formfields("Text3").result
  t4 = wdDoc.formfields("Text4").result
  t5 = wdDoc.formfields("Text5").result
  t8 = wdDoc.formfields("Text8").result
  choice = MsgBox("Invoice Number: " & ActiveCell & vbCr & _
        "Dated: " & t1 & vbCr & _
        "For: " & t8 & vbCr & vbCr & _
        "To:" & vbCr & t3 & vbCr & vbCr & _
        "Location:" & vbCr & t4 & vbCr & vbCr & _
        "Description:" & vbCr & t5 & _
        vbCr & vbCr & "DO YOU WANT TO COPY INVOICE DATA ?", _
        vbYesNoCancel, "Get Invoice Data")
  
  If choice = vbYes Then
    ActiveCell.Offset(0, 1).Value = CDbl(t8)
    ActiveCell.Offset(0, 3).Value = CDate(t1)
  End If
  
  wdDoc.Close savechanges:=False
  Set wdDoc = Nothing
  wdApp.Quit
  Set wdApp = Nothing
Exit Sub
Err_Handler:
  MsgBox ("Invoice file not found - have you created this invoice yet?")
  wdApp.Quit
  Set wdApp = Nothing
End Sub

解决方案

Williamp99,

I'm replying on behalf of a Word engineer who is working this issue. In order to answer your question, we are going to need the following information:

1.      What is the Word 2007 build number you’re running this automation on?
Office button > Word Options button > Resources
provide the two numbers in parenthesis that start with 12.0.####.####

2.      Please provide the same for the Excel 2007 build you’re running the macro in.

3.      What is the full path and file name of the Word file that’s returned by
Debug.Print pname & fname
in your 2007 environment?

4.      What is the exact text of the message you’re seeing in the code when it hits the line
 "Set wdDoc = wdApp.Documents.Open(filename:=pname & fname)"
Note that you may need to disable
On Error GoTo Err_Handler
to get the full error message in VBA.

5.      Does the Word file always get created manually or is it via automation?
If it’s automated, what is the automation to create the file?

6.      Do you have the Word file extensions showing in Windows Explorer?
Asking this to verify that when you save the file in WD2007, that it is getting saved with the expected .doc extension.

7.      Using Word 2003, can you open/edit/save the .doc file you created in Word 2007 using the .dot template?

Thanks,

Will Buffington
Microsoft Excel Support


这篇关于从VBA打开word 2007文档不起作用,但适用于使用Word 2003创建的文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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