2016-06-16 12 views
1

スプレッドシートからの入力を使用して単語コンテンツコントロールを設定しようとしています。私のコードがうまく動作しないか、一貫して2のうち1回動作します。エラー462が発生します。VBAのエラー462:MS WordにExcelを設定する

問題が何であるか把握するのに助けてくれる人がいますか?

ありがとうございます!

Private Sub Accept_Click() 

Dim directory As String 
Dim wrdApp As Word.Application 
Dim doc As Word.Document 
Dim fd As Office.FileDialog 
Dim dt As String 

Set wrdApp = CreateObject("Word.Application") 

directory = Application.ActiveWorkbook.Path 

Set fd = Application.FileDialog(msoFileDialogFilePicker) 

With fd 
    .InitialFileName = directory 
    .AllowMultiSelect = False 
    .Title = "Select doc letter" 
    .Filters.Add "All", "*.*" 

    If .Show = True Then 
     txtfilename = .SelectedItems(1) 
    End If 
End With 

wrdApp.Visible = True 
On Error GoTo Handler 

'i get error on the next line: 
Set doc = wrdApp.Documents.Open(txtfilename, , False, , , , , , , , , True) 

Documents(txtfilename).Activate 

For Each cc In ActiveDocument.StoryRanges(wdPrimaryHeaderStory).ContentControls 
    If cc.Tag = "uptitle" Then cc.Range.Text = mill_box.Text 
Next 

For Each cc In ActiveDocument.StoryRanges(wdMainTextStory).ContentControls 
    If cc.Tag = "client" Then cc.Range.Text = TextBox1.Text & Chr(10) & TextBox2.Text _ 
& ", " & TextBox3 & Chr(10) & TextBox4 & " , " & TextBox5 & Chr(10) & TextBox6 
    If cc.Tag = "mill" Then cc.Range.Text = mill_box.Text 


Next 
ActiveDocument.Windows.Application.WindowState = wdWindowStateMaximize 

Unload Me 

ActiveDocument.Activate 



Exit Sub 
Handler: 
Set wrdApp = Nothing 
Set doc = Nothing 
Unload Me 
MsgBox "error" 

End Sub 
+0

コードをステップ実行します。どのラインでエラーが発生していますか?何を試しましたか?このエラーにはVBAのヒット数がかなり多く、多くの場合、毎回発生することを指定しています。 – Rodger

+0

こんにちはRodger、私はコードでエラーが発生している行を述べた。私は本当にこのエラーについてのブログを見ました。私の場合は、コードが動作していない場合、私はExcelシートを閉じて、それは毎回動作します。ありがとう – OGERWOLF

+0

ああ、それはコメントとしてです。申し訳ありませんが、私は明るい灰色のテキストが欠けていました。 – Rodger

答えて

0

エラーがなくても、私をアンロードしてからサブを終了します。エラーが発生した場合のように、wrdAppとdocをNothingに設定しないでください。そのロジックを修正して、成功した実行で何も設定されないようにすると、それは起こり続けますか?

+0

txtfilenameには完全なパスが含まれています。さらに、コードを実行する前にワープロを終了します。 – OGERWOLF

1

面白いエラー!

  • はあなたを介してWordのApplicationオブジェクトを作成します。Microsoft knowledge base articleで議論の答えにポイント -

    (LMGTFY :) Googleが「エラー462」で私のために#2を襲った)this pageからの情報を読みますコードに変換し、変数に代入します。

  • Wordのアプリケーションメンバ(ActiveDocumentなど)の前に対象のApplication変数がない場合、VBAはその代わりに隠し変数を作成します。
  • VBAは自分の変数と隠し変数の両方を使ってWordを呼び出すことができるので、すべてうまく見えます。
  • 独自の変数をNothingに設定すると、隠し変数はWordを有効にしたままになります。
  • 2回目に近づくと、暗黙の隠し変数が物事を混乱させます。

あなたのコードはまた、代わりに独自のdocwrdAppActiveDocumentDocuments(txtfilename)にアクセスします。

これがあなたの問題の解決策であるかどうかはわかりませんが(それをチェックする時間はありません)、非常に適用可能なようです。

+0

あなたの応答のためにCarlさんに感謝します。私は、「隠れ変数」がすべての問題を引き起こしていると思います。しかし同時に、私は今週オフィス2016を設置しました。私のコードはちょうど停止しました!!とにかく...私はちょうど私が達成しようとしているものを解決する以下のソリューションを掲載しました。良いものを持っている! – OGERWOLF

0

数日間の試行錯誤の末、最終的には完了したコードがあります。この問題で私を助けるためにあなたの時間を取ってくれてありがとう皆様に感謝します。

Private Sub Accept_Click() 

Dim directory As String 
Dim wrdApp As Word.Application 
Dim doc As Word.Document 
Dim fd As Office.FileDialog 

On Error Resume Next 

'this part is not necessary, but end users will know they can't have other word 
'docs open when working with this file. 
Set wrdApp = GetObject(, "Word.Application") 
Set doc = wrdApp.Documents("Welcome letter.docx") 
If Error <> 0 Then 
    Call Killword 
End If 

On Error GoTo Error_Handler 

Set wrdApp = CreateObject("Word.Application") 
directory = Application.ActiveWorkbook.Path 
Set fd = Application.FileDialog(msoFileDialogFilePicker) 

With fd 
    .InitialFileName = directory 
    .AllowMultiSelect = False 
    .Title = "Select welcome letter" 
    .Filters.Add "All", "*.*" 

    If .Show = True Then 
     txtfilename = .SelectedItems(1) 
    End If 
End With 

'here is the thing: i had to instantiate the doc before making wrdapp visible. 
Set doc = wrdApp.Documents.Open(txtfilename, , False, , , , , , , , , True) 
    wrdApp.Visible = True 
doc.Activate 

'from now on I have no choice than to refer to doc (not documents(1) or activedocuments! neither work) 
For Each cc In doc.StoryRanges(wdPrimaryHeaderStory).ContentControls 
    If cc.Tag = "uptitle" Then cc.Range.Text = mill_box.Text 
Next 

For Each cc In doc.StoryRanges(wdMainTextStory).ContentControls 
'here goes the code i need to execute on my word document. can be anything. 

Next 
doc.Windows.Application.WindowState = wdWindowStateMaximize 

Unload Me 


Exit Sub 

Error_Handler: 
    Select Case Err.Number 
     Case 429, 91 
      Err = 0 
      Resume Next 
     Case Else 
      MsgBox ("An unexpected error has occured." & vbCrLf & vbCrLf & _ 
      "Error number: " & Err.Number & vbCrLf & "Error description: " & Err.Description) 
      Resume Next 
    End Select 


'Set wrdApp = Nothing 
'Set doc = Nothing 
Unload Me 


End Sub 
+0

あなたはそれを整理してうれしい!すべてのインスタンスを完全修飾することをお勧めします。コードが適格化されているかどうかにかかわらず、コードが問題なく動作しても問題ありません。コードを明確にし、堅牢性を向上させるだけです。あなたがこれを行うことができるかどうかわからなかったので、もう一つの教訓も私には分かりましたが、今もあなたはどちらもそうすべきではないことを知っています;) –

関連する問題