2017-09-22 3 views
0
여기

는 지금까지 :)이 무엇VBA를 사용하여 바닥 글에 파일 이름을 추가하려면 어떻게합니까?

, 그는 엑셀에서 생성되는 내가 파일 경로와 워드 문서의 바닥 글에 파일 이름을 추가하면된다하려고

...

Function ReportTypeC() 

Dim wdApp As Word.Application 
Dim wb As Workbook 
Dim SrcePath As String 
Dim FileName As String 

FileName = ActiveDocument.FullName 

SrcePath = "L:\TEST\Archive\unnamed.jpg" 

Set wdApp = New Word.Application 

With wdApp 
    .Visible = True 
    .Activate 

    .Documents.Add 
    Application.CutCopyMode = False 

    .ActiveDocument.Sections.Item(1).Headers(wdHeaderFooterPrimary) _ 
     .Range.InlineShapes.AddPicture (SrcePath) 

    .ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary) _ 
    .PageNumbers.Add PageNumberAlignment:=wdAlignPageNumberLeft, FirstPage:=True 

    'With ActiveDocument.Sections(1) 
     '.Footers(wdHeaderFooterPrimary).Range.Text = "FileName" 
    'End With 
End With 

End Function 
+0

질문에 대답이 없습니다 .... – Luuklag

+0

작동하지 않습니까? 오류가 있습니까? – braX

+0

코드에서 수행해야 할 작업은 무엇입니까? 실제로 무엇을하거나하지 않습니까? –

답변

0

여기에는 바닥 글에 문서 이름을 입력하여 필요한만큼 확장 할 수있는 기능이 있습니다.

Option Explicit 

Function ReportTypeC() 

Dim wdApp As Word.Application 
Set wdApp = CreateObject("Word.Application") 
wdApp.Visible = True 
wdApp.Documents.Add 

ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooter 
Selection.TypeText Text:=ThisWorkbook.Path & thisworkbook.Name & ".docx" 
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument 

appWD.ActiveDocument.SaveAs Filename:=ThisWorkbook.Path & thisworkbook.Name & ".docx" 
wdApp.ActiveDocument.Close 
wdApp.Quit 
End Function 
0

질문이 명확하지 않습니다.

문서의 이름이 파일에 있어야하는 경우 먼저 파일을 저장해야하며 이름을 지정해야합니다. 당신이 파일의 엑셀 통합 문서의 파일 경로/이름이해야하는 경우

Sub ReportTypeC() 

    Dim wdApp As New Word.Application 
    Dim wdDoc as Word.Document 
    Dim SrcePath As String 
    Dim FileName As String 

    SrcePath = "L:\TEST\Archive\unnamed.jpg" 

    With wdApp 
     .Visible = True 
     .Activate 
     Set wdDoc = .Documents.Add 
    End With 

    'Build your file path and file name here; I am using ThisWorkbook assuming we are exporting to the same directory as the workbook, and calling the exported document "mydocument.docx" 
    FileName = ThisWorkbook.Path & "\" & "mydocument.docx" 

    With wdDoc 
    .SaveAs FileName:=FileName 
    With .Sections(1) 
     .Headers(wdHeaderFooterPrimary).Range.InlineShapes.AddPicture SrcePath 
     .Footers(wdHeaderFooterPrimary).PageNumbers.Add PageNumberAlignment:=wdAlignPageNumberLeft, FirstPage:=True 
     .Footers(wdHeaderFooterPrimary).Range.Text = FileName 
    End With 
    .Save 
    End With 

End Sub 

는, 당신은 단지 ThisWorkbook 개체와 개체의 FullName 속성을 참조 할 필요가있다.

Sub ReportTypeC() 

    Dim wdApp As New Word.Application 
    Dim wdDoc as Word.Document 
    Dim SrcePath As String 

    SrcePath = "L:\TEST\Archive\unnamed.jpg" 

    With wdApp 
     .Visible = True 
     .Activate 
     Set wdDoc = .Documents.Add 
    End With 

    With wdDoc 
     With .Sections(1) 
      .Headers(wdHeaderFooterPrimary).Range.InlineShapes.AddPicture SrcePath 
      .Footers(wdHeaderFooterPrimary).PageNumbers.Add PageNumberAlignment:=wdAlignPageNumberLeft, FirstPage:=True 
      .Footers(wdHeaderFooterPrimary).Range.Text = ThisWorkbook.FullName 
     End With 
     .Save 
    End With 

End Sub 

개인적으로, 그러나, 처음부터 내가 매크로를 호출 할 때마다 문서를 구축하기보다는, 나는 템플릿을 만들 읽기 전용 모드에서 문서를 열고 찾을 사용하고 동적 데이터를 대체 할 대체 할 것이다 . 예

Sub ReportTypeC() 

    Dim wdApp As New Word.Application 
    Dim wdDoc as Word.Document 
    Dim SrcePath As String 
    Dim FileName As String 
    Dim wdRange as Word.Range 
    Const TemplatePath as String = "L:\TEST\Archive\Report C template.docx" ' This template contains the text "{{ FileName }}" and "{{ SourceWorkbook }}" in the footer, which is to be replaced. 

    SrcePath = "L:\TEST\Archive\unnamed.jpg" 

    With wdApp 
     .Visible = True 
     .Activate 
     Set wdDoc = .Documents.Open(FileName:=TemplatePath, ReadOnly:=True) 
    End With 

    ' Exported file 
    FileName = "L:\TEST\Archive\" & "Report C " & Format(Now, "yyyy-mm-dd") & ".docx" ' e.g. "Report C 2017-09-27.docx" 

    With wdDoc 
     With .Sections(1).Footers(wdHeaderFooterPrimary) 
      ' If we are sure that the template contains "{{ SourceWorkbook }}"), we can work with the range directly 
      FindRange(.Range, "{{ SourceWorkbook }}").Text = ThisWorkbook.FullName 
      ' If we aren't sure whether the template contains "{{ FileName }}" we need to check there's a match, so it doesn't replace the whole footer range 
      Set wdRange = FindRange(.Range, "{{ FileName }}") 
      If wdRange.Text = "{{ FileName }}" Then wdRange.Text = FileName 
     End With 
     ' Save the file 
     .SaveAs FileName:=FileName 
    End With 

End Sub 

Function FindRange(ByRef rLook As Word.Range, ByVal strFind As String) As Word.Range ' returns the first range that is matched by the strFind string 
    rLook.Find.Execute Findtext:=strFind, MatchCase:=True, Forward:=True, Wrap:=wdFindStop, MatchWholeWord:=True 
    Set FindRange = rLook 
End Function